date - Getting time range between midnight and current time JDK 8 -


i have method calculate midnigt , current time long values:

/**  * returns time range between midnight , current time in milliseconds.  *  * @param zoneid time zone id.  * @return {@code long} array, @ index: 0 - midnight time; 1 - current time.  */  public static long[] todaydaterange(zoneid zoneid) {     long[] toreturn = new long[2];      localtime midnight = localtime.midnight;     localdate today = localdate.now(zoneid);     localdatetime todaymidnight = localdatetime.of(today, midnight);     zoneddatetime todaymidnightzdt = todaymidnight.atzone(zoneid);     toreturn[0] = todaymidnightzdt.toinstant().toepochmilli();      zoneddatetime nowzdt = localdatetime.now().atzone(zoneid);     toreturn[1] = nowzdt.toinstant().toepochmilli();     return toreturn; } 

perhaps there simpler way that?

you do:

zoneddatetime nowzdt = zoneddatetime.now(zoneid);  zoneddatetime todayatmidnightzdt = nowzdt.with(localtime.midnight); 

i can't think of simpler way it.


localdatetime vs zoneddatetime

there's (tricky) difference between localdatetime.now().atzone(zoneid) , zoneddatetime.now(zoneid).

for code below, i'm using jvm in default timezone america/sao_paulo , try current date , time in timezone (europe/london). @ moment run code, it's august 20th 2017, in são paulo time 17:56 , in london 21:56.

when do:

localdatetime nowldt = localdatetime.now(); 

it creates localdatetime current date , time in jvm's default timezone. in case, it'll current date , time in são paulo's timezone (which august 20th 2017, @ 17:56):

2017-08-20t17:56:05.159

when call atzone method, creates zoneddatetime corresponds this date , time in specified zone:

zoneid zoneid = zoneid.of("europe/london"); zoneddatetime nowatzone = nowldt.atzone(zoneid); 

the nowatzone variable be:

2017-08-20t17:56:05.159+01:00[europe/london]

the same date (august 20th 2017) , time (17:56) in london timezone. note it's not current date/time in london. if equivalent epochmilli:

system.out.println(nowatzone.toinstant().toepochmilli()); 

it be:

1503248165159

now, if don't use localdatetime , direclty use zoneddatetime instead:

zoneddatetime nowzdt = zoneddatetime.now(zoneid); 

it current date , time in london, be:

2017-08-20t21:56:05.170+01:00[europe/london]

note time changed (it's 21:56). that's because right now, @ moment, that's current time in london. if epochmilli value:

system.out.println(nowzdt.toinstant().toepochmilli()); 

the value be:

1503262565170

note it's different first case using localdatetime (even if ignore difference in milliseconds value, because hour different). if want current date , time @ specified timezone, must use zoneddatetime.now(zoneid).

using localdatetime.now().atzone() not gives different result, change if run in different jvm's, or if jvm default timezone changes (someone might misconfigure it, or application running in same vm calls timezone.setdefault()).


daylight saving time

just remind corner cases due dst (daylight saving time) issues. i'm gonna use timezone live in example (america/sao_paulo).

in são paulo, dst starts @ october 15th 2017: @ midnight, clocks shift 1 hour forward midnight 1 am. local times between 00:00 , 00:59 don't exist in timezone. if create local date in interval, it's adjusted next valid moment:

zoneid zone = zoneid.of("america/sao_paulo");  // october 15th 2017 @ midnight, dst starts in sao paulo localdatetime d = localdatetime.of(2017, 10, 15, 0, 0, 0, 0); zoneddatetime z = d.atzone(zone); system.out.println(z);// adjusted 2017-10-15t01:00-02:00[america/sao_paulo] 

when dst ends: in february 18th 2018 @ midnight, clocks shift back 1 hour, midnight 23 pm of 17th. local times 23:00 23:59 exist twice (in dst , in non-dst), , must decide 1 want:

// february 18th 2018 @ midnight, dst ends in sao paulo // local times 23:00 23:59 @ 17th exist twice localdatetime d = localdatetime.of(2018, 2, 17, 23, 0, 0, 0); // default, gets offset before dst ends zoneddatetime beforedst = d.atzone(zone); system.out.println(beforedst); // before dst end: 2018-02-17t23:00-02:00[america/sao_paulo]  // offset after dst ends zoneddatetime afterdst = beforedst.withlateroffsetatoverlap(); system.out.println(afterdst); // after dst end: 2018-02-17t23:00-03:00[america/sao_paulo] 

note dates before , after dst ends have different offsets (-02:00 , -03:00). affects value of epochmilli.

the above can happen if adjust time using with method.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -