date - Getting time range between the first day of current week and current time JDK 8 -


i can easilly calculate time period between first day of month , current time:

/**  * returns time range between first day of month , current time in milliseconds.  *  * @param zoneid time zone id.  * @return {@code long} array, @ index: 0 - first day of month midnight time; 1 - current time.  */  public static long[] monthdaterange(zoneid zoneid) {     long[] toreturn = new long[2];  zoneddatetime nowzdt = localdatetime.now().atzone(zoneid); zoneddatetime startzdt = nowzdt.withdayofmonth(1); toreturn[0] = startzdt.toinstant().toepochmilli(); toreturn[1] = nowzdt.toinstant().toepochmilli(); return toreturn; } 

but how start counting @ first day (midnight) of current week?

tl;dr

zoneddatetime     .now( zoneid.of( "asia/kolkata" ) )                            // current moment in particular time zone.     .tolocaldate()                                                 // extract date-only value, losing time-of-day , time zone components.     .with( temporaladjusters.previousorsame( dayofweek.sunday ) )  // move day-of-week, or same date if desired day-of-week.     .atstartofday( zoneid.of( "asia/kolkata" ) )                   // determine first moment of day. *not* assume time-of-day 00:00:00 anomalies such daylight saving time (dst) may mean otherwise such 01:00:00.      .toinstant()                                                   // adjust utc, same moment, same point on timeline, viewed through lens of utc time zone.     .toepochmilli()                                                // extract count-from-epoch in milliseconds. *not* recommend tracking date-time way, question requires number. 

details

the answer gruodis good, here's alternative bit more direct , flexible.

get current moment zoneddatetime.

zoneid z = zoneid.of( "pacific/auckland" ) ; zoneddatetime = zoneddatetime.now( z ) ; 

temporaladjuster

the temporaladjuster interface lets manipulate date-time value fresh date-time value. temporaladjusters class (note plural s) provides several handy implementations. use dayofweek enum specify day consider first day of week.

dayofweek dowstartofweek = dayofweek.monday ;  localdate weekstartdate = now.tolocaldate().with( temporaladjusters.previousorsame( dayofweek.monday ) ) ; zoneddatetime start = weekstartdate.atstartofday( z ) ;  // determine first moment of day. note: *not* 00:00:00. 

see code run live @ ideone.com.

2017-08-21t00:00+12:00[pacific/auckland] 2017-08-21t08:44:46.439+12:00[pacific/auckland]

span of time

to report span of time, pou indeed extract count-from-epoch of whole seconds, if required.

long epochseconds = start.toepochsecond() ;  

or extract milliseconds via instant.

long epochmillis = start.toinstant().toepochmilli() ; 

but keep in mind both numbers truncate further fractional second, java.time types resolve nanoseconds.

besides truncation, there other reasons avoid tracking date-time count-from-epoch. since such values meaningless human eye, debugging more difficult , faulty data may escape notice. also, may assume epoch 1970-01-01t00:00:00z, there @ least another couple dozen epochs use common software systems. yet problem ambiguity on granularity of count, systems use whole seconds, others use milliseconds, others use microseconds, others nanoseconds, , still others use other resolutions.

interval

so instead of returning mere long integer numbers, suggest returning object. pair of instant objects work, used interval class in threeten-extra project. class has several handy methods expect calling code may find useful such contains, encloses, abuts, overlaps, span, isempty, , more.

org.threeten.extra.interval interval = interval.of( start.toinstant() , now.toinstant() ) ; 

you can apply time zone view either beginning or ending through lens of region’s own wall-clock time.

zoneddatetime zdtstart = interval.getstart().atzone( z );  // or `getend()`. 

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? -