java - Get previous occurrence of a LocalTime -


how previous occurrence of specified local time instant, system timezone ?

this means getting today @ specified time or yesterday @ specified time depending whether specified time today before or after now.

of course, need take account timezone switch because of daylight saving. is, timezone offset can different today , yesterday.

this i've got :

public instant getpreviousoccurence(localtime scheduledtime) {     instant = instant.now();     instant todayatspecifiedtime = now.with(scheduledtime);     return todayatspecifiedtime.isafter(now) ? todayatspecifiedtime.minus(1, chronounit.days) : todayatspecifiedtime; } 

but after checking source of instant.minus(), noticed removes 84600 seconds day, wrong in case. additionally, i'm not sure whether instant.with() use system timezone or utc.

edit 1

in case there no occurrence of specified time today (because of timezone shift), instant of timezone shift should returned. in case there 2 occurrences of specified time today, latest in past should returned.

edit 2

after checking product owner, turns out in case there 2 occurrences of specified time within day, returning first (or returning second) fine. don't need both.

many jon skeet pointing me zoneddatetime. here solution using type.

public instant getpreviousoccurence(localtime scheduledtime) {     instant = instant.now();     zoneddatetime todayatscheduledtime = zoneddatetime.ofinstant(now, europe_paris).with(scheduledtime).withearlieroffsetatoverlap();     if (todayatscheduledtime.toinstant().isafter(now)) {         return todayatscheduledtime.minusdays(1).withearlieroffsetatoverlap().toinstant();     } else {         return todayatscheduledtime.toinstant();     } } 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -