时间工具类、常用时间转换TimeUtils
public class TimeUtils {
private TimeUtils() {
}
private static final Logger logger = LoggerFactory.getLogger(TimeUtils.class);
private static final String STANDARD_DATETIME_24 = "yyyy-MM-dd HH:mm:ss";
private static final String STANDARD_DATETIME_12 = "yyyy-MM-dd hh:mm:ss";
private static final String STANDARD_DATE = "yyyy-MM-dd";
public static long currentTimestamp() {
return System.currentTimeMillis();
}
public static String getDateTime24() {
Date date = new Date();
SimpleDateFormat simpleFormatter = new SimpleDateFormat(STANDARD_DATETIME_24);
return simpleFormatter.format(date);
}
public static String getDateTime12() {
Date date = new Date();
SimpleDateFormat simpleFormatter = new SimpleDateFormat(STANDARD_DATETIME_12);
return simpleFormatter.format(date);
}
public static String parseDateTime(String str) {
SimpleDateFormat simpleDateFormat;
if (str.contains(":")) {
simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
} else {
simpleDateFormat = new SimpleDateFormat(STANDARD_DATE);
}
try {
Date date = simpleDateFormat.parse(str);
return simpleDateFormat.format(date);
} catch (ParseException e) {
logger.error("paraseDateTime() error occur :{}", e.getMessage());
return null;
}
}
public static Date parseStrTime(String dateTime) {
SimpleDateFormat simpleDateFormat;
if (dateTime.contains(":")) {
simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
} else {
simpleDateFormat = new SimpleDateFormat(STANDARD_DATE);
}
try {
return simpleDateFormat.parse(dateTime);
} catch (ParseException e) {
logger.error("covertStrTime() error occur :{}", e.getMessage());
return null;
}
}
public static String timeStampleToTime(String timestamp) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
long time = Long.parseLong(timestamp);
Date date = new Date(time);
return simpleDateFormat.format(date);
}
public static String timeTotimestamp(String str) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
try {
Date date = simpleDateFormat.parse(str);
long time = date.getTime();
return String.valueOf(time);
} catch (ParseException e) {
logger.error("timeTotimestamp() error occur:{}", e.getMessage());
return null;
}
}
public static String getWantDateTime(int num) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, num);
Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
return simpleDateFormat.format(date);
}
public static String getOrderInYear(String str) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
try {
Date date = simpleDateFormat.parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int days = getOrderDay(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DATE));
return String.valueOf(days);
} catch (ParseException e) {
logger.error("getWhenDay() error occur :{}", e.getMessage());
logger.error("请检查入参,样例入参为 yy-MM-dd HH:mm:ss");
return null;
}
}
public static int getOrderDay(int year, int month, int day) {
int num = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
num += 31;
break;
case 2:
num += (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28;
break;
default:
num += 30;
break;
}
}
num += day;
return num;
}
}