如何使用java.util.Date进行日历算术? [英] How do I do calendar arithmetic with java.util.Date?

查看:80
本文介绍了如何使用java.util.Date进行日历算术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个表示时间的Date对象。我如何向这个对象添加5分钟?

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

推荐答案

你可以使用 日历 ,这将使添加任何时间长度:

You could use Calendar, which will make it easy to add any length of time:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, 5);
Date newDate = cal.getTime();

对于您的情况,您可以添加时间(以毫秒为单位):

For your case you could just add the time in milliseconds like this:

Date newDate = new Date(date.getTime() + 5 * 60 * 1000L);

这篇关于如何使用java.util.Date进行日历算术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