How to add minutes to a date variable in java [dup

2020-05-03 10:23发布

I have a date variable (endTime) with some value (eg : 10:40). I need to create a new variable by adding 10 minutes to endTime. How can I do it?

Thanks in advance

public static  String endTime = "";
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();

endTime = timeFormat.format(cal.getTime());`

标签: java
1条回答
手持菜刀,她持情操
2楼-- · 2020-05-03 10:40

You can use add method on your Calendar:

public static  String endTime = "";
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();

cal.add(Calendar.MINUTE, 10)

endTime = timeFormat.format(cal.getTime());`

Found in Javadoc, it takes me 30 seconds.

查看更多
登录 后发表回答