62,614
社区成员
发帖
与我相关
我的任务
分享
public static String getTheWeekOfDay(Date startDate) {
if (startDate == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.set(Calendar.DAY_OF_WEEK, 1);
DateFormat format = new SimpleDateFormat("yyyyMM");
return format.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周";
}
/**
*
* @param startTime - 某周的第一天日期
* @throws ParseException
*/
public static void test6(Date startTime) throws ParseException {
//参数合法性判断
if (startTime == null) {
//抛异常
//返回null
return;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startTime);
if (cal.get(Calendar.DAY_OF_WEEK) != 1) {
//开始时间错误,不是某周的第一天
//抛异常
//返回null
return;
}
String DATE_TIME_FORMAT = "yyyyMM"; //在类中定义为常量
long oneDay = 24 * 3600 * 1000L; //在类中定义为常量
DateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
long time = startTime.getTime() + 3 * oneDay; //开始时间+3天,为此周时间中间点
cal.setTimeInMillis(time);
System.out.println(format.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周");
}