java-时间相关的类:Date类、SimpleDateFormat类、LocalDateTime类、DateTimeFormatter类等

System类获取时间戳

  • System.currentTimeMillis()
   @Test
    public void test1(){
        long l = System.currentTimeMillis();
        System.out.println(l);
    }

Date类

  • new Date()构造器
  • toString():显示当前的年、月、日、时、分、秒
  • getTime():获取当前Date对象对应的毫秒数。(时间戳)
        Date date=new Date();
        System.out.println(date.toString());
        System.out.println(date.getTime());
  • 创建指定毫秒数的Date对象的构造器
        Date date1=new Date(15455455512L);
        System.out.println(date1);

将java.util.Date对象转换为java.sql.Date对象

        Date date4 = new java.sql.Date(2343243242323L);
        java.sql.Date date5 = (java.sql.Date) date4;
        Date date6 = new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());

SimpleDateFormat类

  • 默认构造器
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat();
        Date date=new Date();
        System.out.println(date.toString());

        //格式化
        String formatStr = simpleDateFormat.format(date);
        System.out.println(formatStr);
        //解析
        String str="20-2-23 上午9:00";
        Date date1 = simpleDateFormat.parse(str);
        System.out.println(date1);
  • 自定义时间格式
        //格式化
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat1.format(date);
        System.out.println(format);
        //解析
        Date parse = simpleDateFormat1.parse("2023-02-23 09:27:06");
        System.out.println(parse);

Calendar类(抽象类)

    @Test
    public void test4(){
        //实例化
        Calendar calendar = Calendar.getInstance();
        //get
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //set
        calendar.set(Calendar.DAY_OF_MONTH,26);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //add
        calendar.add(Calendar.DAY_OF_MONTH,-2);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //gettime
        System.out.println(calendar.getTime());

    }

LocalDateTime类

  • 获取当前时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
  • of():设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 12, 25, 12, 36, 54);
        System.out.println(localDateTime1);
  • getXxx():获取相关的属性
        ///getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getHour());
  • withXXX():设置相关属性 不可变性
        //withXXX():设置相关属性 不可变性
        LocalDateTime localDateTime2 = localDateTime1.withHour(23);
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
  • plusXXX():给属性增加数值
        //plusXXX():给属性增加数值
        LocalDateTime localDateTime3 = localDateTime1.plusHours(4);
        System.out.println(localDateTime3);
  • minusXXX():给属性减少数值
        //minusXXX():给属性减少数值
        LocalDateTime localDateTime4 = localDateTime1.minusMonths(4);
        System.out.println(localDateTime4);

Instant类

  • 获取当前时间
        Instant instant = Instant.now();
        System.out.println(instant);
  • atOffset(ZoneOffset.ofHours(int):添加时间的偏移量
        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
  • toEpochMilli():获取毫秒数
        //获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
        long l = instant.toEpochMilli();
        System.out.println(l);
  • Instant.ofEpochMilli(毫秒数):通过给定的毫秒数,获取Instant实例
        //通过给定的毫秒数,获取Instant实例
        Instant instant1 = Instant.ofEpochMilli(1550475314878L);
        System.out.println(instant1);

DateTimeFormatter类

  • 预定义的标准格式
        //预定义的标准格式
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime localDateTime = LocalDateTime.now();
        //格式化
        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(format);
        //解析
        TemporalAccessor parse = dateTimeFormatter.parse("2023-02-23T17:32:18.225");
        System.out.println(parse);
  • 本地化相关的格式。如:ofLocalizedDateTime()、ofLocalizedDate()
        //本地化相关的格式。如:ofLocalizedDateTime()、ofLocalizedDate()
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        //格式化
        String format1 = dateTimeFormatter1.format(localDateTime);
        System.out.println(format1);
        //解析
        TemporalAccessor parse1 = dateTimeFormatter1.parse("2023年2月23日 星期四");
        System.out.println(parse1);
  • 自定义格式
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format2 = dateTimeFormatter2.format(localDateTime);
        System.out.println(format2);
        //解析
        TemporalAccessor parse2 = dateTimeFormatter2.parse("2023-02-23 05:44:43");
        System.out.println(parse2);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用 `java.time.format.DateTimeFormatter` 将 `java.util.Date` 格式化成 `String` 型。 例如,如果要将日期格式化为 "yyyy-MM-dd" 格式,可以这样做: ``` import java.time.format.DateTimeFormatter; import java.util.Date; Date date = new Date(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String dateString = formatter.format(date.toInstant()); ``` 如果要将时间格式化为 "HH:mm:ss" 格式,可以这样做: ``` import java.time.format.DateTimeFormatter; import java.util.Date; Date date = new Date(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); String dateString = formatter.format(date.toInstant()); ``` 注意,上面的例子中使用了 `java.util.Date.toInstant()` 方法将 `java.util.Date` 对象转换为 `java.time.Instant` 对象。 具体的模式格式可以参考jdk文档,也可以自定义 ### 回答2: 在Java中,我们可以使用DateTimeFormatter来将Date对象格式化为String型。DateTimeFormatterjava.time包中的一个,用于格式化日期和时间。 下面是使用DateTimeFormatterDate对象格式化为String型的步骤: 步骤1:导入必要的包和。 ```java import java.time.format.DateTimeFormatter; import java.util.Date; ``` 步骤2:创建一个DateTimeFormatter对象。 ```java DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); ``` 在这个例子中,我们使用 "yyyy-MM-dd" 作为日期的格式,其中 "yyyy" 表示四位数的年份,"MM" 表示两位数的月份,"dd" 表示两位数的日期。 步骤3:将Date对象转换为LocalDate对象。 ```java LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); ``` 在这个例子中,我们使用Date对象的toInstant()方法将其转换为Instant对象,然后使用atZone()方法将其转换为ZoneId.systemDefault()时区的ZonedDateTime对象,最后使用toLocalDate()方法将其转换为LocalDate对象。 步骤4:使用DateTimeFormatter对象的format()方法将LocalDate对象格式化为String型。 ```java String formattedDate = localDate.format(formatter); ``` 现在,我们已经成功将Date对象格式化为String型。formattedDate变量将包含格式化后的日期字符串,可以根据需要进行使用。 注意:Java 8之前的版本中,可以使用SimpleDateFormat来格式化Date对象为String型,但在Java 8及以后的版本中,推荐使用新的日期和时间API(java.time包)提供的DateTimeFormatter来完成格式化操作。 ### 回答3: 在Java中,我们可以使用DateTimeFormatterDate格式化为String型。DateTimeFormatter是在Java 8中引入的,用于格式化日期和时间对象。 要使用DateTimeFormatterDate格式化为String型,我们需要执行以下步骤: 1. 导入所需的库: ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; ``` 2. 创建Date对象: ```java Date date = new Date(); ``` 3. 将Date对象转换为LocalDateTime对象: ```java LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); ``` 4. 创建DateTimeFormatter对象并定义所需的格式: ```java DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); ``` 5. 使用DateTimeFormatter对象将LocalDateTime对象格式化为String型: ```java String formattedDate = localDateTime.format(formatter); ``` 现在,我们得到了字符串格式化的日期。可以根据需要更改DateTimeFormatter对象的格式模式以获得不同的日期和时间格式。例如,如果我们想要一个只有日期的格式,可以使用"yyyy-MM-dd"作为格式模式。 最后,可以通过输出formattedDate变量来查看格式化后的字符串日期: ```java System.out.println(formattedDate); ``` 请注意,使用Java 8的新日期和时间API会更简洁和安全地处理日期和时间对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅尝酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值