2 annbnncnnd AnnBnnCnnD 于 2013.05.06 13:27 提问

根据当期日期计算星期几

根据日期获取当前的星期数字,比如2013.05.07,星期二。

应该怎么实现?

Sunday 05.05.2013
Monday 06.05.2013
*Tuesday 07.05.2013
Wednesday 08.05.2013
Thursday 09.05.2013
Friday 10.05.2013
Saturday 11.05.2013

3个回答

loFataMer
loFataMer   2013.05.06 15:45
已采纳
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd.MM.yyyy");

for (int i = 0; i < 7; i++) {
    Log.i("dateTag", sdf.format(cal.getTime()));
    cal.add(Calendar.DAY_OF_WEEK, 1);
}
franzhong
franzhong   2013.05.06 16:44
private String getWeek() {
          String Week = "星期";   
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//也可将此值当参数传进来
          Date  curDate = new Date(System.currentTimeMillis());
          String pTime = format.format(curDate);  
          Calendar c = Calendar.getInstance();
          try {
           c.setTime(format.parse(pTime));
          } catch (ParseException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }
          switch(c.get(Calendar.DAY_OF_WEEK)){
          case 1:
              Week += "天";
              break;
          case 2:
              Week += "一";
              break;
          case 3:
              Week += "二";
              break;
          case 4:
              Week += "三";
              break;
          case 5:
              Week += "四";
              break;
          case 6:
              Week += "五";
              break;
          case 7:
              Week += "六";
              break;
          default:
              break;          
          }           
          return Week;
         }
loveiflower
loveiflower   2013.05.06 15:09
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 5);
c.set(Calendar.MONTH, 7);
c.set(Calendar.YEAR, 2013);

int weekNo = c.get(Calendar.WEEK_OF_YEAR);
c.set(Calendar.WEEK_OF_YEAR, weekNo);

c.clear();

c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.set(Calendar.YEAR, 2013);


SimpleDateFormat formatter = new SimpleDateFormat("EEE dd/MM/yyyy"); 
Date startDate = c.getTime();
c.add(Calendar.DATE, 1);
for (int i = 0; i < 5; i++) {
    Log.d(formatter.format(c.getTime()));
    c.add(Calendar.DATE, 1);
}

试试看

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!