Java Date by Examples

Tags: Date Java Date

In this Java core tutorial we learn how to use the java.util.Date class by different Java example programs.

Table of contents

  1. Create Date object from current time
  2. Create Date object from Milliseconds
  3. How to Clone Date object
  4. Convert Date to milliseconds
  5. Convert milliseconds to Date
  6. Convert String to Date object
  7. Convert Date object to String
  8. Check if a Date before the specified Date
  9. Check if a Date after the specified Date
  10. Compare two Date objects for Equality
  11. Compare two Date objects for Ordering
  12. Convert Instance to Date
  13. Convert Date to Instance
  14. Convert Calendar to Date object

Create Date object from current time

Java provides default constructor for Date which can be used to create new Date object with current time as following Java code.

DateExample1.java

import java.util.Date;

public class DateExample1 {
    public static void main(String... args) {
        Date date = new Date();

        System.out.println(date);
    }
}
The output as below.
Wed Apr 13 23:31:14 ICT 2022

Create Date object from Milliseconds

In Java we also can create new Date object with a given milliseconds value since January 1, 1970, 00:00:00 GMT as below.

DateExample2.java

import java.util.Date;

public class DateExample2 {
    public static void main(String... args) {
        long milliseconds = 1600000000000L;

        Date date = new Date(milliseconds);

        System.out.println(milliseconds);
        System.out.println(date);
    }
}
The output as below.
1600000000000
Sun Sep 13 19:26:40 ICT 2020

How to Clone Date object

In Java we can use Date.clone() method to get a copy of Date object as following Java example program.

DateExample3.java

import java.util.Date;

public class DateExample3 {
    public static void main(String... args) {
        Date date1 = new Date();

        Date date2 = (Date)date1.clone();

        System.out.println(date1);
        System.out.println(date2);
    }
}
The output as below.
Wed Apr 13 23:40:46 ICT 2022
Wed Apr 13 23:40:46 ICT 2022

Convert Date to milliseconds

The method Date.getTime() to return the number of milliseconds since January 1, 1970, 00:00:00 GMT. We can use it to convert a Date object into milliseconds values as below.

DateExample4.java

import java.util.Date;

public class DateExample4 {
    public static void main(String... args) {
        Date date = new Date();

        long milliseconds = date.getTime();

        System.out.println(date);
        System.out.println(milliseconds);
    }
}
The output as below.
Wed Apr 13 23:44:14 ICT 2022
1649868254970

Convert milliseconds to Date

In Java we can use the Date.setTime() method to set the milliseconds which represent time after January 1, 1970 00:00:00 GMT for Date object as following Java code.

DateExample5.java

import java.util.Date;

public class DateExample5 {
    public static void main(String... args) {
        long milliseconds = 1549800000000L;

        Date date = new Date();
        date.setTime(milliseconds);

        System.out.println(milliseconds);
        System.out.println(date);
    }
}
The output as below.
1549800000000
Sun Feb 10 19:00:00 ICT 2019

Convert String to Date object

To convert String value into Date object in Java we can use the java.text.SimpleDateFormat class which a given date time format string, and use SimpleDateFormat.parse() method to get Date object from a specified date String as below.

DateExample7.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample7 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date date1 = simpleDateFormat.parse("2022/04/10 08:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/30 20:45:00");

        System.out.println(date1);
        System.out.println(date2);
    }
}
The output as below.
Sun Apr 10 08:00:00 ICT 2022
Sat Apr 30 20:45:00 ICT 2022

Convert Date object to String

To convert Date object into String in Java we can use the java.text.SimpleDateFormat class which a given date time format string, and use SimpleDateFormat.format() method to get the String from a specified Date object as following Java code.

DateExample8.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample8 {
    public static void main(String... args) throws ParseException {
        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String dateString = simpleDateFormat.format(date);

        System.out.println(dateString);
    }
}
The output as below.
2022/04/14 00:01:22

Or simply use the Date.toString() method as below.

DateExample12.java

import java.util.Date;

public class DateExample12 {
    public static void main(String... args) {
        Date date = new Date();

        String dateString = date.toString();

        System.out.println(dateString);
    }
}
The output as below.
Thu Apr 14 00:13:36 ICT 2022

Check if a Date before the specified Date

Java provides the Date.before() method to check if a Date object is before a specified Date object or not. The following Java program to show how to use the Date.before() method.

DateExample6.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample6 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/04/10 08:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/11 08:00:00");

        boolean result1 = date1.before(date2);
        boolean result2 = date2.before(date1);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 1 before Date 2: " + result1);
        System.out.println("Date 2 before Date 1: " + result2);
    }
}
The output as below.
Date 1: Sun Apr 10 08:00:00 ICT 2022
Date 2: Mon Apr 11 08:00:00 ICT 2022
Date 1 before Date 2: true
Date 2 before Date 1: false

