Set time of day in ZonedDateTime in java.time?

How to change part time of an existing object ZonedDateTime? I want to keep the date and time zone, but change the hour and minutes.

+4
source share
1 answer

TL; DR

zdt.with ( LocalTime.of ( 16 , 15 ) )

Immutable objects

The java.time classes use the Immutable Objects template to create new objects and not modify ("mutate") the original object.

with()

A method ZonedDateTime::withis a flexible way of generating a new one ZonedDateTimeon the basis of another, but with a certain difference. You can pass any object that implements the interface TemporalAdjustor.

. LocalTime - - . LocalTime TemporalAdjustor. , .

ZonedDateTime marketOpens = ZonedDateTime.of ( LocalDate.of ( 2016 , 1 , 4 ) , LocalTime.of ( 9 , 30 ) , ZoneId.of ( "America/New_York" ) );
ZonedDateTime marketCloses = marketOpens.with ( LocalTime.of ( 16 , 0 ) );

, , .

Duration duration = Duration.between ( marketOpens , marketCloses );

.

System.out.println ( "marketOpens: " + marketOpens + " | marketCloses: " + marketCloses + " | duration: " + duration );

marketOpens: 2016-01-04T09: 30-05: 00 [America/New_York] | marketCloses: 2016-01-04T16: 00-05: 00 [America/New_York] | : PT6H30M

, . LocalTime , , . . 0 LocalTime. LocalTime ZonedDateTime.

TemporalAdjustor. . , LocalDate, Month, Year . , , .

Hochschild. , , . , (DST).

+13

All Articles