1. 程式人生 > >獲取下周一至周三的時間

獲取下周一至周三的時間

rate demo led second ets 打印 class days ner

public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub
// 日期格式轉換
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 當前日期
Calendar instance = Calendar.getInstance();
// 調整到下周
instance.add(Calendar.WEDNESDAY, 1);
// 調整到下周1
instance.set(Calendar.DAY_OF_WEEK, -2);
//循環打印
for (int i = 1; i <= 3; i++) {
System.out.println( "星期" + i + ":" +sdf.format(instance.getTime()));
instance.add(Calendar.DAY_OF_WEEK, 1);
}
System.out.println(sdf.format(getStartTime()));
System.out.println(sdf.format(getEndTime()));
private static Date getStartTime() {
Calendar weekdayStart = Calendar.getInstance();
weekdayStart.set(Calendar.HOUR, 0);
weekdayStart.set(Calendar.MINUTE, 0);
weekdayStart.set(Calendar.SECOND, 0);
weekdayStart.set(Calendar.MILLISECOND, 0);
return weekdayStart.getStartTime();
}

private static Date getEndTime() {
Calendar weekdayEnd = Calendar.getInstance();
weekdayEnd.set(Calendar.HOUR, 23);
weekdayEnd.set(Calendar.MINUTE, 59);
weekdayEnd.set(Calendar.SECOND, 59);
weekdayEnd.set(Calendar.MILLISECOND, 999);
return weekdayEnd.getTime();
}
}
}

獲取下周一至周三的時間