Calendar showing the wrong day of the week

I have a calendar object as shown below that represents 08 Aug 2014

. It's Friday. So there myCal.get(Calendar.DAY_OF_WEEK)

should be 6. But that gives 2. Why?

java.util.GregorianCalendar[time=1410177767000,areFieldsSet=true,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=251,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=32,SECOND=47,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]

      

+3


source to share


2 answers


I have a calendar object as shown below, which represents 08 Aug 2014.

It is not: MONTH=8

- September, not August (month numbering starts at zero).



You can be sure by checking DAY_OF_YEAR=251

in your release. The 251st day of a non-leap year is September 8th .

Another way to check the timestamp is to paste 1410177767000

into http://www.epochconverter.com/

+10


source


GregorianCalender takes the month in August, and "7" rather than "8" because January is represented as "0". Reference: Gregorian calendar

So kindly check the following and it should work.



   import java.util.*;

   public class Test {
       public static void main(String args[]) {
           GregorianCalendar myCal = new GregorianCalendar(2014, 7, 8);
           System.out.println(myCal.get(Calendar.DAY_OF_WEEK));
       }
   }

      

+3


source







All Articles