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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
To simplify @Christopher's example.
Say you have a constant
You can write.
If you use long to store date/time instead of the Date object you can do
Using the newish java.util.concurrent.TimeUnit class you can do it like this
If you use Apache Commons / Lang, you can do it in one step using
DateUtils.addHours()
:(The original object is unchanged)
This is another piece of code when your
Date
object is in Datetime format. The beauty of this code is, If you give more number of hours the date will also update accordingly.Here is the Output:
tl;dr
Or…
Using java.time
The java.time framework built into Java 8 and later supplants the old Java.util.Date/.Calendar classes. Those old classes are notoriously troublesome. Avoid them.
Use the
toInstant
method newly added to java.util.Date to convert from the old type to the new java.time type. AnInstant
is a moment on the time line in UTC with a resolution of nanoseconds.You can add hours to that
Instant
by passing aTemporalAmount
such asDuration
.To read that date-time, generate a String in standard ISO 8601 format by calling
toString
.You may want to see that moment through the lens of some region’s wall-clock time. Adjust the
Instant
into your desired/expected time zone by creating aZonedDateTime
.Alternatively, you can call
plusHours
to add your count of hours. Being zoned means Daylight Saving Time (DST) and other anomalies will be handled on your behalf.You should avoid using the old date-time classes including
java.util.Date
and.Calendar
. But if you truly need ajava.util.Date
for interoperability with classes not yet updated for java.time types, convert fromZonedDateTime
viaInstant
. New methods added to the old classes facilitate conversion to/from java.time types.For more discussion on converting, see my Answer to the Question, Convert java.util.Date to what “java.time” type?.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.If you're willing to use
java.time
, here's a method to add ISO 8601 formatted durations: