Backend Development 9 min read

Master Java Date & Time: From JDK7 Date to JDK8 LocalDate/LocalDateTime

This article compares Java's legacy date handling in JDK7 with the modern java.time API introduced in JDK8, demonstrating how to create, format, and convert dates using LocalDate, LocalDateTime, DateTimeFormatter, and related methods, and provides a comprehensive table of useful LocalDate APIs.

macrozheng
macrozheng
macrozheng
Master Java Date & Time: From JDK7 Date to JDK8 LocalDate/LocalDateTime

When working with entity classes, timestamps such as creation time are often stored in databases using

datetime

or

date

types, but some legacy code still stores them as

varchar

. In JDK7, developers commonly used the older

Date

,

Calendar

, and

SimpleDateFormat

classes.

The

java.util.Date

class has long been criticized for its confusing design: it mixes date, time, and milliseconds, uses midnight to separate dates, counts months from zero, and provides unintuitive methods for extracting year, month, and day. Moreover, both

Date

and

SimpleDateFormat

are not type‑safe.

Java 8 introduced a new date‑time API that replaces these older classes.

JDK7 vs JDK8 Date Comparison

JDK7 creating a date:

<code>Date date0 = new Date();</code>

JDK8 creating a date:

<code>LocalDate today = LocalDate.now();</code>

Output examples:

JDK7 output:

<code>Wed Apr 13 13:19:06 CST 2022</code>

JDK8 output:

<code>2022-04-13</code>

Because the JDK7 output is less readable, developers often need to format it with

SimpleDateFormat

. The JDK8

LocalDate

prints directly in an ISO format, eliminating that extra step.

Formatting with

SimpleDateFormat

(JDK7):

<code>SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String str = sdf.format(date0);</code>

Formatting with

DateTimeFormatter

(JDK8):

<code>LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String jdk8Time = now.format(formatter);
System.out.println("JDK8 formatted time==" + jdk8Time);</code>

Both produce the same readable date, but the JDK8 code is shorter and the formatter is thread‑safe, unlike

SimpleDateFormat

, which must be created locally for each use.

Common JDK8 Date/Time Operations

String to LocalDate

<code>LocalDate localDate = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = localDate.format(fmt);
System.out.println("LocalDate to String: " + dateStr);</code>

Result:

<code>LocalDate to String: 2022/04/14</code>

Date to LocalDateTime

<code>Date date = new Date();
System.out.println("LocalDateTime(): " + date.toInstant()
    .atZone(ZoneId.systemDefault())
    .toLocalDateTime());</code>

Result:

<code>LocalDateTime(): 2022-04-14T10:07:52.868</code>

Timestamp to LocalDateTime

<code>long timestamp = System.currentTimeMillis();
Instant instant = Instant.ofEpochMilli(timestamp);
System.out.println("Timestamp to LocalDateTime: " +
    LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));</code>

Result:

<code>Timestamp to LocalDateTime: 2022-04-14T10:09:14.780</code>

Key LocalDate API Methods

getYear()

– returns the year as an

int
getMonth()

– returns the month as a

Month

enum

getMonthValue()

– returns the month number (1‑12)

getDayOfWeek()

– returns the day of week as

DayOfWeek
getDayOfMonth()

– returns the day of month as an

int
getDayOfYear()

– returns the day of year as an

int
withYear(int year)

– returns a copy with the specified year

withMonth(int month)

– returns a copy with the specified month

withDayOfMonth(int day)

– returns a copy with the specified day of month

isLeapYear()

– checks if the year is a leap year

lengthOfMonth()

– number of days in the month

lengthOfYear()

– 365 or 366 days

plusYears(long years)

,

plusMonths(long months)

,

plusWeeks(long weeks)

,

plusDays(long days)

– add time

minusYears(long years)

,

minusMonths(long months)

,

minusWeeks(long weeks)

,

minusDays(long days)

– subtract time

compareTo(ChronoLocalDate other)

– compare two dates

isBefore(ChronoLocalDate other)

– true if this date is before the other

isAfter(ChronoLocalDate other)

– true if this date is after the other

isEqual(ChronoLocalDate other)

– true if dates are equal

These APIs cover most daily development needs for handling dates in Java.

JavaBackend DevelopmentDateTimeJDK8java.time
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.