java 时间工具类

简介: java 时间工具类

public class DateTimeUtil {

/**
 * 年月日是分秒
 */
public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
 * 年月日是分秒毫秒(6位)
 */
public final static String YYYY_MM_DD_HH_MM_SS_SSSSSS = "yyyy-MM-dd HH:mm:ss.SSSSSS";
/**
 * 年月日是分秒
 */
public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
/**
 * 年月日是分秒毫秒(6位)
 */
public final static String YYYYMMDDHHMMSSSSSSSS = "yyyyMMddHHmmssSSSSSS";
/**
 * 年月日
 */
public final static String YYYY_MM_DD = "yyyy-MM-dd";
/**
 * 年月日
 */
public final static String YY_MM_DD = "yy-MM-dd";
/**
 * 年月
 */
public final static String YYYY_MM = "yyyy-MM";

/**
 * 月日
 */
public final static String MM_DD = "MM-dd";

/**
 * 时分秒
 */
public final static String HH_MM_SS = "HH:mm:ss";
/**
 * 时分
 */
public final static String HH_MM = "HH:mm";
/**
 * 时分
 */
public final static String HH_MM_00 = "HH:mm:00";

public final static String[][] WEEK_ARRAY = {{"", ""}, {"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};

public final static String[][] WEEK_ARRAY_S = {{"", ""}, {"MONDAY", "周一"}, {"TUESDAY", "周二"}, {"WEDNESDAY", "周三"}, {"THURSDAY", "周四"}, {"FRIDAY", "周五"}, {"SATURDAY", "周六"}, {"SUNDAY", "周日"}};
/**
 * 最小时间为null
 */
public final static LocalTime LOCAL_TIME_MIN = LocalTime.of(0, 0, 0, 000_000_001);

/**
 * Linux时间戳格式化
 *
 * @param epochSecond
 * @return
 */
public static LocalDateTime epochSecond(long epochSecond) {
    return LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.ofHours(8));
}

/**
 * Linux时间戳格式化
 *
 * @param epochSecond
 * @param pattern
 * @return
 */
public static String epochSecondFormat(long epochSecond, String pattern) {
    LocalDateTime time2 = LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.ofHours(8));
    return DateTimeFormatter.ofPattern(pattern).format(time2);
}

/**
 * Linux时间戳格式化
 *
 * @param epochSecond
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String epochSecondFormat(long epochSecond) {
    return epochSecondFormat(epochSecond, YYYY_MM_DD_HH_MM_SS);
}

/**
 * LocalDateTime 转换为 Linux时间戳(秒级时间戳)
 *
 * @param time
 * @return yyyy-MM-dd HH:mm:ss
 */
public static long toEpochSecond(LocalDateTime time) {
    return time.toEpochSecond(ZoneOffset.of("+8"));
}

/**
 * LocalDateTime 转换为 Linux时间戳(毫秒级时间戳)
 *
 * @param time
 * @return yyyy-MM-dd HH:mm:ss
 */
public static long toEpochMilli(LocalDateTime time) {
    return time.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
}

/**
 * 毫秒级时间戳转 LocalDateTime
 *
 * @param epochMilli 毫秒级时间戳
 * @return LocalDateTime
 */
public static LocalDateTime ofEpochMilli(long epochMilli) {
    return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.ofHours(8));
}

/**
 * 日期时间格式化
 *
 * @param now
 * @param pattern
 * @return
 */
public static String format(LocalDateTime now, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).format(now);
}

/**
 * 日期格式化
 *
 * @param now
 * @param pattern
 * @return
 */
public static String format(LocalDate now, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).format(now);
}

/**
 * 时间格式化
 *
 * @param now
 * @param pattern
 * @return
 */
public static String format(LocalTime now, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).format(now);
}

