Why did Calendar.JUNE set the month to July?

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

After daylight saving time, my java calendar object behaves irradically. Setting the month in JUNE actually sets it in July. I have no idea why, but someone suggested setting the object Localein the calendar constructor options. This did not work. The following code returns 01-07-14in 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:

it 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()));
    }
}
+4
source share
2 answers

31 . , , 31 , Calendar 1 .

+5

Source: https://habr.com/ru/post/1534201/


All Articles