Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 942
Bill the Lizard Avatar asked Feb 02 '12 16:02

Bill the Lizard


People also ask

What is GregorianCalendar in Java?

GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.

How is date stored in database * Java sql date Java Util date Java sql datetime Java Util datetime?

In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util. Date that handles SQL specific requirements.

How do I change a calendar object to a date object?

Using Calendar. getInstance() method the date from calendar object is obtained and using getTime() method it is converted to Date object. Date object to Calendar object, Date d=new Date(1515660075000l);


2 Answers

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

like image 137
Bill the Lizard Avatar answered Oct 23 '22 12:10

Bill the Lizard


I'm going from memory, but have you tried

Calendar cal = GregorianCalendar.getInstance(); cal.setTime(rs.getDate()); 
like image 36
DaveH Avatar answered Oct 23 '22 12:10

DaveH