如何获得当周的第一周日期

时间:2013-09-11 07:48:26

标签: android

如何以编程方式获取第一天的第一天日期,如果今天是星期三11/9/2013以规则方式获取2013年9月9日星期一的日期是否有任何方法使用当前日期获得一周的第一天? mDay今天给我一天号码,我想用这个号码来获得星期一的星期几

   public static int mYear;
public static int mMonth;
public static int mDay;

    Calendar mCalendar = Calendar.getInstance();
    mYear = mCalendar.get(Calendar.YEAR);
    mMonth = mCalendar.get(Calendar.MONTH) + 1;
    mDay = mCalendar.get(Calendar.DAY_OF_MONTH);






        how to use this function below



          private static Date firstDayOfWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   calendar.set(Calendar.DAY_OF_WEEK, 1);
  return calendar.getTime();


wht parameter i write when call  function



                   firstDayOfWeek();?????


          }

2 个答案:

答案 0 :(得分:0)

尝试使用此代码可以解决您的问题

public class dateTest {
    public static void main (String args  []) throws ParseException{
        DateFormat df=new SimpleDateFormat("dd/MM/yy");
        Date birthDay=df.parse("29/08/02");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);
        cal.setTime(birthDay);

        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()+1);
        System.out.println("Date:       " + cal.get(Calendar.DATE)+ "/" +cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR));

    }
}

获取工作日

Calendar cal=Calendar.getInstance();
       int dayNum=cal.get(Calendar.DAY_OF_WEEK);
       String day=new String();
       switch (dayNum){
           case 1:
            day="Sunday";
               break;
           case 2:
               day="Monday";
               break;
           case 3:
               day="Tuesday";
               break;
           case 4:
               day="Wednesday";
               break;
           case 5:
               day="Thursday";
               break;
           case 6:
               day="Friday";
               break;
           case 7:
               day="Saturday";
               break;

       }
       System.out.println("Day is: "+day);

答案 1 :(得分:0)

您只需使用以下代码: Calendar Class的add(int field,int amount)方法适合您的需要。

int mYear = 0;
int mMonth;
int mDay;
int dayOFWeek = 0;
String mondayDate="";
Calendar mCalendar = Calendar.getInstance();
mCalendar.setFirstDayOfWeek(Calendar.MONDAY);
dayOFWeek = mCalendar.get(Calendar.DAY_OF_WEEK);        
mCalendar.add(Calendar.DATE, -dayOFWeek);

mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH) + 1;
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);