/**
 * 日期时间格式化
 *
 * @param now
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String format(LocalDateTime now) {
    return format(now, YYYY_MM_DD_HH_MM_SS);
}
/**
 * 日期时间格式化
 *
 * @param now
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String format2(LocalDateTime now) {
    return format(now, YYYY_MM_DD_HH_MM_SS_SSSSSS);
}

/**
 * 日期格式化
 *
 * @param now
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String format(LocalDate now) {
    return format(now, YYYY_MM_DD);
}

/**
 * 时间格式化
 *
 * @param now
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String format(LocalTime now) {
    return format(now, HH_MM_SS);
}


/**
 * 当前日期时间
 *
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String nowFormat() {
    return format(LocalDateTime.now(), YYYY_MM_DD_HH_MM_SS);
}

/**
 * 当前日期
 *
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String nowFormatDate() {
    return format(LocalDate.now(), YYYY_MM_DD);
}

/**
 * 日期时间格式化
 *
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String nowFormat(String pattern) {
    return format(LocalDateTime.now(), pattern);
}

/**
 * Date时间类转LocalDateTime
 *
 * @param date Date时间类
 * @return LocalDateTime
 */
public static LocalDateTime ofDate(Date date) {
    return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
}

/**
 * LocalDateTime时间类转 Date时间类
 *
 * @param localDateTime LocalDateTime时间类
 * @return Date时间类
 */
public static Date toDate(LocalDateTime localDateTime) {
    return Date.from(localDateTime.atZone(ZoneOffset.of("+8")).toInstant());
}

/**
 * 日期时间字符串 转LocalDateTime
 *
 * @param localDateTime 日期时间字符串 2022-05-11 11:10:01
 * @return LocalDateTime
 */
public static LocalDateTime parse(String localDateTime, String pattern) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return LocalDateTime.parse(localDateTime, dateTimeFormatter);
}

/**
 * 日期字符串 转LocalDateTime
 *
 * @param localDateTime 日期字符串 2022-05-11
 * @return LocalDateTime
 */
public static LocalDate parseDate(String localDateTime, String pattern) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return LocalDate.parse(localDateTime, dateTimeFormatter);
}

/**
 * 日志字符串 转LocalDateTime
 *
 * @param localDateTime 时间字符串 11:10:01
 * @return LocalDateTime
 */
public static LocalTime parseTime(String localDateTime, String pattern) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return LocalTime.parse(localDateTime, dateTimeFormatter);
}

/**
 * 时间字符串 转LocalDateTime
 *
 * @param localDateTime 时间字符串
 * @return LocalDateTime
 */
public static LocalDateTime parse(String localDateTime) {
    return parse(localDateTime, YYYY_MM_DD_HH_MM_SS);
}

/**
 * 日期字符串 转LocalDateTime
 *
 * @param localDateTime 日期字符串 2022-05-11
 * @return LocalDateTime
 */
public static LocalDate parseDate(String localDateTime) {
    return parseDate(localDateTime, YYYY_MM_DD);
}

/**
 * 日志字符串 转LocalDateTime
 *
 * @param localDateTime 时间字符串 11:10:01
 * @return LocalDateTime
 */
public static LocalTime parseTime(String localDateTime) {
    return parseTime(localDateTime, HH_MM_SS);
}

/**
 * 当前时间
 *
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String now() {
    return format(LocalDateTime.now(), YYYY_MM_DD_HH_MM_SS);
}

/**
 * 当前日期时间
 */
public static LocalDateTime nowLocalDateTime() {
    return LocalDateTime.now();
}

/**
 * 当前日期
 */
public static LocalDate nowLocalDate() {
    return LocalDate.now();
}

/**
 * 当前时间
 */
public static LocalTime nowLocalTime() {
    return LocalTime.now();
}

/**
 * 一周的第一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate firstDayOfWeek(LocalDate localDate) {
    return localDate.with(DayOfWeek.MONDAY);
}

/**
 * 一周的最后一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate lastDayOfWeek(LocalDate localDate) {
    return localDate.with(DayOfWeek.SUNDAY);
}

/**
 * 月的第一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate firstDayOfMonth(LocalDate localDate) {
    return localDate.with(TemporalAdjusters.firstDayOfMonth());
}

/**
 * 月的最后一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate lastDayOfMonth(LocalDate localDate) {
    return localDate.with(TemporalAdjusters.lastDayOfMonth());
}

/**
 * 每年的第一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate firstDayOfYear(LocalDate localDate) {
    return localDate.with(TemporalAdjusters.firstDayOfYear());
}

/**
 * 每年的最后一天
 *
 * @param localDate 当地日期
 * @return {@link LocalDate}
 */
