java - GregorianCalendar give unexpected result for "Asia/Singapore" TimeZone and date of the system as 30th Jan 2014 -


I am facing a problem related to the Gregorian calendar.

Prerequisite: The system time is: "Asia / Singapore" system time is: 30 January 2014

code executed:

  calendar cal = new Gregorian Calendar (); Timezone tees = Timezone.Gate Time Zone ("Asia / Singapore"); Cal.setTimeZone (TZ); Cal.set (Calendar. MONTH, 1); 1 February is equal to cal.get (calendar.eEER); Cal.set (Calendar: IEER, 2014); Cal.get (Calendar.DAY_OF_MONTH); Cal.set (calendar. DAY_OF_MONTH, 5); Date dte = cal.getTime ();  

Actual results: March 5, 2014

Expected Results: 5 February 2014

But if I execute the same code with the change in sequence then this is the result of the correct value:

  calendar cal = new GregorianCalendar (); Timezone tees = Timezone.Gate Time Zone ("Asia / Singapore"); Cal.setTimeZone (TZ); Cal.get (Calendar.DAY_OF_MONTH); Cal.set (calendar. DAY_OF_MONTH, 5); Cal.set (Calendar. MONTH, 1); 1 February is equal to cal.get (calendar.eEER); Cal.set (Calendar: IEER, 2014);   

post text "itemprop = "Text">

When running your code on January 30, it will be the starting state of your calendar. cal.set (calendar. MONTH, 1); will set the calendar on 30th February. < When calling call> cal.get (DAY_OF_MONTH) the internal state of the calendar will be recalculated to get the right day, then on February 30 it will be changed to 02 March because cal.isLenient () It is true, then on 30th March the result of setting 30

After creating your calendar, try adding cal.setLenient (false) and see how the first version of your code fails.

Remove cal.get () for removing your code - settings, while your canadium is in an intermediate state, so that you can avoid repetition before prematurely.

< P> edit: part about cal.get ()


Comments