How do I convert a java.sql.Date object into a GregorianCalendar?
Asked Answered
O

4

21

I thought I'd be able to create a GregorianCalendar using the constructor that takes the year, month, and day, but I can't reliably get those fields from an instance of the java.sql.Date class. The methods that get those values from java.sql.Date are deprecated, and the following code shows why they can't be used:

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

public class DateTester {

    public static void main(String[] args) {
        Date date = Date.valueOf("2011-12-25");
        System.out.println("Year: " + date.getYear());
        System.out.println("Month: " + date.getMonth());
        System.out.println("Day: " + date.getDate());
        System.out.println(date);

        Calendar cal = new GregorianCalendar(date.getYear(), date.getMonth(), date.getDate());
        System.out.println(cal.getTime());
    }
}

Here's the output, showing that the month and year are not returned correctly from the deprecated getYear() and getMonth() methods of Date:

Year: 111
Month: 11
Day: 25
2011-12-25
Thu Dec 25 00:00:00 EST 111

Since I can't use the constructor that I tried above, and there's no GregorianCalendar constructor that just takes a Date, how can I convert a java.sql.Date object into a GregorianCalendar?

Ogpu answered 2/2, 2012 at 16:24 Comment(0)
O
35

You have to do this in two steps. First create a GregorianCalendar using the default constructor, then set the date using the (confusingly named) setTime method.

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

public class DateTester {

    public static void main(String[] args) {
        Date date = Date.valueOf("2011-12-25");
        System.out.println(date);

        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        System.out.println(cal.getTime());
    }
}

Here's the output:

2011-12-25
Sun Dec 25 00:00:00 EST 2011

Ogpu answered 2/2, 2012 at 16:25 Comment(0)
L
4

I'm going from memory, but have you tried

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(rs.getDate());
Lilylivered answered 2/2, 2012 at 16:28 Comment(3)
From where rs.getDate() came from?Chen
sorry - rs is the variable name I always give to ResultSet objects - it's just there to represent a java.sql.Date instance that you have already instantiated.Lilylivered
Not really the most specific answer, but +1 for following some standards that made this the exact code I was looking for.Tierza
C
4

Try this.

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

public class DateTester {

    public static void main(String[] args) {
        Date date = Date.valueOf("2011-12-25");
        System.out.println(date);

        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        System.out.println(cal.getTime());
    }
}
Chen answered 2/2, 2012 at 16:32 Comment(0)
I
2

Use setTimeInMillis():

java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(date.getTime());

I think this is the simplest way.

Inebriate answered 22/1, 2014 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.