Check if a Date after the specified Date

Java provides the Date.after() method to check if a Date object is after a specified Date object or not. The following Java program to show how to use the Date.after() method.

DateExample9.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample9 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2021/03/05 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/11 07:00:00");

        boolean result1 = date1.after(date2);
        boolean result2 = date2.after(date1);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 1 after Date 2: " + result1);
        System.out.println("Date 2 after Date 1: " + result2);
    }
}
The output as below.
Date 1: Fri Mar 05 07:00:00 ICT 2021
Date 2: Mon Apr 11 07:00:00 ICT 2022
Date 1 after Date 2: false
Date 2 after Date 1: true

Compare two Date objects for Equality

In Java we can use the Date.equals() method to compares two Date objects for equality as following Java example code.

DateExample10.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample10 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date3 = simpleDateFormat.parse("2022/04/11 08:00:00");

        boolean result1 = date1.equals(date2);
        boolean result2 = date1.equals(date3);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 3: " + date3);
        System.out.println("Date 1 equal Date 2: " + result1);
        System.out.println("Date 1 equal Date 3: " + result2);
    }
}
The output as below.
Date 1: Mon Apr 11 07:00:00 ICT 2022
Date 2: Mon Apr 11 07:00:00 ICT 2022
Date 3: Mon Apr 11 08:00:00 ICT 2022
Date 1 equal Date 2: true
Date 1 equal Date 3: false

Compare two Date objects for Ordering

In Java we can use the Date.compareTo() method to return value of -1, 0 or 1 if the a Date object is before, equal to or after the Date object it compare to.

The following Java program to show how to compare two Date objects for ordering using the Date.compareTo() method.

DateExample11.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample11 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date3 = simpleDateFormat.parse("2022/04/11 08:00:00");

        int result1 = date1.compareTo(date2);
        int result2 = date1.compareTo(date3);
        int result3 = date3.compareTo(date1);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 3: " + date3);
        System.out.println("Date 1 compare to Date 2: " + result1);
        System.out.println("Date 1 compare to Date 3: " + result2);
        System.out.println("Date 3 compare to Date 1: " + result3);
    }
}
The output as below.
Date 1: Mon Apr 11 07:00:00 ICT 2022
Date 2: Mon Apr 11 07:00:00 ICT 2022
Date 3: Mon Apr 11 08:00:00 ICT 2022
Date 1 compare to Date 2: 0
Date 1 compare to Date 3: -1
Date 3 compare to Date 1: 1

Convert Instance to Date

Java provides the static method Date.from() which can be used to create new Date object from a given Instance object. The following Java program to show how to use the Date.from() method to convert an Instance object to Date object.

DateExample13.java

import java.time.Instant;
import java.util.Date;

public class DateExample13 {
    public static void main(String... args) {
        Instant instant = Instant.now();

        Date date = Date.from(instant);

        System.out.println("Instant: " + instant);
        System.out.println("Date: " + date);
    }
}
The output as below.
Instant: 2022-04-13T17:21:09.901Z
Date: Thu Apr 14 00:21:09 ICT 2022

Convert Date to Instance

In Java we can use the Date.toInstant() method to convert a Date object into Instance object as Java code below.

DateExample14.java

import java.time.Instant;
import java.util.Date;

public class DateExample14 {
    public static void main(String... args) {
        Date date = new Date();

        Instant instant = date.toInstant();

        System.out.println("Date: " + date);
        System.out.println("Instant: " + instant);
    }
}
The output as below.
Date: Thu Apr 14 00:21:40 ICT 2022
Instant: 2022-04-13T17:21:40.733Z

Convert Calendar to Date object

To convert a Calendar object to Date object we can use Calendar.getTime() method as the following Java program.

DateExample15.java

import java.util.Calendar;
import java.util.Date;

public class DateExample15 {
    public static void main(String... args) {
        Calendar calendar = Calendar.getInstance();

        Date date = calendar.getTime();
        System.out.println(date);
    }
}
The output as below.
Thu Apr 14 00:24:35 ICT 2022

Happy Coding 😊

Java LocalDate by Examples

Java LocalTime by Examples

Java LocalDateTime by Examples

Java Calendar by Examples

Java Get Current Date and Time

Java Calendar using Calendar.Builder by Examples

Java Convert Date to Milliseconds

Java Convert Date to String

Java Convert Date to Instant

Java Convert Date to Calendar

Java Convert Calendar to Date