7

I want to convert LocalDate to GregorianCalendar. Please advice.

In my application we are using GregorianCalendar to work with date's. But I got a Joda LocalDate as a return type when I called one of the webservice. Now I want to convert that LocalDate to GregorianCalendar as it's differing with the month value display. Please see the sample code below.

LocalDate displays 10 for the month October whereas GregorianCalendar display 9 for the month October as it starts from index 0. Please suggest how to convert LocalDate to GregorianCalendar, I tried to see API methods but with no luck.

import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;

// Sample code below
LocalDate localDateObj = new LocalDate();
System.out.println("Month value using LocalDate :" + localDateObj.getMonthOfYear());
System.out.println("Date using LocalDate : " + localDateObj);

// Converting LocalDate to DateTime
DateTime dateTimeObj = localDateObj.toDateTimeAtStartOfDay();
System.out.println("DateTime month " + dateTimeObj.getMonthOfYear());
System.out.println("Date using DateTime : " + dateTimeObj);

// GregorianCalendar
GregorianCalendar gc = new GregorianCalendar();
System.out.println("Month value using GregorianCalendar:" + gc.OCTOBER);
System.out.println("GC :" + gc.getTime());

Output:

Month value using LocalDate :10
Date using LocalDate : 2016-10-13
DateTime month 10
Date using DateTime : 2016-10-13T00:00:00.000-04:00
Month value using GregorianCalendar:9
GC :Thu Oct 13 16:14:43 EDT 2016
informatik01
  • 16,038
  • 10
  • 74
  • 104
joan
  • 131
  • 2
  • 4
  • 12
  • Can you use `gregorianCalendar.setDate(localDate.toDate())`? – clstrfsck Oct 13 '16 at 22:29
  • @msandiford - GregorianCalendar does not have setDate(..) method, cannot do that – joan Oct 14 '16 at 00:05
  • My apologies, typo at the last minute. I meant `gregorianCalendar.setTime(localDate.toDate())`. Despite the name, this actually sets the date as well. – clstrfsck Oct 14 '16 at 00:10
  • @msandiford - LocalDate doesn't have toDate() method..facing hard time to solve this issue.. – joan Oct 14 '16 at 00:22
  • OK, well I'm using Joda Time 2.7, and the documentation tells me that the `toDate()` method has been available since version 2.0. I presume you must be using an older version. Which version? – clstrfsck Oct 14 '16 at 00:26
  • @msandiford - joda-time1.6.jar is the jar I'm using. I tried using gregorianCalendar.setTime(jodaLocalDateObj.toDate()) and its showing error - The method toDate() is not undefined for the type LocalDate. – joan Oct 14 '16 at 00:30
  • OK, then can you use `gregorianCalendar.setTimeInMillis(dateTime.getMillis())`? Note the change to using `DateTime` rather than `LocalDate` - I think this should work in Joda Time 1.6. – clstrfsck Oct 14 '16 at 00:58
  • Possible duplicate of [Convert between LocalDate and XMLGregorianCalendar](https://stackoverflow.com/questions/29767084/convert-between-localdate-and-xmlgregoriancalendar) – Dherik Feb 11 '19 at 19:32

2 Answers2

21

Try this example to convert LocalDate to GregorianCalendar

LocalDate localDateObj = LocalDate.now();
GregorianCalendar gc = GregorianCalendar.from(localDateObj.atStartOfDay(ZoneId.systemDefault()));

Note: gc.OCTOBER is set to 9 but it is indicating the tenth month of the year in the Gregorian and Julian calendars.

Hope this help!

kohane15
  • 809
  • 12
  • 16
David Pham
  • 1,673
  • 19
  • 17
  • 1
    I am using java5, ZoneId class does not support. Please suggest the same when using java5. Thanks. – joan Oct 14 '16 at 01:45
3

Either you can map date manually:

LocalDate date = LocalDate.now();
GregorianCalendar gc = new GregorianCalendar.Builder()
.setDate(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth())
.build();
  • 1
    Note that you'll need to cast it as `build()` returns a `Calendar` type – Andy Jun 13 '20 at 15:58
  • 1
    In addition to @Andy 's comment, it is also worth noticing that the above example is using `LocalDate` from the [`Joda-Time`](https://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html) library, i.e. **not** the Java's own Date and Time library, which also have [`LocalDate`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/time/LocalDateTime.html) class. The method `getMonthOfYear()` is only present in Joda-Time, in the Java's own equivalent it would be `getMonthValue()`. – informatik01 Nov 24 '22 at 18:09