import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.time.temporal.ChronoField.*;
public class DateUtils8 {
private static final ZoneId zoneId = ZoneId.systemDefault();
public static final DateTimeFormatter DATE_STYLE ;
public static final DateTimeFormatter DATETIME_STYLE ;
private static Pattern pattern = Pattern.compile("[0-9]+");
static {
DATE_STYLE = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.toFormatter(Locale.getDefault());
DATETIME_STYLE = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY,2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR,2)
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE,2)
.toFormatter(Locale.getDefault());
}
public static LocalDateTime date2LocalDateTime(Date date){
Instant instant = date.toInstant();
return instant.atZone(zoneId).toLocalDateTime();
}
public static Date localDateTime2Date(LocalDateTime localDateTime){
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
Instant instant = zonedDateTime.toInstant();
return Date.from(instant);
}
public static String format(LocalDateTime localDateTime,DateTimeFormatter formatter){
return localDateTime.format(formatter == null ? DATETIME_STYLE : formatter);
}
public static String format(LocalDate localDate,DateTimeFormatter formatter){
return localDate.format(formatter == null ? DATE_STYLE :formatter);
}
public static LocalDateTime parseDateTime(CharSequence text,DateTimeFormatter formatter){
return LocalDateTime.parse(text, formatter == null ? DATETIME_STYLE :formatter);
}
public static LocalDateTime parseDateTime(CharSequence text){
return LocalDateTime.parse(text, DATETIME_STYLE );
}
public static LocalDate parseDate(CharSequence text,DateTimeFormatter formatter){
return LocalDate.parse(text, formatter == null?DATE_STYLE :formatter);
}
public static LocalDate parseDate(CharSequence text){
return LocalDate.parse(text, DATE_STYLE);
}
public static LocalDateTime parse(CharSequence text){
Matcher matcher = pattern.matcher(text);
int match = 0;
int[] result = new int[6];
result[2] = 1;
while(matcher.find()){
String group = matcher.group();
result[match++] = Integer.parseInt(group);
}
if(match < 2) return null;
return LocalDateTime.of(result[0],result[1],result[2] ,result[3],result[4],result[5]);
}
public static LocalDateTime plus(LocalDateTime localDateTime,long amountToAdd, TemporalUnit unit ){
return localDateTime.plus(amountToAdd,unit);
}
public static long between(LocalDateTime t1,LocalDateTime t2,TemporalUnit unit){
return unit.between(t1,t2);
}
public static String[] getLocalDateTimeDesc(LocalDateTime localDateTime){
if(localDateTime == null ){
localDateTime = LocalDateTime.now();
}
String[] res = new String[2];
res[0] = localDateTime.withHour(0).withMinute(0).withSecond(0).format(DATETIME_STYLE);
res[1] = localDateTime.withHour(23).withMinute(59).withSecond(59).format(DATETIME_STYLE);
return res;
}
public static String[] getDateTimeDesc(Date date){
LocalDateTime localDateTime = null;
if(date == null ){
localDateTime = LocalDateTime.now();
}else{
localDateTime = date2LocalDateTime(date);
}
return getLocalDateTimeDesc(localDateTime);
}
}