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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梅尝酥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值