The use of Localdate __java

Source: Internet
Author: User
Package date;


Import Java.time.DayOfWeek;
Import Java.time.LocalDate;
Import Java.time.temporal.ChronoUnit;
Import Java.time.temporal.TemporalAdjusters;




public class Localdatetest {


/**
*
* @param args
*/
public static void Main (string[] args) {


Current date
Localdate localdate = Localdate.now ();


Localdate localDate1 = localdate.of (2017, 11, 10);


Localdate LocalDate2 = localdate.of (2017, 11, 10);
The current year
System.out.println (Localdate.getyear ());
The current month
System.out.println (Localdate.getmonth ());
The current month
System.out.println (Localdate.getmonthvalue ());
The current date is the day of the week
System.out.println (Localdate.getdayofweek ());
The current number of days in a year is the first day
System.out.println (Localdate.getdayofyear ());
The current date is the day ordinal of the month
System.out.println (Localdate.getdayofmonth ());
Number of days per month
System.out.println (Localdate.lengthofmonth ());
How many days are there in this year
System.out.println (Localdate.lengthofyear ());
Whether it is a leap year
System.out.println (Localdate.isleapyear ());
Current date minus one months
System.out.println (localdate.minusmonths (1));
Current date minus one year
System.out.println (Localdate.minusyears (1));
Current date minus one weeks
System.out.println (Localdate.minusweeks (1));
System.out.println (localdate.min);


System.out.println (Localdate.max);
The judgment of time is judged successively
System.out.println (Localdate.isbefore (localdate.minusmonths (1)));
System.out.println (Localdate.isbefore (localdate.plusmonths (1)));
System.out.println (Localdate.isafter (localDate1));


Modifies the specified local date, gets the new object, and specifies a time change on the original object
Localdate localdate1 = localdate.withdayofmonth (1);
Localdate Localdate2 = localdate.withdayofyear (1);
Localdate localdate3 = localdate.withmonth (1);
Localdate localdate4 = localdate.withyear (2018);


System.out.println (LOCALDATE1);
System.out.println (LOCALDATE2);
System.out.println (Localdate3);
System.out.println (LOCALDATE4);


Localdate today = Localdate.now ();
Take the 1th day of the Month:
Localdate firstdayofthismonth = Today.with (Temporaladjusters.firstdayofmonth ());
Take the 2nd day of the Month:
Localdate seconddayofthismonth = Today.withdayofmonth (2);
Take the last day of this month, no more calculation is 28,29,30 or 31:
Localdate lastdayofthismonth = Today.with (Temporaladjusters.lastdayofmonth ());
Take off one day:
Localdate firstDayOf2015 = lastdayofthismonth.plusdays (1);
Take the first Monday of January 2017
Localdate firstMondayOf2015 = Localdate.parse ("2017-01-01"). With (
Temporaladjusters.firstinmonth (Dayofweek.monday));
Compare two time equals
System.out.println (Localdate1.equals (LocalDate2));
System.out.println (Localdate1.equals (Localdate2));


Compares the size of two times, returns the difference between the days of the year, the month, and the day
System.out.println (Localdate1.compareto (LocalDate2));
System.out.println (Localdate1.compareto (Localdate2));


Two days, years, months, etc.
Long days = Localdate2.until (Localdate4, chronounit.days);
Long hour = Localdate2.until (Localdate4, chronounit.years);
System.out.println (days);
System.out.println (hour);


}


}

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.