各种日期格式转换工具类
import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
@Slf4j
public class DateUtil {
public static String getBuyTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
public static String getTodayDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(new Date());
}
public static String getSendDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(new Date());
}
public static int getMonthDays(int year, int month) {
int days = 0;
boolean isLeapYear = false;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) isLeapYear = true;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
if (isLeapYear) {
days = 29;
} else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
log.debug("month匹配错误!");
break;
}
return days;
}
public static int getStringFormatToInt(String s) {
int i = Integer.parseInt(s.substring(0, 1));
int format = 0;
if (i == 0) {
String sub = s.substring(1);
format = Integer.parseInt(sub);
} else {
format = Integer.parseInt(s);
}
return format;
}
public static String getCalculateTime(int year, int month, int calculateDay) {
if (calculateDay <= 0) {
month = month - 1;
if (month == 0) {
month = 12;
year = year - 1;
}
int monthDays = DateUtil.getMonthDays(year, month);
calculateDay += monthDays;
}
String dd = "";
String mm = "";
if (month < 10) mm = "0" + month;
if (calculateDay < 10) dd = "0" + calculateDay;
return year + "-" + mm + "-" + dd + " 15:00:00";
}
public static int getDayForWeek() {
LocalDate now = LocalDate.now();
return now .getDayOfWeek().getValue();
}
public static int getDayForNumber() {
LocalDate now = LocalDate.now();
return now .getDayOfMonth();
}
}