将X小时加到日期&时间 [英] Add X hours to a date & time

查看:118
本文介绍了将X小时加到日期&时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在获取时间和日期槽:

I am currently fetching the time and date trough:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

此示例返回'05 / 14/2014 01:10:00'

This returns the example '05/14/2014 01:10:00'

现在我要努力做到这一点,这样我就可以增加一个小时的时间,而不必担心新的一天或一个月等等。

Now I am trying to make it so I can add a hour to this time without having to worry about a new day or month etc.

我将如何继续获取 05/14/2014 01:10:00,然后又以相同的格式播放10小时?

How would I go on getting '05/14/2014 01:10:00' but then for 10 hours later in the same format?

请先谢谢。

推荐答案

查看Calendar对象:
日历

Look at the Calendar object: Calendar

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 10);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(dateFormat.format(cal.getTime()));

这篇关于将X小时加到日期&时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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