datetime - How to get milliseconds from LocalDateTime in Java 8 -


i wondering if there way current milliseconds since 1-1-1970 (epoch) using new localdate, localtime or localdatetime classes of java 8.

the known way below:

long currentmilliseconds = new date().gettime(); 

or

long currentmilliseconds = system.currenttimemillis(); 

i'm not entirely sure mean "current milliseconds" i'll assume it's number of milliseconds since "epoch," namely midnight, january 1, 1970 utc.

if want find number of milliseconds since epoch right now, use system.currenttimemillis() anubian noob has pointed out. if so, there's no reason use of new java.time apis this.

however, maybe have localdatetime or similar object somewhere , want convert milliseconds since epoch. it's not possible directly, since localdatetime family of objects has no notion of time zone they're in. time zone information needs supplied find time relative epoch, in utc.

suppose have localdatetime this:

localdatetime ldt = localdatetime.of(2014, 5, 29, 18, 41, 16); 

you need apply time zone information, giving zoneddatetime. i'm in same time zone los angeles, i'd this:

zoneddatetime zdt = ldt.atzone(zoneid.of("america/los_angeles")); 

of course, makes assumptions time zone. , there edge cases can occur, example, if local time happens name time near daylight saving time (summer time) transition. let's set these aside, should aware these cases exist.

anyway, if can valid zoneddatetime, can convert number of milliseconds since epoch, so:

long millis = zdt.toinstant().toepochmilli(); 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -