Java Date vs. LocalDate

Sheeraz Gul Sep 16, 2022
Java Date vs. LocalDate

This tutorial demonstrates the difference between Date and LocalDate in Java.

Java Date vs. LocalDate

The Date class is from Java util package, and the LocalDate was added to the Time package of Java. Both classes are used for the date in Java; let’s see the main difference between Date and LocalDate in Java.

Date LocalDate
The java.util.Date is the core API for a date in Java from JDK 1.0. The java.time.LocalDate was introduced in the 1.8 version of Java.
The Date form util package also shows the time with the date. The LocalDate represents the date in ISO format yyyy-MM-dd without time. Mostly used to save dates like birthdays and paydays.
The Date was introduced in the 1.8 version of Java to provide immutability and thread safety. The LocalDate doesn’t support the immutability and thread safety.

Let’s try examples for both Date and LocalDate in Java.

the Date Class in Java

package delftstack;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Example {

	public static void main(String[] args) {

		//Create an Instant date and convert it to local date time.
		Instant Date_Instant = new Date().toInstant();
		LocalDateTime Demo_Date = LocalDateTime.ofInstant(Date_Instant,
						ZoneId.of(ZoneId.SHORT_IDS.get("PST")));
		System.out.println("The Current Date is:  "+Demo_Date);

		//Create an Instant calendar
		Instant Calendar_Instant = Calendar.getInstance().toInstant();
		System.out.println(Calendar_Instant);
		//Instance of zoneid to timezone
		ZoneId Default_TimeZone = TimeZone.getDefault().toZoneId();
		System.out.println(Default_TimeZone);

		//from specific Calendar
		ZonedDateTime Gregorian_Calendar_DateTime = new GregorianCalendar().toZonedDateTime();
		System.out.println(Gregorian_Calendar_DateTime);

		//Date API Legacy classes
		Date Date_Demo = Date.from(Instant.now());
		System.out.println(Date_Demo);

		TimeZone Time_Zone = TimeZone.getTimeZone(Default_TimeZone);
		System.out.println(Time_Zone);

		GregorianCalendar gc = GregorianCalendar.from(Gregorian_Calendar_DateTime);
		System.out.println(gc);

	}

}

The code above will use Java’s legacy Date API to show the current date and time with default and given timezones. See output:

The Current Date is:  2022-09-13T00:40:09.373
2022-09-13T07:40:09.490Z
Asia/Karachi
2022-09-13T12:40:09.545+05:00[Asia/Karachi]
Tue Sep 13 12:40:09 PKT 2022
sun.util.calendar.ZoneInfo[id="Asia/Karachi",offset=18000000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null]
java.util.GregorianCalendar[time=1663054809545,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Karachi",offset=18000000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=3,DAY_OF_MONTH=13,DAY_OF_YEAR=256,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=40,SECOND=9,MILLISECOND=545,ZONE_OFFSET=18000000,DST_OFFSET=0]

the LocalDate Class in Java

package delftstack;

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;


public class Example {

	public static void main(String[] args) {

		//The Current Date
		LocalDate Current_LocalDate = LocalDate.now();
		System.out.println("The Current Date is: "+Current_LocalDate);

		//The specific date Creation using LocalDate by providing input arguments
		LocalDate FirstLocalDate_2022 = LocalDate.of(2022, Month.JANUARY, 1);
		System.out.println("The Specific Date with inputs: "+FirstLocalDate_2022);


		//Trying to create a date by using Local date with invalid input
		LocalDate LocalDate_Karachi = LocalDate.now(ZoneId.of("Asia/Karachi"));
		System.out.println("The Current Date in Karachi is: "+LocalDate_Karachi);

		//Trying to Get the date from the base date (1970-10-28)
		LocalDate LocalDate_FromBase = LocalDate.ofEpochDay(300);
		System.out.println("300th day from base date: "+LocalDate_FromBase);

		LocalDate HundredDay_2022 = LocalDate.ofYearDay(2022, 100);
		System.out.println("100th day of 2022: "+HundredDay_2022);
	}

}

The code above uses LocalDate to get the current date, specific date, date from a particular timezone, and the date on a particular day of the year. See output:

The Current Date is: 2022-09-13
The Specific Date with inputs: 2022-01-01
The Current Date in Karachi is: 2022-09-13
300th day from base date: 1970-10-28
100th day of 2022: 2022-04-10

As we can see, the legacy Date API shows the full date with the current or given time, and it uses the instances of classes that provide more immutability and thread safety.

On the other hand, the LocalDate doesn’t show the time with the date, but the date can be manipulated using the LocalDate class from the time package of Java.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Date