Question

I suppose some of you read the title, and went "oh, another question about java's 0-based month system...". Well, not this time.

After we switched to daylight savings time, my java calendar object behaves irradically. Setting the month to JUNE, actually sets it to July. I have no idea why, but someone suggested that I set the Locale - object in the calendar's constructor parameters. That did not work. The following code returns 01-07-14 in my console.

Any ideas?

public class test {

    public static void main(String[] args){
        Locale locale = new Locale("da-DK");
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");

        Calendar date = new GregorianCalendar(locale);
        date.set(Calendar.MONTH, Calendar.JUNE);
        System.out.println(sdf.format(date.getTime()));
    }
}

UPDATE:

this also returns 01-07-14

public class test {

    public static void main(String[] args){
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
        TimeZone timeZone = TimeZone.getTimeZone("Europe/Copenhagen");

        Calendar date = new GregorianCalendar(timeZone);
        date.set(Calendar.MONTH, Calendar.JUNE);
        System.out.println(sdf.format(date.getTime()));
    }
}
Was it helpful?

Solution

Today is the 31st of March. When you set the month of the Calendar, the current day remains set, but the 31st of June doesn't exist so the Calendar rolls over to the 1st of July.

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top