Java Tutorials
New package called java.time added in java 8 has new Date, Time, DateTime classes i.e LocalDate,LocalTime,LocalDateTime classes respectively. These are an alternative classes for handling Date time related functionality in JDK8. Earlier versions of Java has Date & Calendar classes to deal with Date & Time functionaly.and it has some limitations called epoch time. New Classes has overcome those problems on Dates. And also It supports various calendars systems like Gregorian Calendar, Buddist Calendar, Japanese calendar Systems etc.,.
LocalDate maintains Year,Month,& Day only. It doesnt conatin time functions and TimeZones related information. It has additional functions for Adding,Substracting Dates,Comparing Dates etc., LocalDate uses ISO-8601 calendar system
LocalTime maintains only Time information. Hours,Minutes,Seconds,and Fraction of Seconds. It doesnt contain TimeZone Information.
LocalDateTime maintains Date and Time in Sinlge class. It doesnt contain TimeZone Information.
Other Classes in java.time package has
LocalDate represents only date, i.e year ,month,day
now()
method returns system's date.of
method returns specific date of your choice. Both are static methods.LocalDate date = LocalDate.now(); LocalDate date =LocalDate.of(2020,11,11); LocalDate date =LocalDate.of(2020,Month.OCTOBER,11); System.out.println(date); 2020-11-11
LocalDate Object has instance methods to get Year,Month and Day.
getYear(), getDayOfMonth(),date.getDayOfWeek(), getMonth(), getMonthValue()
plusDays, plusWeeks, plusMonths, plusYears
Add DaysLocalDate date =LocalDate,of(2010,11,11); System.out.println(date.plusDays(20);); 2011-12-01Add Months
LocalDate date =LocalDate,of(2010,11,11); System.out.println(date.plusMonths(12)); 2011-11-11Add Years
LocalDate date =LocalDate,of(2010,11,11); System.out.println(date.plusyears(21)); 2032-11-11 System.out.println(date.plusyears(-21)); 1990-11-11
LocalDate object implements Comparable
LocalDate date1 = LocalDate.of(2010,1,2); LocalDate date2 = LocalDate.of(1990,1,2); int value = date1.compareTo(date2); //20 date1 is greater than date2 int value = date2.compareTo(date1); //-20 date2 is less than date1
LocalDate objects can be compared with other LocalDate Object using following function, which returns boolean value.
LocalDate date1 = LocalDate.of(2010,1,2); LocalDate date2 = LocalDate.of(1990,1,2); date1.isEqual(date2); //false date1.isAfter(date2); //true date1.isBefore(date2); //false
LocalDate Object can be converted into a String, or String into a LocalDate Object using parse method.
Parse method has 2 variantsBoth are static methods, which can be accessed without using LocalDate Object instance.
String strDate = "1990-12-1"; LocalDate date = LocalDate.parse( strDate ); LocalDate Object is constructed. date.getYear() ; //1990 date.getMonthValue(); //12 date.getDayOfMonth(); //1
Use this method ,Date in any format.DateTimeFormatter class located in package java.time.format.DateTimeFormatter It is a general class to format Date and Time .
for ex: format of the Dates 12Aug2012 Aug122012 Feb1st2012 2022yearmonthOctDay1 to pase any of the above dates, programmer can use DateTimeFormatter class. String strDate = "12Aug2012"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy") LocalDate date = LocalDate.parse( strDate, formatter ); LocalDate Object is constructed. date.getYear() ; //2012 date.getMonthValue(); //8 date.getDayOfMonth(); //12 String strDate = "Feb1st2012"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMd'st'yyyy"); LocalDate date = LocalDate.parse( strDate, formatter ); //2012-02-01
Finding Months ,1st of the month falls on Sunday. In this example we Check every month's 1st is Sunday or Not.
LocalDate creates a Date for a specific year. datesUntil method generates stream of LocalDates from starting of the Year to end of the Year.
Filter condition checks is DayofMonth is 1 and DayOfWeek is SUNDAY, it returns TRUE only above condition is met, otherwise FALSE.
for YEAR 2023 , JANUARY and OCTOBER months Day 1st falls on SUNDAY
import java.time.*; import java.util.stream.*; LocalDate date = LocalDate.of(2023,1,1) Stream<LocalDate> dates = date.datesUntil(LocalDate.of(2023,12,2)) dates.filter(d ->{return d.getDayOfMonth()==1 && d.getDayOfWeek().equals(DayOfWeek.SUNDAY);}).forEach(System.out::println) 2023-01-01 2023-10-01
ADS