Java Util Date is Past, Now is a new Era, Java Time is a Future, my teacher say like that
Java 8 come with the new Api for Date. LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value “2nd October 2007” can be stored in a LocalDate.
Let’s see this code, how to use LocalDate
import java.time.LocalDate; import java.time.Month; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; public class LocalDateMain { @SuppressWarnings("deprecation") public static void main(String[] args) { LocalDate localDate=LocalDate.now(); LocalDate ld=LocalDate.now().minusDays(4); System.out.println("Local date\t\t\t: "+localDate); System.out.println("Local Date Minus 4 Days\t\t: "+ld); System.out.println("Chronology\t\t\t: "+localDate.getChronology()); System.out.println("Day of Month\t\t\t: "+localDate.getDayOfMonth()); System.out.println("Day of Week\t\t\t: "+localDate.getDayOfWeek()); System.out.println("Day of Year\t\t\t: "+localDate.getDayOfYear()); System.out.println("Era\t\t\t\t: "+localDate.getEra()); System.out.println("Month\t\t\t\t: "+localDate.getMonth()); System.out.println("Month Value\t\t\t: "+localDate.getMonthValue()); System.out.println("Year\t\t\t\t: "+localDate.getYear()); System.out.println(localDate+" Is After "+ld +"\t: "+localDate.isAfter(ld)); System.out.println(localDate+" Is Before "+ld +"\t: "+localDate.isBefore(ld)); System.out.println(localDate+" Is Equals "+ld+"\t: "+localDate.equals(ld)); System.out.println("Is Leap Year\t\t\t: "+localDate.isLeapYear()); System.out.println("Now :"+ LocalDate.now()+" Is Leaf Year : 2016-02-02 :"+LocalDate.of(2016, 02, 02).isLeapYear()); System.out.println("Is Supported TemporalField \t: "+localDate.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH)); System.out.println("Is Supported TemporalUnit \t: "+localDate.isSupported(ChronoUnit.CENTURIES)); System.out.println("Length of Month ("+localDate+")\t: "+localDate.lengthOfMonth()); System.out.println("Length of Year ("+localDate+")\t: "+localDate.lengthOfYear()); System.out.println(localDate+" Minus one Day\t: "+localDate.minus(1, ChronoUnit.DAYS)); System.out.println(localDate+" Minus one Day\t: "+localDate.minusDays(1)+"\t//Using minusDays(long)"); System.out.println(localDate+" Minus One Month\t: "+localDate.minus(1,ChronoUnit.MONTHS)); System.out.println(localDate+" Minus One Months\t: "+localDate.minusMonths(1)+"\t//Using minusMonths(long)"); System.out.println(localDate+" Minus One Year\t: "+localDate.minus(1,ChronoUnit.YEARS)); System.out.println(localDate+" Minus One Year\t: "+localDate.minusYears(1)+"\t//Using minusYears(long)"); System.out.println(localDate+" Minus One Weeks\t: "+localDate.minus(1,ChronoUnit.WEEKS)); System.out.println(localDate+" Minus One Weeks\t: "+localDate.minusWeeks(1)+"\t//Using minusWeeks(long)"); System.out.println("LocalDate.of(int Year,int Month,int Days)\t: "+LocalDate.of(2015, 02, 02)); System.out.println("LocalDate.of(int Year,Month Month,int Days)\t: "+LocalDate.of(2015, Month.FEBRUARY, 02)); System.out.println("LocalDate of Year Day \t\t: "+LocalDate.ofYearDay(2015, localDate.getDayOfYear())); System.out.println("LocalDate.parse(CharSequence)\t\t: "+LocalDate.parse("2015-03-30")+"\t //By Default FormatDate yyyy-MM-dd"); System.out.println("LocalDate.parse(CharSequence,DateFormatter)\t: "+LocalDate.parse("30-Mar-2015",DateTimeFormatter.ofPattern("dd-MMM-yyyy"))+"\t//Parse using DateFormater"); System.out.println(localDate+" Plus one Day\t\t: "+localDate.plus(1, ChronoUnit.DAYS)); System.out.println(localDate+" Plus one Day\t\t: "+localDate.plusDays(1)+"\t//Using minusDays(long)"); System.out.println(localDate+" Plus One Month\t: "+localDate.plus(1,ChronoUnit.MONTHS)); System.out.println(localDate+" Plus One Months\t: "+localDate.plusMonths(1)+"\t//Using plusMonths(long)"); System.out.println(localDate+" Plus One Year\t: "+localDate.plus(1,ChronoUnit.YEARS)); System.out.println(localDate+" Plus One Year\t: "+localDate.plusYears(1)+"\t//Using plusYears(long)"); System.out.println(localDate+" Plus One Weeks\t: "+localDate.plus(1,ChronoUnit.WEEKS)); System.out.println(localDate+" Plus One Weeks\t: "+localDate.plusWeeks(1)+"\t//Using plusWeeks(long)"); //LocalDate.with System.out.println("Local Date With(TemporalField,new_value=21)"); System.out.println(localDate+" With(ChronoField.DAY_OF_MONTH,21) : "+localDate.with(ChronoField.DAY_OF_MONTH, 21)); System.out.println(localDate+" With(ChronoField.DAY_OF_YEAR,21) : "+localDate.with(ChronoField.DAY_OF_YEAR, 21)); System.out.println(localDate+" With(ChronoField.DAY_OF_WEEK,21) : "+localDate.with(ChronoField.DAY_OF_WEEK, 1)); } }
And What we get is
Local date : 2015-02-02 Local Date Minus 4 Days : 2015-01-29 Chronology : ISO Day of Month : 2 Day of Week : MONDAY Day of Year : 33 Era : CE Month : FEBRUARY Month Value : 2 Year : 2015 2015-02-02 Is After 2015-01-29 : true 2015-02-02 Is Before 2015-01-29 : false 2015-02-02 Is Equals 2015-01-29 : false Is Leap Year : false Now :2015-02-02 Is Leaf Year : 2016-02-02 :true Is Supported TemporalField : true Is Supported TemporalUnit : true Length of Month (2015-02-02) : 28 Length of Year (2015-02-02) : 365 2015-02-02 Minus one Day : 2015-02-01 2015-02-02 Minus one Day : 2015-02-01 //Using minusDays(long) 2015-02-02 Minus One Month : 2015-01-02 2015-02-02 Minus One Months : 2015-01-02 //Using minusMonths(long) 2015-02-02 Minus One Year : 2014-02-02 2015-02-02 Minus One Year : 2014-02-02 //Using minusYears(long) 2015-02-02 Minus One Weeks : 2015-01-26 2015-02-02 Minus One Weeks : 2015-01-26 //Using minusWeeks(long) LocalDate.of(int Year,int Month,int Days) : 2015-02-02 LocalDate.of(int Year,Month Month,int Days) : 2015-02-02 LocalDate of Year Day : 2015-02-02 LocalDate.parse(CharSequence) : 2015-03-30 //By Default FormatDate yyyy-MM-dd LocalDate.parse(CharSequence,DateFormatter) : 2015-03-30 //Parse using DateFormater 2015-02-02 Plus one Day : 2015-02-03 2015-02-02 Plus one Day : 2015-02-03 //Using minusDays(long) 2015-02-02 Plus One Month : 2015-03-02 2015-02-02 Plus One Months : 2015-03-02 //Using plusMonths(long) 2015-02-02 Plus One Year : 2016-02-02 2015-02-02 Plus One Year : 2016-02-02 //Using plusYears(long) 2015-02-02 Plus One Weeks : 2015-02-09 2015-02-02 Plus One Weeks : 2015-02-09 //Using plusWeeks(long) Local Date With(TemporalField,new_value=21) 2015-02-02 With(ChronoField.DAY_OF_MONTH,21) : 2015-02-21 2015-02-02 With(ChronoField.DAY_OF_YEAR,21) : 2015-01-21 2015-02-02 With(ChronoField.DAY_OF_WEEK,21) : 2015-02-02