程序包 com.ajaxjs.util

类 DateHelper


  • public class DateHelper
    extends Object
    日期工具类 SimpleDateFormat 不是线程安全的,Java 8 之后尽量使用 java.time.DateTimeFormatter
    • 构造器详细资料

      • DateHelper

        public DateHelper()
    • 方法详细资料

      • formatDate

        public static String formatDate​(LocalDate date)
        将 LocalDate 转换为字符串。
        参数:
        date - 日期
        返回:
        格式化的日期字符串
      • formatTime

        public static String formatTime​(LocalTime time)
        将 LocalTime 转换为字符串。
        参数:
        time - 时间
        返回:
        格式化的时间字符串
      • formatDateTime

        public static String formatDateTime​(LocalDateTime dateTime)
        将 LocalDateTime 转换为字符串。
        参数:
        dateTime - 日期时间
        返回:
        格式化的日期时间字符串
      • formatDateTime

        public static String formatDateTime​(Date dateTime)
        将 Date 转换为字符串。
        参数:
        dateTime - 日期时间
        返回:
        格式化的日期时间字符串
      • parseDate

        public static LocalDate parseDate​(String dateStr)
        将字符串解析为 LocalDate。
        参数:
        dateStr - 日期字符串
        返回:
        解析后的 LocalDate
        抛出:
        DateTimeParseException - 如果解析失败
      • parseTime

        public static LocalTime parseTime​(String timeStr)
        将字符串解析为 LocalTime。
        参数:
        timeStr - 时间字符串
        返回:
        解析后的 LocalTime
        抛出:
        DateTimeParseException - 如果解析失败
      • parseDateTime

        public static LocalDateTime parseDateTime​(String dateTimeStr)
        将字符串解析为 LocalDateTime。
        参数:
        dateTimeStr - 日期时间字符串
        返回:
        解析后的 LocalDateTime
        抛出:
        DateTimeParseException - 如果解析失败
      • parseDateTimeShort

        public static LocalDateTime parseDateTimeShort​(String dateTimeStr)
        将字符串解析为 LocalDateTime。
        参数:
        dateTimeStr - 日期时间字符串
        返回:
        解析后的 LocalDateTime
        抛出:
        DateTimeParseException - 如果解析失败
      • localDateTime2Date

        public static Date localDateTime2Date​(LocalDateTime localDateTime)
        将 LocalDateTime 转换为 Date 对象 此方法用于处理时间数据类型的转换,将 LocalDateTime 对象转换为 Date 对象 主要通过获取当前时区的时区信息,将 LocalDateTime 转换为 Instant 对象,然后转换为 Date
        参数:
        localDateTime - 需要转换的 LocalDateTime 对象
        返回:
        转换后的 Date 对象
      • localDate2Date

        public static Date localDate2Date​(LocalDate localDate)
        将 LocalDate类型转换为 Date 类型 此方法用于处理日期转换,将 Java 8 引入的 LocalDate 对象转换为传统的 Date 对象 转换基于系统默认时区,将 LocalDate 的开始时刻视为一天的起点
        参数:
        localDate - LocalDate 对象,代表需要转换的日期
        返回:
        Date对象,代表转换后的日期
      • toLocalDate

        public static LocalDate toLocalDate​(Date date)
        将Date对象转换为LocalDate对象 此方法主要用于将Java传统日期时间API中的Date对象转换为Java 8引入的LocalDate对象 这种转换通常需要考虑时区问题,因此这里使用了getZone方法(假设此方法存在且已实现)来处理时区转换
        参数:
        date - Date对象,表示要转换的日期
        返回:
        LocalDate对象,表示转换后的日期
      • toLocalDateTime

        public static LocalDateTime toLocalDateTime​(Date date)
        将Date对象转换为LocalDateTime对象 此方法用于处理时间转换,将一个Date对象(表示特定的瞬间,精确到毫秒) 转换为LocalDateTime对象(表示日期和时间,没有时区信息) 主要用于需要进行日期和时间操作,但不涉及时区的情景
        参数:
        date - Date对象,表示需要转换的时间点
        返回:
        LocalDateTime对象,表示转换后的日期和时间
      • getGMTDate

        public static String getGMTDate()
        请求的时间戳,格式必须符合 RFC1123 的日期格式
        返回:
        当前日期
      • object2Date

        public static Date object2Date​(Object obj)
        支持任意对象转换为日期类型
        参数:
        obj - 任意对象
        返回:
        日期类型对象,返回 null 表示为转换失败
      • nowDateTime

        public static Date nowDateTime()
        获取当前日期时间。
        返回:
        当前日期时间
      • now

        public static String now()
        获取当前日期时间(字符串)。
        返回:
        当前日期时间
      • nowShort

        public static String nowShort()
        获取当前日期时间(字符串),格式如
        返回:
        当前日期时间