Java中Date和LocalDateTime格式转换工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateUtil {

    private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    private static DateTimeFormatter localDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter localDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static DateTimeFormatter localTimeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static String getLocalDateTimeFormat(LocalDateTime localDateTime){
        String format = localDateTimeFormat.format(localDateTime);
        return format;
    }

    public static String getLocalDateFormat(LocalDate localDate){
        String format = localDateFormat.format(localDate);
        return format;
    }

    public static String getLocalTimeFormat(LocalTime localTime){
        String format = localTimeFormat.format(localTime);
        return format;
    }

    public static String getDateTimeFormat(Date date){
        String format = dateTimeFormat.format(date);
        return format;
    }

    public static String getDateFormat(Date date){
        String format = dateFormat.format(date);
        return format;
    }

    public static String getTimeFormat(Date date){
        String format = timeFormat.format(date);
        return format;
    }
    
    public static Long getLocalDateTimeCurrentSec(){
        //获取当前时间戳,单位秒
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        return second;
    }

    public static Long getLocalDateTimeCurrentMilliSec(){
        //获取当前时间戳,单位毫秒
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return milliSecond;
    }

    public static LocalDateTime dateConvertToLocalDateTime(Date date) {
        //将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

    public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
        //将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }

    public static LocalDateTime secondToLocalDateTime(Long second) {
        //时间戳转LocalDateTime (单位:秒)
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
        return localDateTime;
    }

    public static LocalDateTime millisecondsToLocalDateTime(Long milliseconds) {
        //时间戳转LocalDateTime (单位:毫秒)
        LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTime;
    }

    public static Date timestampToDate(Long timestamp){
        return new Date(timestamp);
    }

    public static Date timeStrToDate(String timeStr){
        //字符串格式 yyyy-MM-dd HH:mm:ss
        Date date = null;
        try {
            date = dateTimeFormat.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static LocalDateTime timeStrToLocalDateTime(String timeStr){
        //字符串格式 yyyy-MM-dd HH:mm:ss
        LocalDateTime parse = LocalDateTime.parse(timeStr, localDateTimeFormat);
        return parse;
    }
}

测试:

public static void localDateTimeTest(){
        String localDateTimeFormat = LocalDateUtil.getLocalDateTimeFormat(LocalDateTime.now());
        System.out.println(localDateTimeFormat);
        String localDateFormat = LocalDateUtil.getLocalDateFormat(LocalDate.now());
        System.out.println(localDateFormat);
        String localTimeFormat = LocalDateUtil.getLocalTimeFormat(LocalTime.now());
        System.out.println(localTimeFormat);

        String dateTimeFormat = LocalDateUtil.getDateTimeFormat(new Date());
        System.out.println(dateTimeFormat);
        String dateFormat = LocalDateUtil.getDateFormat(new Date());
        System.out.println(dateFormat);
        String timeFormat = LocalDateUtil.getTimeFormat(new Date());
        System.out.println(timeFormat);

        System.out.println("---------------------------------------");
        System.out.println(LocalDateUtil.getLocalDateTimeCurrentSec());
        System.out.println(LocalDateUtil.getLocalDateTimeCurrentMilliSec());
        System.out.println(LocalDateUtil.dateConvertToLocalDateTime(new Date()));
        System.out.println(LocalDateUtil.localDateTimeConvertToDate(LocalDateTime.now()));

        System.out.println("---------------------------------------");
        System.out.println(LocalDateUtil.secondToLocalDateTime(System.currentTimeMillis()/1000));
        System.out.println(LocalDateUtil.millisecondsToLocalDateTime(System.currentTimeMillis()));
        System.out.println(LocalDateUtil.timestampToDate(System.currentTimeMillis()));
        System.out.println(LocalDateUtil.timeStrToDate("2022-08-26 14:18:23"));
        System.out.println(LocalDateUtil.timeStrToLocalDateTime("2022-08-26 14:18:23"));
    }

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值