public static LocalDate lastDayOfYear(LocalDate localDate) {
    return localDate.with(TemporalAdjusters.lastDayOfYear());
}


/**
 * 每周的所有日期  即周一到周日
 *
 * @param localDate 当地日期
 * @return {@link List<LocalDate>}
 */
public static List<LocalDate> allDaysOfWeek(LocalDate localDate) {
    List<LocalDate> allDays = new ArrayList<>();
    allDays.add(localDate.with(DayOfWeek.MONDAY));
    allDays.add(localDate.with(DayOfWeek.TUESDAY));
    allDays.add(localDate.with(DayOfWeek.WEDNESDAY));
    allDays.add(localDate.with(DayOfWeek.THURSDAY));
    allDays.add(localDate.with(DayOfWeek.FRIDAY));
    allDays.add(localDate.with(DayOfWeek.SATURDAY));
    allDays.add(localDate.with(DayOfWeek.SUNDAY));
    return allDays;
}

/**
 * 每月的所有日期  即1日到31日
 *
 * @param localDate 当地日期
 * @return {@link List<LocalDate>}
 */
public static List<LocalDate> allDaysOfMonth(LocalDate localDate) {
    List<LocalDate> allDays = Lists.newArrayList();
    LocalDate firstDayOfMonth = firstDayOfMonth(localDate);
    LocalDate lastDayOfMonth = lastDayOfMonth(localDate);
    allDays.add(firstDayOfMonth);
    int i = 1;
    LocalDate temp = firstDayOfMonth;
    while (!temp.isEqual(lastDayOfMonth)) {
        LocalDate day = firstDayOfMonth.plusDays(i);
        allDays.add(day);
        temp = day;
        i++;
    }
    return allDays;
}

/**
 * 获取两个日期间隔的所有日期
 *
 * @param start 格式必须为'2018-01-25'
 * @param end   格式必须为'2018-01-25'
 * @return
 */
public static List<String> getBetweenDate(String start, String end) {
    List<String> list = Lists.newArrayList();
    LocalDate startDate = LocalDate.parse(start);
    LocalDate endDate = LocalDate.parse(end);

    long distance = ChronoUnit.DAYS.between(startDate, endDate);
    if (distance < 1) {
        return list;
    }
    Stream.iterate(startDate, d -> {
        return d.plusDays(1);
    }).limit(distance + 1).forEach(f -> {
        list.add(f.toString());
    });
    return list;
}

/**
 * 获取某年某月---有多少天。例如2022-05有31天
 *
 * @param monthOfYear 格式必须为"2022-05"
 * @return
 */
public static Integer getDaysOfMonth(String monthOfYear) {

    String[] dateStrArray = monthOfYear.split("-");

    // 指定月份
    int year = Integer.valueOf(dateStrArray[0]);
    int month = Integer.valueOf(dateStrArray[1]);

    // 获取日历对象
    Calendar c = Calendar.getInstance();
    // 设置日历对象为指定年月日,为指定月份的第一天
    c.set(year, month, 1);

    // 设置日历对象,指定月份往前推一天,也就是最后一天
    c.add(Calendar.DATE, -1);

    // 获取这一天输出即可
    int day = c.get(Calendar.DATE);

    return day;
}

/**
 * 获取周几,例如星期二 返回 二
 *
 * @param value 数字星期 几  周二,填  2,周日 填 7
 * @return 返回  二
 */
public static String getWeekName(int value) {
    if (value <= 0 || value > 7) {
        return "";
    }
    String[] strings = WEEK_ARRAY[value];
    if (null != strings) {
        return strings[1];
    }
    return "";
}

/**
 * 获取周几,例如星期二 返回 周二
 *
 * @param value 数字星期 几  周二,填  2,周日 填 7
 * @return 返回  二
 */
public static String getWeekName_s(int value) {
    if (value <= 0 || value > 7) {
        return "";
    }
    String[] strings = WEEK_ARRAY_S[value];
    if (null != strings) {
        return strings[1];
    }
    return "";
}

/**
 * 获取周几,例如星期二 返回 二
 *
 * @param currentDate
 * @return 返回  二
 */
public static String getWeekName(LocalDateTime currentDate) {
    return getWeekName(currentDate.getDayOfWeek().getValue());
}

