java 判断当前日期星期几
当中有两种判断日期的方式,一种是判断数字的星期,还有一种是判断中文的星期。
//首先获取当前的时间
public static void main(String[] args) throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sf.format(new Date());
//在这里调用方法进行操作
}
开始方法进行判断数字星期与日期对应
public static int dayForWeek(String pTime) throws Exception {
//日期的格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
开始方法进行判断中文星期与日期对应
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];
}