首先,定义`时间枚举值`
public enum TimeEnum { /** * 时间格式 */ YYYY_MM_DD("yyyy-MM-dd"), YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"), YYYYMMDDHHMMSS("yyyyMMddHHmmss"), YYYY_MM("yyyy/MM/"), YYYYMMDD("yyyyMMdd"); String format; TimeEnum(String format) { this.format = format; } public String getFormat() { return format; } }
其次,copy以下方法:
import org.apache.commons.lang.time.FastDateFormat; import org.springframework.lang.NonNull; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class DateUtils { private static final FastDateFormat YYYY_MM = FastDateFormat.getInstance(TimeEnum.YYYY_MM.getFormat()); //yyyy/MM/ private static final FastDateFormat YYYYMMDD = FastDateFormat.getInstance(TimeEnum.YYYYMMDD.getFormat()); //yyyyMMdd private static final FastDateFormat YYYY_MM_DD = FastDateFormat.getInstance(TimeEnum.YYYY_MM_DD.getFormat()); // yyyy-MM-dd private static final FastDateFormat YYYY_MM_DD_HH_MM_SS = FastDateFormat.getInstance(TimeEnum.YYYY_MM_DD_HH_MM_SS.getFormat()); // yyyy-MM-dd HH:mm:ss private static final FastDateFormat YYYYMMDDHHMMSS = FastDateFormat.getInstance(TimeEnum.YYYYMMDDHHMMSS.getFormat()); // yyyyMMddHHmmss public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // yyyy-MM-dd HH:mm:ss /** * 获取当前时间 * * @param timeEnum 时间格式类 * @param date 时间 */ public static String getNowTimeByString(@NonNull TimeEnum timeEnum,@NonNull Date date) { switch (timeEnum) { case YYYY_MM_DD: return YYYY_MM_DD.format(date); case YYYYMMDDHHMMSS: return YYYYMMDDHHMMSS.format(date); case YYYY_MM: return YYYY_MM.format(date); case YYYYMMDD: return YYYYMMDD.format(date); case YYYY_MM_DD_HH_MM_SS: default: return YYYY_MM_DD_HH_MM_SS.format(date); } } /** * 指定时间(参数)向前推,或向后推指定时间 * * @param date 时间 * @param daysCount 天数 * @param timeEnum 时间格式类 */ public static String movePointerTime(Date date, int field, int daysCount, TimeEnum timeEnum) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(field, daysCount); return getNowTimeByString(timeEnum, calendar.getTime()); } /** * 将本地时间, 转换成目标时区的时间 * @param sourceDate * @param targetZoneId {@link ZoneIds} * @return */ public static Date convertTimezone(Date sourceDate, String targetZoneId){ return convertTimezone(sourceDate, TimeZone.getTimeZone(targetZoneId)); } public static Date convertTimezone(Date sourceDate, String sourceZoneId, String targetZoneId){ TimeZone sourceTimeZone=TimeZone.getTimeZone(sourceZoneId); TimeZone targetTimeZone=TimeZone.getTimeZone(targetZoneId); return convertTimezone(sourceDate, sourceTimeZone, targetTimeZone); } /** * 将本地时间,转换成对应时区的时间 * @param localDate * @param targetTimezone 转换成目标时区所在的时间 * @return */ public static Date convertTimezone(Date localDate, TimeZone targetTimezone){ return convertTimezone(localDate, TimeZone.getDefault(), targetTimezone); } /** * 将sourceDate转换成指定时区的时间 * @param sourceDate * @param sourceTimezone sourceDate所在的时区 * @param targetTimezone 转化成目标时间所在的时区 * @return */ public static Date convertTimezone(Date sourceDate, TimeZone sourceTimezone, TimeZone targetTimezone){ Calendar calendar=Calendar.getInstance(); // date.getTime() 为时间戳, 为格林尼治到系统现在的时间差,世界各个地方获取的时间戳是一样的, // 格式化输出时,因为设置了不同的时区,所以输出不一样 long sourceTime=sourceDate.getTime(); calendar.setTimeZone(sourceTimezone); calendar.setTimeInMillis(sourceTime);// 设置之后,calendar会计算各种filed对应的值,并保存 //获取源时区的到UTC的时区差 int sourceZoneOffset=calendar.get(Calendar.ZONE_OFFSET); calendar.setTimeZone(targetTimezone); calendar.setTimeInMillis(sourceTime); int targetZoneOffset=calendar.get(Calendar.ZONE_OFFSET); int targetDaylightOffset=calendar.get(Calendar.DST_OFFSET); // 夏令时 long targetTime=sourceTime+ (targetZoneOffset+targetDaylightOffset) -sourceZoneOffset; return new Date(targetTime); } /** * 时间追加减去几天 */ public static String minusTime(@NonNull TimeEnum timeEnum,@NonNull String time, @NonNull Integer count) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat(timeEnum.getFormat()); Calendar calendar = Calendar.getInstance(); calendar.setTime(dateFormat.parse(time)); calendar.add(Calendar.DAY_OF_WEEK, count); return dateFormat.format(calendar.getTime()); } public static void main(String[] args) throws ParseException { System.out.println(minusTime(TimeEnum.YYYYMMDD, "20200709", -1)); } }