import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* <p>
* Title:
* </p>
*
* @author: xiangsizi
* @date: 2019/04/03
* @version: v1.0
*/
public class DateUtils {
public static final String YEAR_MONTH_DAY_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String YEAR_MONTH_DAY_SECOND_1 = "yyyy-M-dd HH:mm:ss";
public static final String YEAR_MONTH_DAY_SECOND_2 = "yyyy-MM-d HH:mm:ss";
public static final String YEAR_MONTH_DAY_SECOND_3 = "yyyy-M-d HH:mm:ss";
public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
public static final String YEAR_MONTH_DAY_1 = "yyyy-M-dd";
public static final String YEAR_MONTH_DAY_2 = "yyyy-MM-d";
public static final String YEAR_MONTH_DAY_3 = "yyyy-M-d";
public static final String HHMMSS = "HH:mm:ss";
private static final DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_SECOND);
private static final DateTimeFormatter formatterTime_1 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_SECOND_1);
private static final DateTimeFormatter formatterTime_2 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_SECOND_2);
private static final DateTimeFormatter formatterTime_3 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_SECOND_3);
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY);
private static final DateTimeFormatter formatter_1 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_1);
private static final DateTimeFormatter formatter_2 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_2);
private static final DateTimeFormatter formatter_3 = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_3);
private static final SimpleDateFormat time = new SimpleDateFormat(HHMMSS);
private static final ZoneId zoneId = ZoneId.systemDefault();
/**
* 1000-01-01
*/
public static final Date minDate = new Date(-30609820800000l);
/**
* 3000-01-01
*/
public static final Date maxDate = new Date(32503651200000l);
private static final SimpleDateFormat COMMON_DATE_FORMAT_NY = new SimpleDateFormat("yyyy-MM");
private static final SimpleDateFormat COMMON_DATE_FORMAT_YR = new SimpleDateFormat("MM-dd");
private static final SimpleDateFormat COMMON_DATE_FORMAT_Y = new SimpleDateFormat("MM");
/**
* 时间转字符
* @param date
* @return MM
*/
public static String dateToStrinMM(Date date) {
synchronized (COMMON_DATE_FORMAT_Y) {
String updateDate = COMMON_DATE_FORMAT_Y.format(date);
return updateDate;
}
}
/**
* 时间转字符
* @param date
* @return MM-dd
*/
public static String dateToStrinMMDD(Date date) {
synchronized (COMMON_DATE_FORMAT_YR) {
String updateDate = COMMON_DATE_FORMAT_YR.format(date);
return updateDate;
}
}
/**
* 时间转字符
* @param date
* @return yyyy-MM
*/
public static String dateToStringYYYYMM(Date date) {
synchronized (COMMON_DATE_FORMAT_NY) {
String updateDate = COMMON_DATE_FORMAT_NY.format(date);
return updateDate;
}
}
/**
* 时间转字符
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
public static String dateToStringTime(Date date) {
if(date == null) {
return null;
}
Instant instant = date.toInstant();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
String updateDate = formatterTime.format(localDateTime);
return updateDate;
}
/**
* 时间转字符
* @param date
* @return yyyy-MM-dd
*/
public static String dateToString(Date date) {
if(date == null) {
return null;
}
Instant instant = date.toInstant();
LocalDate localDateTime = instant.atZone(zoneId).toLocalDate();
String updateDate = formatter.format(localDateTime);
return updateDate;
}
/**
* 时间转字符
* @param date
* @return HH:mm:ss
*/
public static String dateToTime(Date date) {
synchronized (time) {
if(date == null) {
return null;
}
String updateDate = time.format(date);
return updateDate;
}
}
/**
* 字符转时间
* @param dateStr yyyy-MM-dd HH:mm:ss
* @return Date
*/
public static Date stringTimeToDate(String dateStr) {
String[] splits = dateStr.split("-");
DateTimeFormatter temp = null;
if(splits[1].length() == 1 && splits[2].length() == 11) {
temp = formatterTime_1;
}else if(splits[1].length() == 2 && splits[2].length() == 10) {
temp = formatterTime_2;
}else if(splits[1].length() == 1 && splits[2].length() == 10) {
temp = formatterTime_3;
}else {
temp = formatterTime;
}
LocalDateTime date= LocalDateTime.parse(dateStr, temp);
Instant instant = date.atZone(zoneId).toInstant();
return Date.from(instant);
}
/**
* 字符转时间
* @param dateStr yyyy-MM-dd
* @return Date
*/
public static Date stringToDate(String dateStr) {
String[] splits = dateStr.split("-");
DateTimeFormatter temp = null;
if(splits[1].length() == 1 && splits[2].length() == 2) {
temp = formatter_1;
}else if(splits[1].length() == 2 && splits[2].length() == 1) {
temp = formatter_2;
}else if(splits[1].length() == 1 && splits[2].length() == 1) {
temp = formatter_3;
}else {
temp = formatter;
}
LocalDate date= LocalDate.parse(dateStr, temp);
Instant instant = date.atStartOfDay().atZone(zoneId).toInstant();
return Date.from(instant);
}
/**
* 字符转时间
* @param dateStr HH:mm:ss
* @return Date
* @throws ParseException
*/
public static Date timeToDate(String dateStr) throws ParseException {
synchronized (time) {
return time.parse(dateStr);
}
}
/**
* 检查是不是有效的时期
*
* @param date
* @return
*/
public static boolean checkDate(Date date) {
if (minDate.compareTo(date) > 0) {
return false;
} else if (maxDate.compareTo(date) < 0) {
return false;
}
return true;
}
public static void main(String[] strs) {
}
}
Java日期工具类DateUtils
最新推荐文章于 2024-04-09 15:21:28 发布