0

如何以编程方式获取一周的第一天日期,例如今天是 2013 年 11 月 9 日星期三 以编程方式获取 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();?????


          }
4

2 回答 2

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);
于 2013-09-11T08:13:38.847 回答
0

您可以简单地使用下面的代码:Calendar 类的 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);
于 2013-09-11T08:14:16.677 回答