/**
* 获取本周开始和结束时间
*
* @return String[] 返回格式"yyyy-MM-dd HH:mm:ss
* @author xyl
* @date 2023/1/4 13:25
*/
public static String[] getCurrentWeekTimeFrame() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//start of the week
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_YEAR, -1);
}
calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
String startTime = conversion(calendar.getTimeInMillis());
//end of the week
calendar.add(Calendar.DAY_OF_WEEK, 6);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
String endTime = conversion(calendar.getTimeInMillis());
return new String[]{startTime, endTime};
}
/**
* 传入时间戳获取日期格式时间
*
* @param time
* @return String
* @author xyl
* @date 2023/1/4 13:24
*/
public static String conversion(Long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(time);
}
获取本周开始和结束时间
最新推荐文章于 2024-08-16 10:36:34 发布