Class Converter

java.lang.Object
org.aoju.bus.core.date.Formatter
org.aoju.bus.core.date.Converter
Direct Known Subclasses:
Almanac

public class Converter extends Formatter
日期转换
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Converter

      public Converter()
  • Method Details

    • toDate

      public static Date toDate(LocalDateTime localDateTime)
      LocalDateTime转Date
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      Date
    • toDate

      public static Date toDate(LocalDate localDate)
      LocalDate转Date
      Parameters:
      localDate - LocalDate
      Returns:
      Date
    • toDate

      public static Date toDate(LocalTime localTime)
      LocalTime转Date 以当天的日期+LocalTime组成新的LocalDateTime转换为Date
      Parameters:
      localTime - LocalTime
      Returns:
      Date
    • toDate

      public static Date toDate(Instant instant)
      Instant转Date
      Parameters:
      instant - Instant
      Returns:
      Date
    • toDate

      public static Date toDate(long epochMilli)
      时间戳epochMilli毫秒转Date
      Parameters:
      epochMilli - 时间戳
      Returns:
      Date
    • toDate

      public static Date toDate(ZonedDateTime zonedDateTime)
      ZonedDateTime转Date 注意时间对应的时区和默认时区差异
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      Date
    • toDate

      public static Date toDate(YearMonth yearMonth, int dayOfMonth)
      YearMonth转Date 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 如果要转换为当月最后一天,可以使用下面方法:toDateEndOfMonth(YearMonth)
      Parameters:
      yearMonth - YearMonth
      dayOfMonth - 天
      Returns:
      Date
    • toDateStartOfMonth

      public static Date toDateStartOfMonth(YearMonth yearMonth)
      YearMonth转Date,转换为当月第一天
      Parameters:
      yearMonth - YearMonth
      Returns:
      Date
    • toDateEndOfMonth

      public static Date toDateEndOfMonth(YearMonth yearMonth)
      YearMonth转Date,转换为当月最后一天
      Parameters:
      yearMonth - YearMonth
      Returns:
      Date
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Date date)
      Date转LocalDateTime
      Parameters:
      date - Date
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Timestamp timestamp)
      Timestamp转LocalDateTime
      Parameters:
      timestamp - Timestamp
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(LocalDate localDate)
      LocalDate转LocalDateTime
      Parameters:
      localDate - LocalDate
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(LocalTime localTime)
      LocalTime转LocalDateTime 以当天的日期+LocalTime组成新的LocalDateTime
      Parameters:
      localTime - LocalTime
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Instant instant)
      Instant转LocalDateTime
      Parameters:
      instant - Instant
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(long epochMilli)
      时间戳epochMilli毫秒转LocalDateTime
      Parameters:
      epochMilli - 时间戳
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(TemporalAccessor temporal)
      temporal转LocalDateTime
      Parameters:
      temporal - TemporalAccessor
      Returns:
      LocalDateTime
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime)
      ZonedDateTime转LocalDateTime 注意时间对应的时区和默认时区差异
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      LocalDateTime
    • toLocalDate

      public static LocalDate toLocalDate(Date date)
      Date转LocalDate
      Parameters:
      date - Date
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(LocalDateTime localDateTime)
      LocalDateTime转LocalDate
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(Instant instant)
      Instant转LocalDate
      Parameters:
      instant - Instant
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(long epochMilli)
      时间戳epochMilli毫秒转LocalDate
      Parameters:
      epochMilli - 时间戳
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(TemporalAccessor temporal)
      temporal转LocalDate
      Parameters:
      temporal - TemporalAccessor
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(ZonedDateTime zonedDateTime)
      ZonedDateTime转LocalDate 注意时间对应的时区和默认时区差异
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      LocalDate
    • toLocalDate

      public static LocalDate toLocalDate(YearMonth yearMonth, int dayOfMonth)
      YearMonth转LocalDate 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 如果要转换为当月最后一天,可以使用下面方法:toLocalDateEndOfMonth(YearMonth)
      Parameters:
      yearMonth - YearMonth
      dayOfMonth - 天
      Returns:
      LocalDate
    • toTimeZone

      public static DateTime toTimeZone(Date date, ZoneId zoneId)
      Date 转换时区
      Parameters:
      date - Date
      zoneId - ZoneId
      Returns:
      DateTime
    • toTimeZone

      public static DateTime toTimeZone(Date date, TimeZone timeZone)
      Date 转换时区
      Parameters:
      date - Date
      timeZone - TimeZone
      Returns:
      DateTime
    • toLocalDateStartOfMonth

      public static LocalDate toLocalDateStartOfMonth(YearMonth yearMonth)
      YearMonth转LocalDate,转换为当月第一天
      Parameters:
      yearMonth - YearMonth
      Returns:
      LocalDate
    • toLocalDateEndOfMonth

      public static LocalDate toLocalDateEndOfMonth(YearMonth yearMonth)
      YearMonth转LocalDate,转换为当月最后一天
      Parameters:
      yearMonth - YearMonth
      Returns:
      LocalDate
    • toLocalTime

      public static LocalTime toLocalTime(Date date)
      Date转LocalTime
      Parameters:
      date - Date
      Returns:
      LocalTime
    • toLocalTime

      public static LocalTime toLocalTime(LocalDateTime localDateTime)
      LocalDateTime转LocalTime
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      LocalTime
    • toLocalTime

      public static LocalTime toLocalTime(Instant instant)
      Instant转LocalTime
      Parameters:
      instant - Instant
      Returns:
      LocalTime
    • toLocalTime

      public static LocalTime toLocalTime(TemporalAccessor temporal)
      temporal转LocalTime
      Parameters:
      temporal - TemporalAccessor
      Returns:
      LocalTime
    • toLocalTime

      public static LocalTime toLocalTime(ZonedDateTime zonedDateTime)
      ZonedDateTime转LocalTime 注意时间对应的时区和默认时区差异
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      LocalTime
    • toInstant

      public static Instant toInstant(Date date)
      Date转Instant
      Parameters:
      date - Date
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(Timestamp timestamp)
      Timestamp转Instant
      Parameters:
      timestamp - Timestamp
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(LocalDateTime localDateTime)
      LocalDateTime转Instant
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(LocalDate localDate)
      LocalDate转Instant
      Parameters:
      localDate - LocalDate
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(Calendar calendar)
      CalendarInstant对象
      Parameters:
      calendar - Date对象
      Returns:
      Instant对象
    • toInstant

      public static Instant toInstant(LocalTime localTime)
      LocalTime转Instant 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant
      Parameters:
      localTime - LocalTime
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(long epochMilli)
      时间戳epochMilli毫秒转Instant
      Parameters:
      epochMilli - 时间戳
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(ZonedDateTime zonedDateTime)
      ZonedDateTime转Instant 注意,zonedDateTime时区必须和当前系统时区一致,不然会出现问题
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      Instant
    • toInstant

      public static Instant toInstant(TemporalAccessor temporalAccessor)
      Date对象转换为Instant对象
      Parameters:
      temporalAccessor - Date对象
      Returns:
      Instant对象
    • toCalendar

      public static Calendar toCalendar(long millis)
      转换为Calendar对象
      Parameters:
      millis - 时间戳
      Returns:
      Calendar对象
    • toCalendar

      public static Calendar toCalendar(Date date)
      Date转换为Calendar
      Parameters:
      date - 日期转换为日历的日期
      Returns:
      创建的日历
    • toCalendar

      public static Calendar toCalendar(XMLGregorianCalendar calendar)
      转换为Calendar对象
      Parameters:
      calendar - 日期对象
      Returns:
      Calendar对象
    • toCalendar

      public static Calendar toCalendar(Date date, TimeZone timeZone)
      Date转换为Calendar.
      Parameters:
      date - 日期转换为日历的日期
      timeZone - 时区
      Returns:
      创建的日历
    • toEpochMilli

      public static long toEpochMilli(Date date)
      Date转时间戳 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      date - Date
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(Timestamp timestamp)
      Timestamp转时间戳 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      timestamp - Timestamp
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(LocalDateTime localDateTime)
      LocalDateTime转时间戳 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(LocalDate localDate)
      LocalDate转时间戳 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      localDate - LocalDate
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(Instant instant)
      Instant转时间戳 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      instant - Instant
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(ZonedDateTime zonedDateTime)
      ZonedDateTime转时间戳,注意,zonedDateTime时区必须和当前系统时区一致,不然会出现问题 从1970-01-01T00:00:00Z开始的毫秒值
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      时间戳
    • toEpochMilli

      public static long toEpochMilli(TemporalAccessor temporalAccessor)
      TemporalAccessor转换为 时间戳(从1970-01-01T00:00:00Z开始的毫秒数) 如果为Month,调用Month.getValue()
      Parameters:
      temporalAccessor - Date对象
      Returns:
      Instant对象
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(Date date)
      Date转ZonedDateTime,时区为系统默认时区
      Parameters:
      date - Date
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(Date date, String zoneId)
      Date转ZonedDateTime
      Parameters:
      date - Date
      zoneId - 目标时区
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(Date date, ZoneId zone)
      Date转ZonedDateTime
      Parameters:
      date - Date
      zone - 目标时区
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime)
      LocalDateTime转ZonedDateTime,时区为系统默认时区
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, String zoneId)
      LocalDateTime转ZonedDateTime,时区为zoneId对应时区 注意,需要保证localDateTime和zoneId是对应的,不然会出现错误
      Parameters:
      localDateTime - LocalDateTime
      zoneId - LocalDateTime
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(LocalDate localDate)
      LocalDate转ZonedDateTime,时区为系统默认时区
      Parameters:
      localDate - LocalDate
      Returns:
      ZonedDateTime such as 2020-02-19T00:00+08:00[Asia/Shanghai]
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(LocalTime localTime)
      LocalTime转ZonedDateTime 以当天的日期+LocalTime组成新的ZonedDateTime,时区为系统默认时区
      Parameters:
      localTime - LocalTime
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(Instant instant)
      Instant转ZonedDateTime,时区为系统默认时区
      Parameters:
      instant - Instant
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(long epochMilli)
      时间戳epochMilli毫秒转ZonedDateTime,时区为系统默认时区
      Parameters:
      epochMilli - 时间戳
      Returns:
      ZonedDateTime
    • toZonedDateTime

      public static ZonedDateTime toZonedDateTime(TemporalAccessor temporal)
      temporal转ZonedDateTime,时区为系统默认时区
      Parameters:
      temporal - TemporalAccessor
      Returns:
      ZonedDateTime
    • toYearMonth

      public static YearMonth toYearMonth(Date date)
      Date转YearMonth
      Parameters:
      date - Date
      Returns:
      YearMonth
    • toYearMonth

      public static YearMonth toYearMonth(LocalDateTime localDateTime)
      LocalDateTime转YearMonth
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      YearMonth
    • toYearMonth

      public static YearMonth toYearMonth(LocalDate localDate)
      LocalDate转YearMonth
      Parameters:
      localDate - LocalDate
      Returns:
      YearMonth
    • toYearMonth

      public static YearMonth toYearMonth(Instant instant)
      Instant转YearMonth
      Parameters:
      instant - Instant
      Returns:
      YearMonth
    • toYearMonth

      public static YearMonth toYearMonth(ZonedDateTime zonedDateTime)
      ZonedDateTime转YearMonth
      Parameters:
      zonedDateTime - ZonedDateTime
      Returns:
      YearMonth
    • toTimestamp

      public static Timestamp toTimestamp(Date date)
      Date转Timestamp
      Parameters:
      date - Date
      Returns:
      Timestamp
    • toTimestamp

      public static Timestamp toTimestamp(LocalDateTime localDateTime)
      LocalDateTime转Timestamp
      Parameters:
      localDateTime - LocalDateTime
      Returns:
      Timestamp
    • toTimestamp

      public static Timestamp toTimestamp(Instant instant)
      Instant转Timestamp
      Parameters:
      instant - Instant
      Returns:
      Timestamp
    • toTimestamp

      public static Timestamp toTimestamp(long epochMilli)
      时间戳epochMilli转Timestamp
      Parameters:
      epochMilli - 时间戳
      Returns:
      Timestamp
    • toTime

      public static String toTime(int seconds)
      秒数转为时间格式(HH:mm:ss)
      Parameters:
      seconds - 需要转换的秒数
      Returns:
      转换后的字符串
    • toSecond

      public static int toSecond(String time)
      HH:mm:ss 时间格式字符串转为秒数
      Parameters:
      time - 字符串时分秒(HH:mm:ss)格式
      Returns:
      时分秒转换后的秒数