/**
* 当前时间所在一周的周一和周日时间,指定时间格式,默认格式:yyyy-MM-dd
* @param
* @return
*/
public static Map<String, Date> getWeekDate(String source,String patternString) {
Assert.notNull(source,"source must not be null");
if(StringUtils.isEmpty(patternString)){
patternString = CommonUtil.patternString;
}
Date sourceDate = CommonUtil.parseDateByString(source,patternString);
Instant instant = sourceDate.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zone);
LocalDate from = localDateTime.toLocalDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternString);
from.format(formatter);
//指定日期星期数
java.time.DayOfWeek date =from.getDayOfWeek();
//星期一
LocalDate day2 =from.with(TemporalAdjusters.previous(java.time.DayOfWeek.SUNDAY)).plusDays(1);
//星期天
LocalDate day3=from.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY)).minusDays(1);
Map<String, Date> map = new ConcurrentHashMap<>(3);
map.put(MONDAY_STRING,localDateToDate(day2));
map.put(SUNDAY_STRING,localDateToDate(day3));
return map;
}
/**
* localDate 转换为 date
* @param localDate
* @return
*/
private static Date localDateToDate(LocalDate localDate){
Assert.notNull(localDate,"localDate must not be null");
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
Date date = Date.from(zdt.toInstant());
return date;
}
/**
* date转换为指定格式的String
* @param date
* @param patternString
* @return
*/
public static final String dateToString(Date date,String patternString){
if(!Optional.ofNullable(date).isPresent()){
return null;
}
patternString = StringUtils.isEmpty(patternString) ? CommonUtil.patternString:patternString;
SimpleDateFormat format = new SimpleDateFormat(patternString);
String var = format.format(date);
return var;
}