Several features of java1.8 (II.)

Source: Internet
Author: User
Tags time zones local time locale new set time and date

VII. Date API

Java 8 contains a new set of time-date APIs under package Java.time. The new date API is similar to the open source Joda-time library, but not exactly the same, the following example shows some of the most important parts of this new set of APIs:

1  Public classTest {2      Public Static voidMain (string[] args) {3         /**4 * (i) clock clocks5 * The Clock class provides a way to access the current date and time, clock is time zone sensitive and can be used to replace system.currenttimemillis () to obtain the current number of microseconds. 6 * A specific point in time can also be expressed using the instant class, and the instant class can also be used to create old Java.util.Date objects. 7          */8Clock Clock =Clock.systemdefaultzone ();9         LongMillis =Clock.millis ();TenSystem.out.println (millis+ ":" +System.currenttimemillis ()); OneInstant Instant =clock.instant (); ADate legacydate = Date.from (instant);//Legacy Java.util.Date - System.out.println (legacydate); -  the         /** - * (ii) timezones time zone - * The time zone is represented in the new API using ZoneID. The time zone can be easily obtained by using the static method of the.  - * Time zone defines the time difference to UTS, which is extremely important when converting between instant time objects to local date objects.  +          */ -System.out.println (Zoneid.getavailablezoneids ());//output all time zones +ZoneID zone1 = Zoneid.of ("Europe/berlin"); AZoneID zone2 = Zoneid.of ("Brazil/east"); atSystem.out.println (Zone1.getrules ());//output ZONERULES[CURRENTSTANDARDOFFSET=+01:00] -System.out.println (Zone2.getrules ());//output ZONERULES[CURRENTSTANDARDOFFSET=-03:00] -  -         /** - * (iii) LocalTime local time - * LocalTime defines a time with no time zone information, such as 10 o'clock in the evening, or 17:30:15.  in * The following example creates two local time using the time zone created by the preceding code. Then compare the time and calculate the two time difference in hours and minutes: -          */ toLocalTime Now1 =Localtime.now (zone1); +LocalTime Now2 =Localtime.now (zone2); -System.out.println (Now1.isbefore (now2));//false the         LongHoursbetween =ChronoUnit.HOURS.between (Now1, now2); *         LongMinutesbetween =ChronoUnit.MINUTES.between (Now1, now2); $System.out.println (Hoursbetween);//-2Panax NotoginsengSystem.out.println (Minutesbetween);//-179 -  the         /**LocalTime provides a variety of factory methods to simplify the creation of objects, including parsing time strings.  +          * A          */ theLocalTime late = Localtime.of (23, 59, 59); +System.out.println (late);//23:59:59 -DateTimeFormatter Germanformatter = $ DateTimeFormatter $ . Oflocalizedtime (Formatstyle.short) - . Withlocale (Locale.german); -LocalTime leettime = Localtime.parse ("13:37", germanformatter); theSystem.out.println (Leettime);//13:37 - Wuyi         /** the * (d) localdate Local dates - * Localdate represents an exact date, such as 2014-03-11. The object value is immutable and is basically consistent with localtime.  Wu * The following example shows how to add a day/month/year to a Date object. It is also important to note that these objects are immutable and that the operation returns always a new instance.  -          */ AboutLocaldate today =Localdate.now (); $Localdate tomorrow = Today.plus (1, chronounit.days); -Localdate yesterday = tomorrow.minusdays (2); - System.out.println (yesterday); -Localdate IndependenceDay = Localdate.of (Month.july, 4); ADayOfWeek DayOfWeek =Independenceday.getdayofweek (); +System.out.println (DayOfWeek);//FRIDAY the  -DateTimeFormatter GermanFormatter1 = $ DateTimeFormatter the . Oflocalizeddate (Formatstyle.medium) the . Withlocale (Locale.german); theLocaldate Xmas = localdate.parse ("24.12.2014", germanFormatter1); theSystem.out.println (xmas);//2014-12-24 -         //This is much more convenient than the date used before. in  the         /** the * (Fri) LocalDateTime local date and time About * LocalDateTime also represents the time and date, which is equivalent to merging the first two sections onto an object.  the * LocalDateTime and LocalTime and Localdate are immutable. LocalDateTime provides a number of ways to access specific fields.  the          */ theLocalDateTime sylvester = Localdatetime.of (Month.december, 31, 23, 59, 59); +DayOfWeek DayOfWeek1 =Sylvester.getdayofweek (); -System.out.println (DAYOFWEEK1);//Wednesday theMonth month =sylvester.getmonth ();BayiSYSTEM.OUT.PRINTLN (month);//December the         LongMinuteofday =Sylvester.getlong (chronofield.minute_of_day); theSystem.out.println (Minuteofday);//1439 -         /** - * As long as the time zone information is attached, it can be converted to a point-in-time instant object, instant Point-in-time objects can be easily converted to old-fashioned java.util.Date.  the          */ theInstant instant1 =Sylvester the . Atzone (Zoneid.systemdefault ()) the . Toinstant (); -Date legacyDate1 =Date.from (instant1); theSystem.out.println (LEGACYDATE1);//Wed Dec 23:59:59 CET the  the         /**94 * Format LocalDateTime and format time and date, in addition to using predefined formats, we can also define the format: the          */ theDateTimeFormatter formatter = the DateTimeFormatter98. Ofpattern ("MMM DD, yyyy-hh:mm"); AboutLocalDateTime parsed = Localdatetime.parse ("Nov 03, 2014-07:13", formatter); -String string =Formatter.format (parsed);101System.out.println (string);//2014-07:13, Geneva102 103         //Unlike Java.text.NumberFormat, the new version of DateTimeFormatter is immutable, so it is thread-safe. 104         //For more information about time-date formats:http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html the         //Java jdk1.8 The new offer of this time is still good at some point.106 107 108     }109  the 111  the}

It's okay. Use the new API or something, also good, learn more, think more

Several features of java1.8 (II.)

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.