-1

The following code sometimes give correct output And sometimes gives wrong

For example :

7th april 1990 returns Monday which is correct

31st dec 1987 returns Sunday which is incorrect, it should be Tuesday

I have tried:

int mm=Integer.parseInt(m);
GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(y),mm,Integer.parseInt(d));
int i = calendar.get(Calendar.DAY_OF_WEEK);
String dayOfTheWeek = null;
if(i == 2){
    dayOfTheWeek = "Monday";           
} else if (i==3){
    dayOfTheWeek = "Tuesday";
} else if (i==4){
    dayOfTheWeek = "Wednesday";
} else if (i==5){
    dayOfTheWeek = "Thursday";
} else if (i==6){
    dayOfTheWeek = "Friday";
} else if (i==7){
    dayOfTheWeek = "Saturday";
} else if (i==1){
    dayOfTheWeek = "Sunday";
}
Log.v("Event Week",Integer.toString(i));
return dayOfTheWeek;
sabadow
  • 5,095
  • 3
  • 34
  • 51
karthik
  • 165
  • 14

2 Answers2

0
GregorianCalendar calendar = new GregorianCalendar(1987, 11, 31);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));

Gives output as Saturday

and

GregorianCalendar calendar = new GregorianCalendar(1990, 3, 7);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));

Gives output as Thursday

Which matches what the calendar shows.

Note: The month field is 0-index based. So, January is 0, and December is 11.

PS: The actual output to the above code is an integer. So, you will need to check the values and figure out the DAY.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

I managed to solve my problem

The following code gives

1.Day of week based on birthdate of the coming birthday

2.Also tested with leap year Feb 29, works perfect

int checkFutureLeapYear=0;
            Calendar today=Calendar.getInstance();
              Calendar BDay=Calendar.getInstance();
              int CYear=today.get(Calendar.YEAR);   
              // checking whether entered date contains Feb 29 ,if yes check whether //current year is leap years so that it can handle 29
              if (Integer.parseInt(m)==02 && (Integer.parseInt(d)==29)) {            
            // if leap year leave the date as it as , since it can handle 29      
        if ((CYear % 4 == 0 && CYear % 100 != 0) || (CYear % 400 == 0)) {
                      d=d;            
                }
            // if not subtract 1 from date , to make it 28 , as current year donot contains feb 29 ,so making it feb 28
                  else {
                      int date=Integer.parseInt(d);
                      date=(date-1);
                      d=Integer.toString(date);           
                }             
            }

            String startDate=y+"/"+m+"/"+d;     
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
            Date date=null;
            try{
                  date=sdf.parse(startDate);
              }
          catch (Exception e) {
            e.printStackTrace();
         }        
          BDay.setTime(date);     
          int BMonth=BDay.get(Calendar.MONTH);
          int BDate=BDay.get(Calendar.DATE);
          int CMonth=today.get(Calendar.MONTH);
          int CDate=today.get(Calendar.DATE);

          BDay.set(Calendar.YEAR,today.get(Calendar.YEAR));
      // if current month is greater than birth month , then there is no birthday in the current year , means birthday has gone for the year.Hence looking for birthday in the next year by adding 1 to the current year
          if (BMonth<CMonth) {
            BDay.set(Calendar.YEAR,today.get(Calendar.YEAR)+1);
            checkFutureLeapYear=1;
         }

     // Also if current month and birth month are same , but current date is greater than birth date , then there is no birthday in the current year , means birthday has gone for the year.Hence looking for birthday in the next year by adding 1 to the current year
          else if ((BMonth==CMonth)&&(BDate<CDate)) {
              BDay.set(Calendar.YEAR,today.get(Calendar.YEAR)+1);
              checkFutureLeapYear=1;
         }
     // code to check that ,is the future year can handle feb 29, same as above conditions 
          if (checkFutureLeapYear==1) {
              CYear=BDay.get(Calendar.YEAR);
              Log.v("checkFutureLeapYear",Integer.toString(CYear));
;             if (Integer.parseInt(m)==02 && (Integer.parseInt(d)==29)) {            
                  if ((CYear % 4 == 0 && CYear % 100 != 0) || (CYear % 400 == 0)) {
                      d=d;            
                }
                  else {
                      int date1=Integer.parseInt(d);
                      date1=(date1-1);
                      d=Integer.toString(date1);    
                      Log.v("Date 29 subtracted to ",(d));
                }             
            }
                startDate=Integer.toString(CYear)+"/"+m+"/"+d;      
                sdf=new SimpleDateFormat("yyyy/MM/dd");
                date=null;
                try{
                        date=sdf.parse(startDate);
                    }
                    catch (Exception e) {
                            e.printStackTrace();
                    }         
                    BDay.setTime(date);
                    Log.v("dfdfgdffgfgb",Integer.toString(BDay.get(Calendar.YEAR)));
        }


        String dayOfTheWeek;
        Log.v("Birth year",Integer.toString(BDay.get(Calendar.YEAR)));
        Log.v("Birth month",Integer.toString(BDay.get(Calendar.MONTH)));
        Log.v("Birth date",Integer.toString(BDay.get(Calendar.DAY_OF_MONTH)));
        SimpleDateFormat mFormat = new SimpleDateFormat("EEEE", Locale.US);
        dayOfTheWeek = mFormat.format(BDay.getTime());
        return dayOfTheWeek;    
karthik
  • 165
  • 14