Java system time, how to calculate how much time is left from now to the wee hours?

Source: Internet
Author: User

Apache Commons-lang3

long millisecondslefttoday = 86400000-     dateutils.getfragmentinmilliseconds (Calendar.getinstance ( ), calendar.date);

System.currenttimemillis () is a timestamp of 0 time zones, so you have to add 8 hours.
The most basic wording:

 Public classMain { Public Static voidMain (string[] args) {Longhour = 60 * 60 * 1000; LongTime =System.currenttimemillis ();        System.out.println (time); time+ = 8 *hour;        System.out.println (time); time%= 24 *hour; System.out.printf ("%02d:%02d\n", Time/hour, time% hour/60000); time= * hour-Time ; System.out.printf ("%02d:%02d\n", Time/hour, time% hour/60000); }}

In the current time zone has daylight saving time, another one with System.currenttimemillis () the answer is not handled correctly, such as 15 U.S. daylight savings date from March 8 2 o'clock in the morning, if it is March 8 1 o'clock, that way calculated is wrong, The calendar API can be handled correctly, although it is difficult to use.

Calendar C = calendar.getinstance (); long now =10000); long Millis = C.gettimeinmillis ()-now;

The wording in Java8

LocalTime midnight == = = Todaymidnight.plusdays (1); System.out.println (TimeUnit.NANOSECONDS.toSeconds (Duration.between (Localdatetime.now (), tomorrowmidnight). Tonanos ()));

https://www.zhihu.com/question/28475461



Java system time, how to calculate how much time is left from now to the wee hours?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.