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

前端 未结 6 840
無奈伤痛
無奈伤痛 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:42

    Date has the time in milli-seconds. However, you might find using a long is simpler for this type of calculations.

    Date date1 = new Date();
    long time1 = date1.getTime();
    long time2 = time1 + 5 * 60 * 1000;
    Date date2 = new Date(time2);
    

    If you use plain long you can drop the lines with Date objects.

提交回复
热议问题