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 – 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 – How to change date format in a String

If Java 8, DateTimeFormatter, else SimpleDateFormat to change the date format in a String. 1. DateTimeFormatter (Java 8) Convert the String to LocalDateTime and change the date format with DateTimeFormatter DateFormatExample1.java package com.mkyong; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateFormatExample1 { // date format 1 private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); // date …

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 – 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

Java 8 – How to convert String to LocalDate

Here are a few Java examples of converting a String to the new Java 8 Date API – java.time.LocalDate DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy"); String date = "16/08/2016"; //convert String to LocalDate LocalDate localDate = LocalDate.parse(date, formatter); The key is understand the DateTimeFormatter patterns Note You may interest at this classic java.util.Date example – How to …

Read more

Java – Convert date and time between timezone

In this tutorial, we will show you few examples (ZonedDateTime (Java 8), Date, Calendar and Joda Time) to convert a date and time between different time zones. All examples will be converting the date and time from (UTC+8:00) Asia/Singapore – Singapore Time Date : 22-1-2015 10:15:55 AM to (UTC-5:00) America/New_York – Eastern Standard Time Date …

Read more

Java – Display list of TimeZone with GMT

This Java example shows you how to display a list of TimeZone with GMT in front. P.S Tested with JDK 1.7 Java 8 You may interest at this example – Display all ZoneId and its UTC offset TimeZoneExample.java package com.mkyong.test; import java.util.TimeZone; import java.util.concurrent.TimeUnit; public class TimeZoneExample { public static void main(String[] args) { String[] …

Read more

Java – Convert Date to Calendar example

In Java, you can use calendar.setTime(date) to convert a Date object to a Calendar object. Calendar calendar = Calendar.getInstance(); Date newDate = calendar.setTime(date); A Full example DateAndCalendar.java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateAndCalendar { public static void main(String[] argv) throws ParseException { //1. Create a Date from String SimpleDateFormat sdf …

Read more

Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar. 1. Java Date Examples Few examples to work with Date APIs. Example 1.1 – Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); String date = sdf.format(new Date()); System.out.println(date); //15/10/2013 Example 1.2 – Convert String to Date. SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String …

Read more

Java – Time elapsed in days, hours, minutes, seconds

Two Java examples show you how to print the elapsed time in days, hours, minutes and seconds format. 1. Standard JDK Date APIs You need to calculate the elapsed time manually. DateTimeUtils.java package com.mkyong.dateUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateTimeUtils { public static void main(String[] args) { DateTimeUtils obj = new DateTimeUtils(); …

Read more

How to convert String to Date – Java

In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); Refer to table below for some of the common date and time …

Read more

Android date picker example

In Android, you can use “android.widget.DatePicker” class to render a date picker component to select day, month and year in a pre-defined user interface. In this tutorial, we show you how to render date picker component in current page via android.widget.DatePicker, and also in dialog box via android.app.DatePickerDialog. In addition, we also show you how …

Read more

Insert date value in PreparedStatement

Problem A simple table script in Oracle database. CREATE TABLE DBUSER ( USER_ID NUMBER (5) NOT NULL, USERNAME VARCHAR2 (20) NOT NULL, CREATED_BY VARCHAR2 (20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY ( USER_ID ) ) No idea how to insert current date value, e.g. “04/04/2011” into “CREATED_DATE” field, via JDBC PreparedStatement. String insertTableSQL …

Read more

JSF 2 convertDateTime example

f:convertDateTime” is a standard JSF converter tag, which converts String into a specified “Date” format. In addition, it’s used to implement the date validation as well. The following JSF 2.0 example shows you how to use this “f:convertDateTime” tag. 1. Managed Bean A simple managed bean, with a “date” property. package com.mkyong; import java.io.Serializable; import …

Read more

Spring inject Date into bean property – CustomDateEditor

Spring example to show you how to inject a “Date” into bean property. package com.mkyong.common; import java.util.Date; public class Customer { Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Customer [date=" + date + "]"; } } Bean configuration …

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

Java regex validate date format examples

This article shows how to use regex + code to validate a date format, support single and leading zero month and day format (1 or 01), check for the 30 or 31 days of the month, and leap year validation. Below are the requirements for a valid date. Year format, 1900, 2099 regex Month format, …

Read more

How to calculate elapsed / execute time in Java

In Java, you can use the following ways to measure elapsed time in Java. 1. System.nanoTime() This is the recommended solution to measure elapsed time in Java. ExecutionTime1.java package com.mkyong.time; import java.util.concurrent.TimeUnit; public class ExecutionTime1 { public static void main(String[] args) throws InterruptedException { //start long lStartTime = System.nanoTime(); //task calculation(); //end long lEndTime = …

Read more

How to modify date time (Date Manipulation) – Java

Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class. Get current date time with Calendar() DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println("Current Date Time : " + dateFormat.format(cal.getTime())); Calender date time manipulation …

Read more