Java8 Time Usage Summary

Source: Internet
Author: User

Localdate represents a date for an iOS format (YYYY-MM-DD) Gets the current date: localdate localdate = Localdate.now ();//localdate: Indicates a date without a time zone. Localdate is immutable and thread-safe System.out.println ("localdate:" + localdate);//localdate:2017-08-24//localdate can specify a specific date, Call the of or Parse method to return the instance: System.out.println (Localdate.of, ());//2017-07-20system.out.println (Localdate.parse ( "2017-07-20");//2017-07-20//add a day for today, that is to get tomorrow System.out.println (Localdate.now (). Plusdays (1));//2017-08-25// Add 7 days for today, that is, to get a week after the Today System.out.println (Localdate.now (). Plusweeks (1));//2017-08-31//adds one months to today, That is to get one months later today System.out.println (Localdate.now (). Plusmonths (1));//2017-09-24//adds a year to today, That is to get a year later today System.out.println (Localdate.now (). Plusyears (1));//2018-08-24//minus one months from today (plus the same can be obtained minus 1 days, a week, 1 years after the time) System.out.println (Localdate.now (). Minus (1, chronounit.months));//2017-07-24system.out.println (Localdate.now (). Minusmonths (1));//2017-07-24//resolution date 2017-07-20, gets the week of the week and the day of the month, etc. System.out.println ("Thursday:" +localdate.parse (" 2017-07-20 "). GetDayOfWeek ());//thursdaysystem.out.println("Day:" +localdate.parse ("2017-07-20"). GetDayOfMonth ());//20system.out.println ("Month:" +localdate.parse ("2017-07-20") . Getmonthvalue ());//7system.out.println ("Year:" +localdate.parse ("2017-07-20"). GetYear ());//2017system.out.println ("Month:" +localdate.parse ("2017-07-20"). GetMonth ());//july//is not a leap year (leap years judgment) System.out.println (Localdate.now (). Isleapyear ());//false//determines whether before or after the date: System.out.println (Localdate.parse ("2017-07-20"). Isafter (Localdate.parse (" 2017-07-21 "));//falsesystem.out.println (Localdate.parse (" 2017-07-20 "). Isbefore (Localdate.parse (" 2017-07-21 ")) );//truesystem.out.println (Localdate.parse ("2217-07-20"). Isbefore (Localdate.parse ("2017-07-21"));//false// Get the first day of the Month: System.out.println (Localdate.parse ("2017-07-20"). With (Temporaladjusters.firstdayofmonth ()));// 2017-07-01//get the first day of next month, etc... System.out.println (Localdate.parse ("2017-07-20"). With (Temporaladjusters.firstdayofnextmonth ()));// 2017-08-01system.out.println (Localdate.parse ("2017-07-20"). With (Temporaladjusters.lastdayofmonth ()));// 2017-07-31System.out.println (Localdate.parse ("2017-07-20"). With (Temporaladjusters.firstdayofnextyear ()));// 2018-01-01system.out.println (Localdate.parse ("2017-07-20"). With (Temporaladjusters.firstdayofyear ()));// 2017-01-01//judge whether today is my birthday, for example my birthday is 2006-07-20localdate birthday = Localdate.of (2006, 07, 20); MonthDay Birthdaymd = Monthday.of (Birthday.getmonth (), Birthday.getdayofmonth ()); MonthDay Today=monthday.from (Localdate.now ()); System.out.println ("Whether today is my birthday:" + today.equals (BIRTHDAYMD));//falsesystem.out.println ("=======localtime represents a time, Instead of the date =========== ");//Get the current time 09:52:19.446system.out.println (" Present Time: "+localtime.now ());// Resolves a string time to LocalTimeSystem.out.println (Localtime.parse ("15:02")), and//15:02//creates a time System.out.println using the static method ( Localtime.of (16, 02));//16:02//uses parsing strings and adds an hour, output 16:02system.out.println (Localtime.parse ("15:02"). Plus (1, chronounit.hours));//16:02system.out.println (Localtime.parse ("15:02"). Plus (chronounit.minutes));//15:12// Gets the hour, Minute System.out.println (Localtime.parse ("15:02") of the time. GethOur ());//15system.out.println (Localtime.parse ("15:02"). Getminute ());//2//Check if one time is before and after another time System.out.println (Localtime.parse ("15:02"). Isafter (Localtime.parse ("14:02"));//truesystem.out.println (Localtime.parse ("15:02") . Isbefore (Localtime.parse ("14:02")))//false//the start and end times of each day System.out.println (Localtime.max);// 23:59:59.999999999system.out.println (localtime.min);//00:00system.out.println ("=======localdatetime is used to represent the date and time , which is one of the most commonly used classes of =========== "); System.out.println ("Now:" +localdatetime.now ());//2017-08-24t10:31:03.600// LocalDateTime also provides APIs for adding or subtracting dates and times System.out.println ("Tomorrow's Now:" +localdatetime.now (). Plusdays (1));// 2017-08-25t10:33:05.363//get the current month and so on System.out.println (Localdatetime.now (). Getmonthvalue ());//8system.out.println (Localdatetime.now (). GetMonth ());//augustsystem.out.println (Localdatetime.now (). GetYear ());//2017// Date format System.out.println ("====== date format ======="); LocalDateTime now = Localdatetime.now ();D atetimeformatter datetimeformatter = Datetimeformatter.ofpattern (" YYYY-MM-DD hh:mM:ss "); SYSTEM.OUT.PRINTLN ("Default format:" + now); SYSTEM.OUT.PRINTLN ("Custom format:" + Now.format (datetimeformatter)); LocalDateTime LocalDateTime = Localdatetime.parse ("2017-07-20 15:27:44", datetimeformatter); System.out.println ("String goto localDateTime:" + Localdatetime.format (datetimeformatter));D Atetimeformatter datetimeformatter_s = Datetimeformatter.ofpattern ("Yyyy-mm-dd"); String datestring = Datetimeformatter_s.format (Localdate.now ()); System.out.println ("Date to String:" + datestring);//The date and period period class is used to modify the difference between a given date or two dates obtained. Localdate initialdate = Localdate.parse ("2017-07-20"); Localdate finaldate = Initialdate.plus (Period.ofdays (5));//Add 5 days to the initialized date System.out.println ("Initialize Date:" + initialdate);// 2017-07-20system.out.println ("Add date after:" + finaldate),//2017-07-25//period (Period) API provides us with a comparison of two dates, as follows to obtain the difference in days: ( Similarly can get two time difference of hours, weeks, months, years) long between = ChronoUnit.DAYS.between (Initialdate, finaldate); System.out.println ("Gap days:" + between);//5//how to convert the date class to a time class in Java8//date and Instant convert to and from date date = Date.from (Instant.now ()); InstanT instant = date.toinstant ();//date converted to localdatetime/*localdatetime localdatetime_s = Localdatetime.from (new date ()); System.out.println (localdatetime_s); *///localdatetime to Datedate date_s =date.from (Localdatetime.atzone ( Zoneid.systemdefault ()). Toinstant ()); System.out.println (date_s);//localdate to Datedate date_t =date.from (Localdate.now (). Atstartofday (). Atzone ( Zoneid.systemdefault ()). Toinstant ()); System.out.println (date_t);

  

Java8 Time Usage Summary

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.