Welcome

Java Tutorials


Java 8 DateTime Classes

      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 Class

     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 Class

     LocalTime maintains only Time information. Hours,Minutes,Seconds,and Fraction of Seconds. It doesnt contain TimeZone Information.

LocalDateTime Class

     LocalDateTime maintains Date and Time in Sinlge class. It doesnt contain TimeZone Information.

Other Classes in java.time package has

  • Instance
  •    Instance: encapsulates Instance in Time

  • Duration
  •      Duration: encapsulates Length of Time,also expressed in nano seconds.

  • Period
  •      Period:encapsulates Length of Date

LocalDate class Usage

   LocalDate represents only date, i.e year ,month,day

Creating a LocalDate Object. LocalDate's

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
      

Extract Year, Month and Day from LocalDate

    LocalDate Object has instance methods to get Year,Month and Day.

     getYear(), getDayOfMonth(),date.getDayOfWeek(), getMonth(), getMonthValue()

      
      

Add days ,months ,years to LocalDate

    plusDays, plusWeeks, plusMonths, plusYears

Add Days
      		LocalDate date =LocalDate,of(2010,11,11);
      		System.out.println(date.plusDays(20););
      		2011-12-01
      
Add Months
      		LocalDate date =LocalDate,of(2010,11,11);
      		System.out.println(date.plusMonths(12));
      		2011-11-11
      
Add 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
      

Comparing LocalDate Objects

    LocalDate object implements Comparable interface. So date objects can be comparable with each other.

  • returns 0 means both dates are equal
  • -ve value date1 is less than date 2
  • +ve value date1 is greater than date2
	      	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.

  • isAfter
  • isBefore
  • isEqual
	      	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
      		
      

Parse LocalDate Object

      LocalDate Object can be converted into a String, or String into a LocalDate Object using parse method.

Parse method has 2 variants
  • parse(CharSequence text)
  • parse(CharSequence text, DateTimeFormatter formatter)

Both are static methods, which can be accessed without using LocalDate Object instance.

.parse(CharSequence text) example. Use this method only when date format is 'yyyy-mm-dd'

		String strDate = "1990-12-1";
		
		LocalDate date = LocalDate.parse( strDate );
		
		LocalDate Object  is constructed.
		
		date.getYear() ;   //1990
		
		date.getMonthValue();  //12
		
		date.getDayOfMonth();  //1
		
	


.parse(CharSequence text, DateTimeFormatter formatter) example.

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		
		
	

Example:Find a Month(s) ,Pay Day(1st day of the Month) falls on Sunday for a given Year

    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