import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtil {
private Logger LOGGER = LoggerFactory.getLogger(this.getClass());
public static final String SHORT_DATETIME = "yyyyMMddHHmmss";
public static final String SHORT_DATETIME2 = "yyyyMMddHHmm";
public static String DATA_TIME_TUC_0_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSS+00:00";
public static String DAY_HOUR_MIN_CHN = "dd'日'HH':'mm'分'";
private static final String GMT = "EEE,d MMM yyyy HH:mm:ss 'GMT'";
public static final String MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String ISO_DATETIME = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static String DATA_TIME_TUC_8_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSS+08:00";
public static String DATA_TIME_TUC_8_MIN = "yyyy-MM-dd'T'00:00:00.000+08:00";
public static String DATA_TIME_TUC_8_MAX = "yyyy-MM-dd'T'23:59:59.999+08:00";
public static final String DATE_MONTH = "yyyy-MM";
public static final String DATE_DAY = "yyyy-MM-dd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DAY_OF_MAX = "yyyy-MM-dd 23:59:59";
public static final String DAY_OF_MIN = "yyyy-MM-dd 00:00:00";
public static SimpleDateFormat MILLISECOND_FORMAT = new SimpleDateFormat(MILLISECOND);
public static SimpleDateFormat ISO_DATETIME_FORMAT = new SimpleDateFormat(ISO_DATETIME);
public static SimpleDateFormat default_sdf4 = new SimpleDateFormat(DATA_TIME_TUC_8_TIME);
public static SimpleDateFormat DATA_TIME_TUC_8_MIN_FORMAT = new SimpleDateFormat(DATA_TIME_TUC_8_MIN);
public static SimpleDateFormat DATA_TIME_TUC_8_MAX_FORMAT = new SimpleDateFormat(DATA_TIME_TUC_8_MAX);
public static SimpleDateFormat DATE_MONTH_FORMAT = new SimpleDateFormat(DATE_MONTH);
public static SimpleDateFormat DATE_DAY_FORMAT = new SimpleDateFormat(DATE_DAY);
public static SimpleDateFormat DATETIME_FORMAT_FORMAT = new SimpleDateFormat(DATETIME_FORMAT);
public static SimpleDateFormat DAY_OF_MAX_FORMAT = new SimpleDateFormat(DAY_OF_MAX);
public static SimpleDateFormat DAY_OF_MIN_FORMAT = new SimpleDateFormat(DAY_OF_MIN);
public static Date changeStringToDate(String dateString, SimpleDateFormat format) {
if (StringUtils.isBlank(dateString)) {
return null;
}
try {
return format.parse(dateString);
} catch (Exception e) {
return null;
}
}
public static String changeDateToString(Date date, SimpleDateFormat format) {
if (date == null) {
return null;
}
return format.format(date);
}
}
测试及结果打印
public static void getTodayTime() {
String date= "2022-11-03T23:59:59.999+08:00";
String date2= "2022-11-03 00:00:00";
Date endTime = changeStringToDate(date, DATA_TIME_TUC_8_TIME_FORMAT);
Date startTime = changeStringToDate(date2, DATETIME_FORMAT_FORMAT);
System.out.println("today startTime : " + startTime + " endTime : " + endTime);
return;
}
public static void getWeekTime() {
Calendar instance = Calendar.getInstance();
String endTime = changeDateToString(instance.getTime(), DATA_TIME_TUC_8_MAX_FORMAT);
instance.add(Calendar.DAY_OF_YEAR, -7);
String startTime = changeDateToString(instance.getTime(), DATA_TIME_TUC_8_MIN_FORMAT);
System.out.println("week startTime : " + startTime + " endTime : " + endTime);
return;
}
public static void main(String[] args) {
getTodayTime();
getWeekTime();
}
today startTime : Thu Nov 03 00:00:00 CST 2022 endTime : Thu Nov 03 23:59:59 CST 2022
week startTime : 2022-10-27T00:00:00.000+08:00 endTime : 2022-11-03T23:59:59.999+08:00