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.
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: Date date0 = new Date(); JDK8 creating a date: LocalDate today = LocalDate.now(); Output examples:
JDK7 output: Wed Apr 13 13:19:06 CST 2022 JDK8 output: 2022-04-13 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):
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String str = sdf.format(date0);Formatting with DateTimeFormatter (JDK8):
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String jdk8Time = now.format(formatter);
System.out.println("JDK8 formatted time==" + jdk8Time);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
LocalDate localDate = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = localDate.format(fmt);
System.out.println("LocalDate to String: " + dateStr);Result: LocalDate to String: 2022/04/14 Date to LocalDateTime
Date date = new Date();
System.out.println("LocalDateTime(): " + date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime());Result: LocalDateTime(): 2022-04-14T10:07:52.868 Timestamp to LocalDateTime
long timestamp = System.currentTimeMillis();
Instant instant = Instant.ofEpochMilli(timestamp);
System.out.println("Timestamp to LocalDateTime: " +
LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));Result:
Timestamp to LocalDateTime: 2022-04-14T10:09:14.780Key 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
