Jackson Java 8 date/time type `java.time.LocalDate` not supported by default

This article shows how Jackson can support the Java 8 date time APIs or JSR-310. Table of contents: 1. Download Jackson 2. Jackson does not support Java 8 date time APIs 3. Make Jackson support Java 8 date time APIs or JSR310 3.1 jackson-datatype-jsr310 3.1 Register JavaTimeModule() 4. Download Source Code 5. References P.S Tested …

Read more

How to format FileTime in Java

In Java, we can use DateTimeFormatter to convert the FileTime to other custom date formats. public static String formatDateTime(FileTime fileTime) { LocalDateTime localDateTime = fileTime .toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); return localDateTime.format( DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss")); } 1. File Last Modified Time This example displays the last modified time of a file in a custom date format. GetLastModifiedTime.java package …

Read more

Java – Check if the date is older than 30 days or 6 months

This article shows Java 8 and legacy date-time APIs example to check if a date is 30 days or 6 months older than the current date. 1. Java 8 isBefore() 2. Java 8 ChronoUnit.{UNIT}.between() 3. Java 8 Period.between() 4. Legacy Calendar and Date 5. References 1. Java 8 isBefore() First minus the current date and …

Read more

Java 8 – How to calculate days between two dates?

In Java 8, we can use ChronoUnit.DAYS.between(from, to) to calculate days between two dates. 1. LocalDate JavaBetweenDays1.java package com.mkyong.java8; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class JavaBetweenDays1 { public static void main(String[] args) { LocalDate from = LocalDate.now(); LocalDate to = from.plusDays(10); long result = ChronoUnit.DAYS.between(from, to); System.out.println(result); // 10 } } Output 10 2. LocalDateTime …

Read more

Java 8 – Difference between two LocalDate or LocalDateTime

In Java 8, we can use Period, Duration or ChronoUnit to calculate the difference between two LocalDate or LocaldateTime. Period to calculate the difference between two LocalDate. Duration to calculate the difference between two LocalDateTime. ChronoUnit for everything. 1. Period JavaLocalDate.java package com.mkyong.java8; import java.time.LocalDate; import java.time.Period; public class JavaLocalDate { public static void main(String[] …

Read more

Java 8 – Convert Epoch time milliseconds to LocalDate or LocalDateTime

In Java 8, we can use Instant.ofEpochMilli().atZone() to convert the epoch time in milliseconds back to LocalDate or LocalDateTime Epoch time to LocalDate LocalDate ld = Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate(); Epoch time to LocalDateTime LocalDateTime ldt = Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDateTime(); P.S Epoch time is the number of seconds that have elapsed since 0:00:00 UTC on 1 January 1970 1. Epoch …

Read more

Java 8 – Convert LocalDate and LocalDateTime to Date

A Java example to convert Java 8 java.time.LocalDate and java.time.LocalDateTime back to the classic java.uti.Date. JavaDateExample.java package com.mkyong.time; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public class JavaDateExample { public static void main(String[] args) { // LocalDate -> Date LocalDate localDate = LocalDate.of(2020, 2, 20); Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); // LocalDateTime -> …

Read more

Java 8 – How to parse date with "dd MMM" (02 Jan), without year?

This example shows you how to parse a date (02 Jan) without a year specified. JavaDateExample.java package com.mkyong.time; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class JavaDateExample { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM", Locale.US); String date = "02 Jan"; LocalDate localDate = LocalDate.parse(date, formatter); System.out.println(localDate); System.out.println(formatter.format(localDate)); } } Output …

Read more

Java 8 – Unable to obtain LocalDateTime from TemporalAccessor

An example of converting a String to LocalDateTime, but it prompts the following errors: Java8Example.java package com.mkyong.demo; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; public class Java8Example { public static void main(String[] args) { String str = "31-Aug-2020"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US); LocalDateTime localDateTime = LocalDateTime.parse(str, dtf); System.out.println(localDateTime); } } Output Exception in thread "main" …

Read more

Java 8 – How to parse date with LocalDateTime

Here are a few Java 8 examples to parse date with LocalDateTime. First, find the DateTimeFormatter pattern that matches the date format, for example: String str = "2020-01-30 12:30:41"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); second, parse it with LocalDateTime.parse DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String str = "2020-01-30 12:30:41"; LocalDateTime localDateTime = LocalDateTime.parse(str, dtf); 1. …

Read more

Java 8 – Convert ZonedDateTime to Timestamp

Java example to convert java.time.ZonedDateTime to java.sql.Timestamp and vice verse. 1. ZonedDateTime -> Timestamp TimeExample1.java package com.mkyong.jdbc; import java.sql.Timestamp; import java.time.ZonedDateTime; public class TimeExample1 { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(); // 1. ZonedDateTime to TimeStamp Timestamp timestamp = Timestamp.valueOf(now.toLocalDateTime()); // 2. ZonedDateTime to TimeStamp , no different Timestamp timestamp2 = …

Read more

Java 8 – Convert LocalDateTime to Timestamp

In Java, we can use Timestamp.valueOf(LocalDateTime) to convert a LocalDateTime into a Timestamp. 1. LocalDateTime Timestamp Java example to convert java.time.LocalDateTime to java.sql.Timestamp and vice verse. TimeExample.java package com.mkyong; import java.sql.Timestamp; import java.time.LocalDateTime; public class TimeExample { public static void main(String[] args) { // LocalDateTime to Timestamp LocalDateTime now = LocalDateTime.now(); Timestamp timestamp = Timestamp.valueOf(now); …

Read more

Spring Boot + Spring Data JPA + Java 8 date and time (JSR310)

In Spring Boot + Spring Data JPA application, to support the JSR310 java.time.* APIs, we need to register this Jsr310JpaConverters manually. import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters; @EntityScan( basePackageClasses = {Application.class, Jsr310JpaConverters.class} ) @SpringBootApplication public class Application { //… } P.S Tested with Spring Boot 1.5.1.RELEASE, Spring Data JPA 1.11.0.RELEASE 1. Full Example 1.1 A model contains …

Read more

Java Date Time Tutorials

A collection of Java date and time examples. 1. Java Date Time APIs In old days, we use the following classic Date and Calendar APIs to represent and manipulate date. java.util.Date – date and time, print with default time-zone. java.util.Calendar – date and time, more methods to manipulate date. java.text.SimpleDateFormat – formatting (date -> text), …

Read more

Java 8 – TemporalAdjusters examples

In Java 8, you can use the predefined java.time.temporal.TemporalAdjusters to adjust a date or Temporal 1. TemporalAdjusters Example to move a date to firstDayOfMonth, firstDayOfNextMonth, next Monday and etc. TestDate.java package com.mkyong.time; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class TestDate { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("current date : …

Read more

Java – How to add days to current date

This article shows you how to add days to the current date, using the classic java.util.Calendar and the new Java 8 date and time APIs. 1. Calendar.add Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current date. DateExample.java package com.mkyong.time; import java.text.DateFormat; import java.text.SimpleDateFormat; import …

Read more

Java 8 – Period and Duration examples

Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the difference between dates. Duration – Measures time in seconds and nanoseconds. Period – Measures time in years, months and days. 1. Duration Example A java.time.Duration example to find out difference seconds between two LocalDateTime DurationExample.java package …

Read more

Java 8 – How to format LocalDateTime

Few examples to show you how to format java.time.LocalDateTime in Java 8. 1. LocalDateTime + DateTimeFormatter To format a LocalDateTime object, uses DateTimeFormatter TestDate1.java package com.mkyong.time; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class TestDate1 { public static void main(String[] args) { //Get current date time LocalDateTime now = LocalDateTime.now(); System.out.println("Before : " + now); DateTimeFormatter formatter …

Read more

Java 8 – HijrahDate, How to calculate the Ramadan date

Ramadan is the 9th month of the Islamic calendar, the entire month. 1. HijrahDate -> Ramadan 2016 Full example to calculate the start and end of the Ramadan 2016 TestHijrahDate.java package com.mkyong.date; import java.time.LocalDate; import java.time.chrono.HijrahDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAdjusters; public class TestDate { public static void main(String[] args) { //first day of Ramadan, 9th …

Read more

Java 8 – MinguoDate examples

This MinguoDate calendar system is primarily used in Taiwan (Republic of China…) (ISO) 1912-01-01 = 1-01-01 (Minguo ROC) To convert the current date to the Minguo date, just subtracts the current year with number 1911, for example 2016 (ISO) – 1911 = 105 (Minguo ROC) 1. LocalDate -> MinguoDate Review a full example to convert …

Read more

Java 8 – ZonedDateTime examples

Few Java 8 java.time.ZonedDateTime examples to show you how to convert a time zone between different countries. Table of contents 1. Convert LocalDateTime to ZonedDateTime 2. Malaysia (UTC+08:00) -> Japan (UTC+09:00) 3. France, Paris (UTC+02:00, DST) -> (UTC-05:00) 4. References 1. Convert LocalDateTime to ZonedDateTime The LocalDateTime has no time zone; to convert the LocalDateTime …

Read more

Java 8 – Convert Date to LocalDate and LocalDateTime

Here is the code to convert java.util.Date to java.time.LocalDate. Date date = new Date(); LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); // different way of create instant object LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); Convert java.util.Date to java.time.LocalDateTime. Date date = new Date(); LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); Convert java.util.Date to java.time.ZonedDateTime. Date date = new Date(); ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault()); …

Read more

Java 8 – Convert Instant to ZonedDateTime

Java 8 examples to show you how to convert from Instant to ZonedDateTime 1. Instant -> ZonedDateTime Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9 InstantZonedDateTime1.java package com.mkyong.date; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; public class InstantZonedDateTime1 { public static void main(String[] argv) { // Z = UTC+0 Instant instant = Instant.now(); …

Read more

Java 8 – Convert Instant to LocalDateTime

Java 8 examples to show you how to convert from Instant to LocalDateTime 1. Instant -> LocalDateTime The java.time.LocalDateTime has no concept of time zone, just provide a zero offset UTC+0. InstantExample1.java package com.mkyong.date; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; public class InstantExample1 { public static void main(String[] argv) { // Parse a ISO 8601 …

Read more

How to Get Current Timestamps in Java

This article shows how to get the current date time or timestamps in Java. import java.sql.Timestamp; import java.time.Instant; import java.util.Date; // 2025-03-07 21:34:46.504 // Get current java.sql.Timestamp Timestamp timestamp = new Timestamp(System.currentTimeMillis()); // 2025-03-07 21:34:46.504 // Get current java.sql.Timestamp from a Date Date date = new Date(); Timestamp timestamp2 = new Timestamp(date.getTime()); // convert Instant …

Read more

How to compare dates in Java

This article shows few examples to compare two dates in Java. Updated with Java 8 examples. 1. Compare two date 1.1 Date.compareTo 1.2 Date.before(), Date.after() and Date.equals() 1.3 Check if a date is within a certain range 2. Compare two calendar 3. Compare two date and time (Java 8) 3.1 Compare two LocalDate 3.2 Compare …

Read more

Java – How to get current date time

In this tutorial, we will show you how to get the current date time from the new Java 8 java.time.* like Localdate, LocalTime, LocalDateTime, ZonedDateTime, Instant and also the legacy date time APIs like Date and Calendar. Table of contents 1. Get current date time in Java 2. java.time.LocalDate 3. java.time.LocalTime 4. java.time.LocalDateTime 5. java.time.ZonedDateTime …

Read more