在Java中添加n小时到日期? [英] Adding n hours to a date in Java?

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

问题描述

如何在Date对象中添加n小时?我发现另一个例子是使用StackOverflow上的日子,但仍然不明白如何在几小时内完成。

How do I add n hours to a Date object? I found another example using days on StackOverflow, but still don't understand how to do it with hours.

推荐答案

检查日历类。它有添加方法(和其他一些)以允许时间操纵。这样的事情应该可以工作。

Check Calendar class. It has add method (and some others) to allow time manipulation. Something like this should work.

    Calendar cal = Calendar.getInstance(); // creates calendar
    cal.setTime(new Date()); // sets calendar time/date
    cal.add(Calendar.HOUR_OF_DAY, 1); // adds one hour
    cal.getTime(); // returns new date object, one hour in the future

检查 API

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

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