Search code examples
javagregorian-calendarlocaldate

converting date to GregorianCalendar Date format


I want to convert the org.joda.time.LocalDate object to java.util.GregorianCalendar. java.util.GregorianCalendar month displayed from index 0 to 11. So October is represented with digit 9 , November with digit 10....Whereas for LocalDate October is represented with digit 10, november with 11....

I am working on existing application where they have used GregorianCalendar. Now I want to convert my LocalDate value which I had received through a method call into GregorianCalendar format so that month values are matched.

I have already searched many blogs but could not convert the value. Below is the code I tried but still could not able to change the date format to GregorianCalendar format.

java code:

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
class Convert{
    public static String format(GregorianCalendar calendar){
        SimpleDateFormat fmt = new SimpleDateFormat("dd-M-yyyy");
        fmt.setCalendar(calendar);
        String dateFormatted = fmt.format(calendar.getTime());
        System.out.println("--date formatted--- " + dateFormatted);
        return dateFormatted;
    }
    void convertLocalDateToGregorianCalendar(){
        LocalDate localDate = new LocalDate();
        DateTime dateTimeObj = localDate.toDateTimeAtStartOfDay();
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(dateTimeObj.getMillis());
        String formattedGCDate = format(gc);
        System.out.println("converted LocalDate to GregorianDate date value " + formattedGCDate);
    }
    public static void main(String[] args) {
                new Convert().convertLocalDateToGregorianCalendar();         
} }

Output:

--date formatted--- 13-10-2016
converted LocalDate to GregorianDate date value 13-10-2016

Expected date value in the output: 13-9-2016 (As for GregorianCalendar October month is represent with digit 9).

PS: java5 is the version I am using.


Solution

  • If i understand the question right we are mixing 2 things

    a) Internal representation of the months within Calender / GregorianCalender and the actual Calendar itself.

    The internal representation as you rightly say is represented from 0-11 denoting 12 months of the year , the human interpretation of that is 1-12 months of the year. When you call the format operation you get the way the date appears on a actual human calender which is October , month 10 of the year. The internal representation is still 9. If you ask how , you can check using the API

    gc.setTimeInMillis(dateTimeObj.getMillis());
    System.out.println(gc.get(Calendar.MONTH));
    

    This prints the numeral 9

    If you want the internal representation of the date , you can do so too. A really hacky way would be as follows although i do not vouch for it , but gives you an overall idea of things.

    LocalDate localDate = new LocalDate();
    DateTime dateTimeObj = localDate.toDateTimeAtStartOfDay();
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(dateTimeObj.getMillis());
    System.out.println(gc.get(Calendar.DAY_OF_MONTH) +"-"+ ("00"+ gc.get(Calendar.MONTH)).substring(1)  +"-"+gc.get(Calendar.YEAR));