How do I do calendar arithmetic with java.util.Date?

前端 未结 6 837
無奈伤痛
無奈伤痛 2020-12-08 06:46

Currently I have a Date object representing a time. How would I add 5 minutes to this object?

6条回答
  •  眼角桃花
    2020-12-08 07:48

    With Java 8 there are new options. In fact Joda Time recommends moving to Java 8 Date and Time APIs as soon as possible. Here is an example of adding time from this link:

    https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

    The following code adds 5 minutes to the current time:

    LocalDateTime now = LocalDateTime.now();
    now = now.plusMinutes(5);
    System.out.println(now);
    

提交回复
热议问题