# LocalDate, LocalTime, LocalDateTime

In 
Time
Date
Published 2022-12-03

This tutorial explains to you how to use LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Duration and Period classes in Java.

Starting from Java 8 we can use the java.time package that will streamline the process of working with time in Java.

Please take a look at the following example and read carefully the comments. The code is self-explanatory.

When the base application was downloaded from Spring Initializr, I updated the main class as below:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.time.*;
import java.time.temporal.ChronoField;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {

		SpringApplication.run(DemoApplication.class, args);

		//Work with current Date
		LocalDate today = LocalDate.now();
		System.out.println("Current Date="+today);

		//Work with a specific date
		LocalDate specificDate = LocalDate.of(2023, Month.JANUARY, 20);
		System.out.println("Specific Date = "+specificDate);

		//Get the year from the date
		int year1 = today.getYear();
		int year2 = today.get(ChronoField.YEAR);
		System.out.println("year1="+year1+" / year2="+year2);

		//Work with current Time
		LocalTime currentTime = LocalTime.now();
		System.out.println("Current Time = "+currentTime);

		//Work with a specific time
		LocalTime specificTime1 = LocalTime.of(11,23,11);
		System.out.println("Specific Time of Day #1="+specificTime1);

		LocalTime specificTime2 = LocalTime.parse("15:00:22");
		System.out.println("Specific Time of Day #2="+specificTime2);

		//Work with LocalDateTime class
		LocalDateTime dt1 = LocalDateTime.now();
		System.out.println("dt1="+dt1);

		LocalDateTime dt2 = LocalDateTime.of(today, specificTime1);
		System.out.println("dt2="+dt2);

		//Working with Duration class
		Duration d1 = Duration.between(dt1, dt2);
		Duration d2 = Duration.between(specificTime1, specificTime2);
		System.out.println("d1="+d1.getSeconds());
		System.out.println("d2="+d2);

		//Working with Period class
		Period p1 = Period.between(today, specificDate);
		System.out.println("p1="+p1);
		System.out.println("p1.getDays()="+p1.getDays());
		
	}
}

When I run the code, I get :

Current Date=2023-06-07
Specific Date = 2023-01-20
year1=2023 / year2=2023
Current Time = 21:41:45.504494500
Specific Time of Day #1=11:23:11
Specific Time of Day #2=15:00:22
dt1=2023-06-07T21:41:45.506495500
dt2=2023-06-07T11:23:11
d1=-37115
d2=PT3H37M11S
p1=P-4M-18D
p1.getDays()=-18

Process finished with exit code 0

The following example could be helpful as well :

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;

@SpringBootApplication
public class Demo1Application {

  public static void main(String[] args) {
    SpringApplication.run(Demo1Application.class, args);

    //ZonedDateTime
    ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
    System.out.println("currentZonedDateTime = " + currentZonedDateTime);

    //OffsetDateTime
    OffsetDateTime currentOffsetDateTime1 = currentZonedDateTime.toOffsetDateTime();
    OffsetDateTime currentOffsetDateTime2 = OffsetDateTime.now();
    System.out.println("currentOffsetDateTime1 = " + currentOffsetDateTime1);
    System.out.println("currentOffsetDateTime2 = " + currentOffsetDateTime2);

    //ZoneOffset
    ZoneOffset offset = OffsetDateTime.now().getOffset();
    System.out.println("offset = " + offset);
    System.out.println("---------------------------");

    //DateTimeFormatter
    DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm:ss Z");
    System.out.println("Formated Date = " + df.format(currentZonedDateTime));

    //ChronoField
    System.out.println("ChronoField/MONTH_OF_YEAR = " + currentZonedDateTime.get(ChronoField.MONTH_OF_YEAR));
    System.out.println("ChronoField/SECOND_OF_DAY = " + currentZonedDateTime.get(ChronoField.SECOND_OF_DAY));
  }
}

When we run this code we will receive something like this:

currentZonedDateTime = 2024-01-08T21:43:48.693021400+02:00[Europe/Bucharest]
currentOffsetDateTime1 = 2024-01-08T21:43:48.693021400+02:00
currentOffsetDateTime2 = 2024-01-08T21:43:48.694021700+02:00
offset = +02:00
---------------------------
Formated Date = 08/01/2024 09:43:48 +0200
ChronoField/MONTH_OF_YEAR = 1
ChronoField/SECOND_OF_DAY = 78228