/**
 * 获取周几,例如星期二 返回 二
 *
 * @param currentDate
 * @return 返回  二
 */
public static String getWeekName(LocalDate currentDate) {
    return getWeekName(currentDate.getDayOfWeek().getValue());
}

/**
 * 获取周几,例如星期二 返回 周二
 *
 * @param currentDate
 * @return 返回  周x
 */
public static String getWeekName_s(LocalDate currentDate) {
    return getWeekName_s(currentDate.getDayOfWeek().getValue());
}

/**
 * 时间相差 毫秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMillis(LocalTime now, LocalTime end) {
    return Duration.between(now, end).toMillis();
}

/**
 * 日期相差 毫秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMillis(LocalDate now, LocalDate end) {
    return Duration.between(now, end).toMillis();
}

/**
 * 日期时间相差 秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToSecond(LocalDateTime now, LocalDateTime end) {
    return Duration.between(now, end).toMillis() / 1000;
}

/**
 * 时间相差 秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToSecond(LocalTime now, LocalTime end) {
    return Duration.between(now, end).toMillis() / 1000;
}

/**
 * 日期相差 秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToSecond(LocalDate now, LocalDate end) {
    return Duration.between(now, end).toMillis() / 1000;
}

/**
 * 日期时间相差 毫秒
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMillis(LocalDateTime now, LocalDateTime end) {
    return Duration.between(now, end).toMillis();
}

/**
 * 时间相差 分钟
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMinutes(LocalTime now, LocalTime end) {
    return Duration.between(now, end).toMinutes();
}

/**
 * 日期相差 分钟
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMinutes(LocalDate now, LocalDate end) {
    return Duration.between(now, end).toMinutes();
}

/**
 * 日期时间相差 分钟
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToMinutes(LocalDateTime now, LocalDateTime end) {
    return Duration.between(now, end).toMinutes();
}

/**
 * 时间相差 小时
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToHours(LocalTime now, LocalTime end) {
    return Duration.between(now, end).toHours();
}

/**
 * 日期相差 小时
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToHours(LocalDate now, LocalDate end) {
    return Duration.between(now, end).toHours();
}

/**
 * 日期时间相差 小时
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToHours(LocalDateTime now, LocalDateTime end) {
    return Duration.between(now, end).toHours();
}

/**
 * 日期相差 天
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToDays(LocalDate now, LocalDate end) {
    return Duration.between(now, end).toDays();
}

/**
 * 日期时间相差 天
 * end 比 now 大得到 正整数,end 比 now 小得到 负数
 *
 * @param now
 * @param end
 * @return
 */
public static long differToDays(LocalDateTime now, LocalDateTime end) {
    return Duration.between(now, end).toDays();
}

/**
 * 自动设置值,如果为null,那么为最小值
 *
 * @param local
 * @return
 */
public static LocalDate setDefault(LocalDate local) {
    return null == local ? LocalDate.MIN : local;
}

/**
 * 自动设置值,如果为null,那么为最小值
 *
 * @param local
 * @return
 */
public static LocalDateTime setDefault(LocalDateTime local) {
    return null == local ? LocalDateTime.MIN : local;
}

/**
 * 自动设置值,如果为null,那么为最小值
 *
 * @param local
 * @return
 */
public static LocalTime setDefault(LocalTime local) {
    return null == local ? LOCAL_TIME_MIN : local;
}

}

相关文章
|
10天前
|
算法 搜索推荐 Java
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
这篇文章介绍了如何使用Java后端技术,结合Graphics2D和Echarts等工具,生成包含个性化信息和图表的海报,并提供了详细的代码实现和GitHub项目链接。
30 0
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
|
15天前
|
Java
Java 些许公共工具类
Java 些许公共工具类
12 1
|
2月前
|
缓存 前端开发 Java
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
【8月更文挑战第10天】java基础巩固,工具类的封装
24 1
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
|
2月前
|
Java
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
|
2月前
|
存储 设计模式 安全
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
38 0
|
3月前
|
设计模式 存储 安全
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
57 1
|
3月前
|
安全 Java 开发者
Java中的并发工具类与线程安全实现
Java中的并发工具类与线程安全实现
|
4月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
3月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
3月前
|
并行计算 Java API
Java中的并发工具类详解
Java中的并发工具类详解