java判断日期与星期

原文:http://www.open-open.com/code/view/1440592372888

 

 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Testdouhao {
    public static void main(String[] args) {
        int dayu=-7;
        for(int i=0;i>dayu;i--){
        Date    date=new   Date();//取时间
         Calendar   calendar   =   new   GregorianCalendar();
         calendar.setTime(date);
         calendar.add(calendar.DATE,i);//把日期往后增加一天.整数往后推,负数往前移动
         date=calendar.getTime();   //这个时间就是日期往后推一天的结果
    //    System.out.println(date);
        SimpleDateFormat sFormat=new SimpleDateFormat("yyyyMMdd");
        if(getWeekOfDate(date).equals("星期六") ){
            --dayu;
        }else if(getWeekOfDate(date).equals("星期日")){
            --dayu;
        }else{
            System.out.println(sFormat.format(date));    
        }
        
        }
    }
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
        w = 0;

        return weekDays[w];
        }
}

 

posted @ 2017-09-06 13:36  这个名字想了很久~  阅读(409)  评论(0编辑  收藏  举报