ZonedDateTime 转为 java.util.Date

    public static void main(String[] args) {
//        1、使用ZoneId定义美国时区
        ZoneId zoneId = ZoneId.of("America/New_York");
//        2、获取当前时间
        ZonedDateTime now = ZonedDateTime.now();
//        3、将当前时间转换为美国时间
        ZonedDateTime zonedDateTime = now.withZoneSameInstant(zoneId);
//        4、定义格式输出
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = zonedDateTime.format(dateFormat);
        System.out.println(format);
        // 转换时间对象
        Date date = MyDateUtil.dateStrToDate("yyyy-MM-dd HH:mm:ss", format);
        System.out.println(MyDateUtil.dateToDateStr(date));
    }

 

posted on 2024-05-14 09:57  陈惟鲜的博客  阅读(79)  评论(0编辑  收藏  举报

导航