import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class DutyTest {
//一共两中判断星期几的方法
// public void getDays(int year,int month, int day) {
//
// Calendar calendar = new GregorianCalendar(year, month, day);
// List<String> weekList = new ArrayList<String>();
// int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// for(int i = 1 ; i <= days ; i++) {
// calendar.add(calendar.DAY_OF_MONTH, i);
// }
// }
//根据输入的日期判断是星期几
public static String getWeekFromDate(String sDate,int type){
if("".equals(sDate)){
return "";
}
SimpleDateFormat df = null;
if(type==0){
df = new SimpleDateFormat("yyyy-MM-dd");
}else if(type==1){
df = new SimpleDateFormat("yyyyMMdd");
}
Date date = null;
try {
date = df.parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int mydate = cd.get(Calendar.DAY_OF_WEEK) - 1 ;
String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
if (mydate < 0)
mydate = 0;
return weekDays[mydate];
//这是第二种
// String showDate = "";
// switch (mydate) { //mydate分别是 1---7:日,一,二。。。
// case 1:
// showDate = "日";
// break;
// case 2:
// showDate = "一";
// break;
// case 3:
// showDate = "二";
// break;
// case 4:
// showDate = "三";
// break;
// case 5:
// showDate = "四";
// break;
// case 6:
// showDate = "五";
// break;
// default:
// showDate = "六";
// break;
// }
//
// return showDate;
}
public static void main(String[] args) {
System.out.println("今天是:"+DutyTest.getWeekFromDate("20130122", 1));
}
}
判断某个日期星期几的方法
最新推荐文章于 2022-10-21 19:58:15 发布