Java Convert SQL Date to GregorianCalendar

Tags: Java SQL Date Java GregorianCalendar

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

How to convert SQL Date to GregorianCalendar in Java

In Java, with a given SQL Date object we can convert it to a GregorianCalendar object by using GregorianCalendar.setTime(Date date) to set the value of SQL date to the GregorianCalendar instance as the following example Java program.

ConvertSQLDateToGregorianCalendarExample1.java

import java.sql.Date;
import java.util.GregorianCalendar;

public class ConvertSQLDateToGregorianCalendarExample1 {
    public static void main(String... args) {
        Date date = Date.valueOf("2022-05-29");

        // Convert SQL Date object to GregorianCalendar object
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);

        System.out.println("SQL Date: " + date);
        System.out.println("GregorianCalendar: " + gregorianCalendar.getTime());
    }
}
The output as below.
SQL Date: 2022-05-29
GregorianCalendar: Sun May 29 00:00:00 ICT 2022

Happy Coding 😊

Java Convert SQL Date to ZonedDateTime

Java Convert SQL Date to OffsetDateTime

Java Convert SQL Date to Calendar

Java Convert SQL Date to Instant

Java Convert LocalDate to SQL Date

Java Convert Instant to SQL Date

Java Convert LocalDateTime to SQL Date

Java Convert ZonedDateTime to SQL Date

Java Convert OffsetDateTime to SQL Date

Java Convert Date to SQL Date

Java Convert Calendar to SQL Date

Java Convert GregorianCalendar to SQL Date

Java Convert SQL Date to LocalDate

Java Convert SQL Date to LocalDateTime