Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting LocalDate to GregorianCalendar in Java

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
like image 590
joan Avatar asked Oct 13 '16 20:10

joan


People also ask

Can we convert LocalDate to date?

Convert LocalDate To Date in Java Step 1: First Create the LocalDate object using either now() or of() method. now() method gets the current date where as of() creates the LocalDate object with the given year, month and day. Step 2: Next, Get the timezone from operating system using ZoneId. systemDefault() method.

How do you convert GregorianCalendar to XMLGregorianCalendar?

DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

How do I convert a calendar to LocalDate?

Calendar cal = Calendar. getInstance(); Date input = cal. getTime(); LocalDate da = input. toInstant().


2 Answers

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!

like image 107
David Pham Avatar answered Oct 16 '22 17:10

David Pham


Either you can map date manually:

LocalDate date = LocalDate.now();
GregorianCalendar gc = new GregorianCalendar.Builder()
.setDate(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth())
.build();
like image 36
Алексей Виноградов Avatar answered Oct 16 '22 18:10

Алексей Виноградов