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

58,687

Solution 1

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

Solution 2

I'm going from memory, but have you tried

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(rs.getDate());

Solution 3

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());
    }
}

Solution 4

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.

Share:
58,687
Bill the Lizard
Author by

Bill the Lizard

Python, Java, Android, etc. developer living in Charlotte, NC. I am the author of @BountyBot, a Twitter bot that posts new and interesting bounty questions from Stack Overflow. You can view the source on GitHub. My Android apps on Google Play: Three of a Kind - A fun, fast-paced strategy card game. Match any three symbols to win. Sketchboard - Sketch drawings on your mobile device. Serpent - An Android version of the classic mobile game Snake. Math Blitz! - A fast-paced flashcard game to help students practice their arithmetic skills. Linear Interpolator - Fill in the gaps in your data using linear interpolation. Kitchen Calculator - A simple unit converter for common units used in cooking and homebrewing. If you use any of these apps, please leave me a rating and any feedback for improvement.

Updated on January 23, 2020

Comments

  • Bill the Lizard
    Bill the Lizard over 4 years

    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?

  • Ravindra Gullapalli
    Ravindra Gullapalli over 12 years
    From where rs.getDate() came from?
  • DaveH
    DaveH over 12 years
    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.
  • jumps4fun
    jumps4fun almost 10 years
    Not really the most specific answer, but +1 for following some standards that made this the exact code I was looking for.