package com.yenange.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestWeekDay {
public static void main(String[] args) throws ParseException {
String[] weekArr={"周日","周一","周二","周三","周四","周五","周六"};
String str="2010-10-3";
System.out.println("求"+str+"是周几?");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date1=sdf.parse("0001-1-1");
Date date2=sdf.parse(str);
System.out.println("第一种方法:用Date.getDay()");
System.out.println(weekArr[date2.getDay()]);
System.out.println("第二种方法:用Calendar.get(Calendar.DAY_OF_WEEK)");
Calendar c=Calendar.getInstance();
c.setTime(date2);
System.out.println(weekArr[c.get(Calendar.DAY_OF_WEEK)-1]);
System.out.println("第三种方法:计算出天数差,模7取余");
System.out.println(weekArr[(differ(date1, date2).intValue()%7)]);
}
public static Long differ(Date date1, Date date2){
//return date1.getTime() / (24*60*60*1000) - date2.getTime() / (24*60*60*1000);
return date2.getTime() / 86400000 - date1.getTime() / 86400000; //用立即数,减少乘法计算的开销
}
}