Java8 new Date and time demo

Source: Internet
Author: User

 //LocalDateTime    Private Static voidtest1 () {//Initialize date and timeLocalDateTime Ldt1=localdatetime.of (2016,10,04,10,40,50); System.out.println (LDT1);//2016-10-04t10:40:50//get the current date and timeLocalDateTime ldt2=Localdatetime.now (); System.out.println (LDT2);//2017-11-04t11:02:57.721//Increased yearLocalDateTime Ldt3=ldt2.plusyears (2);//2019-11-04t11:02:57.721System.out.println (LDT3); //reduced minutesLocalDateTime Ldt4=ldt2.minusminutes (30);//2017-11-04t10:32:57.721System.out.println (LDT4); //get month day time secondsSystem.out.println ("Year:" +ldt2.getyear ());//Year:System.out.println ("Month:" +ldt2.getmonthvalue ());//Month: oneSystem.out.println ("Day:" +ldt2.getdayofmonth ());//Day: 4System.out.println ("Time:" +ldt2.gethour ());//Time: OneSystem.out.println ("Points:" +ldt2.getminute ());//points: 2System.out.println ("Seconds:" +ldt2.getsecond ());//seconds:}
LocalDateTime Use
//Instant: Timestamp (in Unix: January 1, 1970 0:0 0 seconds to a millisecond value of a specified time    Private Static voidtest2 () {Instant ins1=instant.now ();//get UTC time Zone by defaultSystem.out.println (INS1);//2017-11-04t03:23:45.442z//get East Zone eight time (Beijing time)Offsetdatetime odt= Ins1.atoffset (zoneoffset.ofhours (8));//2017-11-04t11:23:45.442+08:00System.out.println (ODT); //Get millisecond valueSystem.out.println (Ins1.toepochmilli ());//1509765825442//gets the seconds valueSystem.out.println (Odt.toepochsecond ());//1509765825Instant ins2=instant.ofepochmilli (60);//1970-01-01t00:00:00.060zSystem.out.println (INS2); }
Instant: Timestamp (in Unix: January 1, 1970 0:0 0 seconds to a millisecond value of a specified time
//Calculate time interval    Private Static voidtest3 () {//The first wayInstant ins1=Instant.now (); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } Instant ins2=Instant.now (); Duration Duration=Duration.between (INS1,INS2); System.out.println ("Millisecond of the interval:" +duration.tomillis ());//Millisecond of interval: 1031System.out.println ("Day of the interval:" +duration.todays ());//days at intervals: 0System.out.println ("Interval of Hours:" +duration.tohours ());//hours of interval: 0System.out.println ("Minute of the interval:" +duration.tominutes ());//interval of minutes: 0System.out.println ("Nanosecond of the interval:" +duration.tonanos ());//nanosecond of interval: 1031000000//The second wayLocalTime lt1=Localtime.now (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } localtime LT2=Localtime.now (); System.out.print (Duration.between (LT1,LT2). Tomillis ());//2000 Ms}
Calculate time interval
   Private Static voidtest4 () {localdate ld1=localdate.of (2015,1,1); Localdate ld2=Localdate.now (); System.out.println (LD2);//2017-11-04Period Period=Period.between (LD1,LD2); SYSTEM.OUT.PRINTLN (period);//P2y10m3dSystem.out.println (Period.getyears ());//2System.out.println (Period.getmonths ());//TenSystem.out.println (Period.getdays ());//3}
Calculate date interval

Java8 new Date and time demo

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.