根据提供的日期获取星期几

时间:2013-12-27 08:59:09

标签: android date

以下代码有时会提供正确的输出,有时会提供错误的

例如:

1990年4月7日返回星期一,这是正确的

1987年12月31日返回星期日,这是不正确的,应该是星期二

我试过了:

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;

2 个答案:

答案 0 :(得分:0)

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

将输出设为星期六

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

将输出设为星期四

与日历显示的内容相匹配。

注意:月份字段基于0索引。所以,1月是0,12月是11。

PS:上述代码的实际输出是整数。因此,您需要检查值并找出DAY。

答案 1 :(得分:0)

我设法解决了我的问题

以下代码给出了

1.基于即将到来的生日的出生日期的一周中的一周

2.也在2月29日的闰年测试,效果很好

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;