Java Convert Calendar to Date

Tags: Java Date Java Calendar

In this Java core tutorial we learn how to convert a java.util.Calendar object to java.util.Date or java.sql.Date object in Java programming language.

Table of contents

  1. How to Convert Calendar to java.util.Date
  2. How to Convert Calendar to java.sql.Date

How to Convert Calendar to java.util.Date

To convert a Calendar object to java.util.Date object we can use the Calendar.getTime() method to return Date object from a Calendar object as Java code below.

ConvertCalendarToDateExample1.java

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

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

        // convert Calendar to java.util.Date
        Date date = calendar.getTime();

        System.out.println(date);
    }
}
The output as below.
Sat Apr 16 13:26:58 ICT 2022

How to Convert Calendar to java.sql.Date

To convert a Calendar object to java.sql.Date object we can use the Calendar.getTimeInMillis() to get milliseconds value of Calendar object and use it to instantiate a java.sql.Date object as following Java program.

ConvertCalendarToDateExample2.java

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

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

        // Convert Calendar to java.sql.Date
        long milliseconds = calendar.getTimeInMillis();
        Date sqlDate = new Date(milliseconds);

        System.out.println(sqlDate);
    }
}
The output as below.
2022-04-16

Happy Coding 😊

Java Convert Date to Calendar

Java Convert Calendar to Milliseconds

Java Convert Calendar to Instant

Java Convert Calendar to String

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples

Java Date by Examples