Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar giving wrong day of week

I have a calendar object as below that represents 08 Aug 2014.It is a Friday. So myCal.get(Calendar.DAY_OF_WEEK) should be 6. But it gives 2. Why is that?

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]
like image 392
faizal Avatar asked Sep 21 '14 06:09

faizal


Video Answer


2 Answers

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

It doesn't: MONTH=8 is September, not August (month numbering starts from zero).

You can verify yourself by noting DAY_OF_YEAR=251 in your output. The 251st day of a non-leap year is 8 September.

Another way to check the timestamp is by pasting 1410177767000 into http://www.epochconverter.com/

like image 125
NPE Avatar answered Sep 30 '22 03:09

NPE


The GregorianCalender takes month for august as '7' and not '8' since 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));
       }
   }
like image 31
Parth Satra Avatar answered Sep 30 '22 03:09

Parth Satra