JDK8新时间:LocalDate、LocalTime、LocalDateTime

请添加图片描述

请添加图片描述

LocalDate

里面的是年月日星期,最主要的是六方面如下

请添加图片描述

package com.API1;

import java.time.LocalDate;

public class test_LocalDate {
    public static void main(String[] args) {
        //获取本地日期对象(不可变对象)
        LocalDate ld = LocalDate.now();//年、月、日
        System.out.println("日期:"+ld);

        //1.获取日期对象中的信息
        int year=ld.getYear();//得到年份
        System.out.println("年份:"+year);
        //ld.Month得到的是Month类型,ld.getMonthValue方法返回的是int型的数据
        int month=ld.getMonth().getValue();//得到月份
        //int month = ld.getMonthValue();
        System.out.println("月份:"+month);
        //getDayOfMonth表示一个月中的第几天
        int day=ld.getDayOfMonth();//得到具体几号
        System.out.println(day+"号");
        //一年当中的第几天
        int year_inday=ld.getDayOfYear();
        System.out.println("一年当中的第"+year_inday+"天");
        //星期几
        int dayofweek=ld.getDayOfWeek().getValue();
        System.out.println("哦~今天星期几~哦~今天星期"+dayofweek);

        //2.直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayOfYear
        //因为LocalDate里面只有年月日,因此只能修改年月日
        LocalDate ld2 = ld.withYear(2099);//此方法返回的是新对象,原本日期不改变
        System.out.println("修改后的日期:"+ld2);
        System.out.println("系统真正的日期:"+ld);
        //每次修改都是返回一个新的对象
        LocalDate ld3 = ld.withMonth(1);
        System.out.println("拜托回到第"+ld3+"月");
        LocalDate ld4 = ld.withDayOfMonth(1);
        System.out.println("拜托回到这个月的第"+ld4+"天");
        LocalDate ld5 = ld.withDayOfYear(1);
        System.out.println("拜托回到今年的第"+ld5+"天");

        //3.把某个信息加多少:plusYears、pulsMonths、plusDays、plusWeeks
        LocalDate ld6=ld.plusYears(2);
        LocalDate ld7=ld.plusDays(2);

        //4.把某个信息减多少;minusYears、minusDays、minusWeeks
        LocalDate ld8 = ld.minusYears(9);

        //5.获取指定日期的LocalDate对象:public LocalDate of(int year,int month,int dayOfMonth);
        LocalDate ld9=LocalDate.of(2099,12,12);
        //6.判断2个日期对象,是否相等,在前还是在后:equals isBefore isAfter
        System.out.println(ld9.equals(ld));
        System.out.println(ld9.isAfter(ld));
        System.out.println(ld9.isBefore(ld));
    }
}

LocalTime

时分秒纳秒,用法和LocalDate用法相同

请添加图片描述

ZonedDateTime:带时区时间的常见方法

请添加图片描述

package com.API1;

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class test_Zoneld {
    public static void main(String[] args) {
        //获取系统默认的时区
        ZoneId zoneld=ZoneId.systemDefault();
        System.out.println(zoneld.getId());
        System.out.println(zoneld);

        //获取JAVA支持的全部时区ID
        System.out.println(ZoneId.getAvailableZoneIds());

        //public static ZoneId of(String zoneId),把某个时区id封装成ZoneId对象
        ZoneId zoneid1=ZoneId.of("America/New_York");

        //ZonedDateTime:带时区的时间
        //获取某个时间的ZonedDateTime对象,即返回的是带纽约时区的时间
        ZonedDateTime now= ZonedDateTime.now(zoneid1);
        System.out.println(now);

        //获取系统默认时区的ZonedDateTime对象,即带上海时区的时间
        ZonedDateTime now2=ZonedDateTime.now();
        System.out.println(now2);

        //世界标准时间
        ZonedDateTime now1=ZonedDateTime.now(Clock.systemDefaultZone());
        System.out.println(now1);

        //也是返回一个新的ZonedDateTime对象
        System.out.println(now2.getMonth());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值