JAVA新的时间相关方法——LocalDate、LocalTime、LocalDateTime的应用

注意!Local*在JDK8以上有效

LocalDateLocalTimeLocalDateTime作为JDK8新添加的方法,在处理本地日期、本地时间以及本地日期时间等问题上更加友好,是一个真正的类似于现实世界的日历对象,相较于DateCalendar于公历相对,没有偏移量:
星期一就是星期一,1月就是1,2020年就是2020
并且具有不可变性,在进行设置或者参照对象,原对象依然不变。

以下*代指Date、Time、DateTime

一、构造方法

获取当前时间或者设置一个时间:

  1. Local*.now()获取当前时间
  2. Local*.of(y,m,d,h,m,s)设置一个时间
 /*  Local*.now()   获取当前的时间/日期  */
        LocalDate date=LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(date);
        System.out.println(localTime);
        System.out.println(localDateTime);
运行效果如下:

在这里插入图片描述


        /* .of    设置指定的时间  没有偏移量 */

        date=LocalDate.of(2020,6,6);
        localTime=LocalTime.of(23,23,23);
        localDateTime=LocalDateTime.of(2020,6,6,6,6,6);
        System.out.println(date);
        System.out.println(localTime);
        System.out.println(localDateTime);
运行结果:在这里插入图片描述

二、get方法:

用于获取当前时间对象在日期中的具体参数:

  1. .getDayOfMonth(), 获取当前是本月的第N天
  2. .getDayOfYear(),获取当前是本年的第N天
  3. .getDayOfWeek(),获取当前是星期几
  4. .getDayOfWeek(),获取当前是星期几
  5. .getMonth(),获取当前第N月
  6. .getMonthValue(),获取当前月名称(英文)
  7. .getMinute(),获取当前分钟
   /* get   获取 */
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());
运行效果:

在这里插入图片描述

三、with方法

用来以当前时间对象为基础,获取另一个对象,原本的对象不会改变

  1. .withDayOfMonth() 获取当月第N天的时间对象
  2. .withDayOfYear() 获取当年第N天的时间对象
  3. .withYear() 获取当前日期在第N年的时间对象
  /* with方法  不可变性  */
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime1);

        LocalDateTime localDateTime2 = localDateTime.withYear(1998);
        System.out.println(localDateTime2);
运行效果:

在这里插入图片描述

四、plus方法

用来以当前时间对象为基础 加上一个参数后的时间对象,原本的对象不会改变

  1. plusYears() 当前时间对象N年后的时间对象
  2. plusHours() 当前时间对象N小时后的时间对象
  LocalDateTime localDateTime3 = localDateTime.plusYears(2);
        LocalDateTime localDateTime4 = localDateTime.plusHours(2);
        System.out.println(localDateTime3);
        System.out.println(localDateTime4);
运行效果:

在这里插入图片描述

五、minus方法

用来以当前时间对象为基础,减去一个参数后的时间对象,原本的对象不会改变:

  1. minusDays() 当前时间对象,N天前的时间对象
  2. minusHours() 当前时间对象,N小时前的时间对象
    /* minus  */
        LocalDateTime localDateTime5 = localDateTime.minusDays(5);
        LocalDateTime localDateTime6 = localDateTime.minusYears(5);
        System.out.println(localDateTime5);
        System.out.println(localDateTime6);
运行截图:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值