tl;dr
ZonedDateTime.of(
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) , // Current date in a particular time zone.
LocalTime.parse( "23:00" ) , // Specify 11 PM.
ZoneId.of( "Pacific/Auckland" ) // Specify a time zone as the context for this date and time. Adjustments made automatically if that date-time is not valid in that zone.
)
2018-01-23T23:00+13:00[Pacific/Auckland]
java.time
The modern approach uses java.time classes instead of the troublesome old legacy date-time classes.
java.time.LocalTime
For a time-of-day only, without a date and without a time zone, use the LocalTime
class.
LocalTime lt = LocalTime.parse( "23:00" ) ;
To determine a specific point on the timeline with that time-of-day, apply a date (LocalDate
) and a time zone (ZoneId
) to produce a ZonedDateTime
object.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Africa/Tunis" );
Use that zone to get the current date.
LocalDate ld = LocalDate.now( z ) ;
ZonedDateTime
Combine.
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
zdt.toString(): 2018-01-23T23:00+01:00[Africa/Tunis]
Keep in mind that for that particular zone, your date and time-of-day may not be valid. The ZonedDateTime
class adjusts automatically. Study the doc to be sure you understand and agree with its algorithm for that adjustment.
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.
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.