tl;dr
ZonedDateTime.now() // Current moment captured in the JVM’s current default time zone. Better to explicitly pass the desired/expected time zone rather than rely implicitly on the default.
.with( // Adjust value of the original object to produce a new object. Using immutable objects pattern.
LocalTime.of( 6 , 0 ) // 6 AM specified in a time-of-day-only object.
) // Returns a new `ZonedDateTime` object. If the specified time-of-day were not valid on that date in that zone, the time would be adjusted as needed.
java.time
The modern approach uses the java.time classes that supplanted the troublesome old date-time classes such as Date
& Calendar
.
If you meant android.text.format.Time
, that class was deprecated in Android 22. Now supplanted by the ZonedDateTime
class.
Your Question is unclear about whether you wanted time-of-day only or date-with-time-of-day values.
Time-of-day only
For a time-of-day value without date and without time zone, use LocalTime
.
LocalTime lt = LocalTime.of( 6 , 0 ) ; // 6 AM.
LocalTime minutesLater = lt.plusMinutes( 70 ) ; // Add any number of minutes.
minutesLater.toString(): 07:10
Note that LocalTime
is limited to single 24-hour stretch of time. The adding seven hours to 11 PM gets you 6 AM.
LocalTime lt = LocalTime.of( 23 , 0 );
LocalTime later = lt.plusHours( 7 );
later.toString(): 06:00
Date with time
If you were interested in a date-with-time-of-day, then use ZonedDateTime
with LocalTime
class. And of course you will need the ZoneId
class as well, to get the current date and time. For any given moment, the date and time-of-day both vary around the globe by zone.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdtNow = ZonedDateTime.now( z ) ; // Capture the current moment according to the wall-clock time used by the people of a certain region (a time zone).
Specify the time-of-day, and adjust. If your specified time-of-day happens to not be valid in that zone on that date such as during a Daylight Saving Time (DST) cut-over, the ZonedDateTime
class adjusts as necessary. Be sure to read the documentation to understand its algorithms for that adjustment.
LocalTime lt = LocalTime.of( 6 , 0 ) ; // 6 AM.
ZonedDateTime zdtSixAmToday = zdtNow.with( lt ) ; // Same date, same time zone, different time-of-day. Will be adjusted if your given time-of-day is not valid on that date in that zone.
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?