Java8日期时间——LocalDateTime的使用以及相互转换

日期时间处理

Java8内每个类含义

在 Java8 之前操作时间,用的都是 Date 和 Calendar 类,但这两个类,操作起来及其繁琐,且在时间转换、时区转换的时候也很麻烦,因此 JDK 官方在 8 之后,引入了 LocalDateTime 以及相关类,通过新的类来定义和操作时间,也十分的简单清晰,下面我们就来我看一下如何操作。

  1. Instant: 时间戳
  2. Duration: 持续时间, 时间差
  3. LocalDate: 只包含⽇期, ⽐如: 2016-10-20
  4. LocalTime: 只包含时间, ⽐如: 231210
  5. LocalDateTime: 包含⽇期和时间, ⽐如: 2016-10-20 231421
  6. Period: 时间段
  7. ZoneOffset: 时区偏移量, ⽐如: +8:00
  8. ZonedDateTime: 带时区的时间
  9. Clock: 时钟, ⽐如获取⽬前美国纽约的时间

代码实现

localdatetime -> 其他类型

 // =================================================================================

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        Date date = Date.from(localDateTime.toInstant(ZoneOffset.ofHours(8)));
        return date;
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
        Date date = Date.from(localDateTime.toInstant(zoneOffset));
        return date;
    }

    public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
        long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
        return timestamp;
    }

    public static long localDateTimeToTimestamp(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
        long timestamp = localDateTime.toInstant(zoneOffset).getEpochSecond();
        return timestamp;
    }


    public static String localDateTimeToString(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String string = dateTimeFormatter.format(localDateTime);
        return string;
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
        String string = dateTimeFormatter.format(localDateTime);
        return string;
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        String string = dateTimeFormatter.format(localDateTime);
        return string;
    }

date -> 其他类型

// =================================================================================
    public static long dateToTimeStamp(Date date) {
        return date.getTime();
    }

    public synchronized static String dateToString(Date date, SimpleDateFormat simpleDateFormat) {
        return simpleDateFormat.format(date);
    }

    public static String dateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return localDateTime;
    }

    public static LocalDateTime dateToLocalDateTime(Date date, ZoneId zoneId) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);
        return localDateTime;
    }

timestamp -> 其他类型

    // =================================================================================
    public static Date timestampToDate(long timestamp) {
        return new Date(timestamp);
    }

    public static LocalDateTime timestampToLocalDateTime(long timestamp) {
        LocalDateTime localDateTime = Instant.ofEpochSecond(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTime;
    }

string -> 其他类型

    // =================================================================================
    public static Date stringToDate(String string, SimpleDateFormat simpleDateFormat) throws ParseException {
        return simpleDateFormat.parse(string);
    }

    public static Date stringToDate(String string) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(string);
    }

    public static LocalDateTime stringToLocalDateTime(String string) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
        return localDateTime;
    }

    public static LocalDateTime stringToLocalDateTime(String string, DateTimeFormatter dateTimeFormatter) {
        LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
        return localDateTime;
    }

    public static long stringToTimestamp(String string) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
        long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
        return timestamp;
    }

公众号截图




文章在公众号「iceWang」第一手更新,有兴趣的朋友可以关注公众号,第一时间看到笔者分享的各项知识点,谢谢!笔芯!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章