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

Bill the Lizard :

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?

Bill the Lizard :

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

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL: How do I convert nvarchar to date

How do I convert this date in to a valid date object?

How do I combine a java.util.Date object with a java.sql.Time object?

Convert changed GregorianCalendar to Date

How do I convert an object containing an Oracle Date to a DateTime?

How do I convert certain Date object to the corresponding time in germany?

How do i convert List into datetime.date object?

How do I parse date String into Date object in Java?

How can I convert year and week to Java Date object?

How do I convert to object different pattern in java

How do I change 'AM' to 'PM' in a Date Object JAVA

How can I convert sql time to java time for an object?

Java GregorianCalendar What am I doing wrong? Wrong date?

How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java?

How do i convert "[object Object]" to a number

How do I convert java.sql.Array to List<MyCustomEnum>?

How do i convert a date in varchar (1-Jan-2020) to date type in DB2 sql?

How to convert string to date object in java

how to convert java string to Date object

how do I have a java date not automatically convert if its an invaild date

How do i convert a json array object of an object into a java 8 optional list of that object

How do I convert a factor into date format?

How do I convert a string with no delimiters into a date?

How do I convert datetime to date (in Python)?

How do I convert a datetime to date?

Is there a term for this type of date and how do I convert it?

how do I convert a date to YYYYMMDD?

VBA - How do I convert Date into String

How i do convert datetime to date pandas?