Java时间类(Date,Calendar,LocalDate,Period,Duration)

第一章.Date日期类

时间原点: 1970年1月1日0时0分0秒

构造:
  Date()  根据当前系统时间创建Date对象
  Date(long time) 根据指定的时间创建Date对象,传递的是毫秒值      

 public static void main(String[] args) {
        Date date1 = new Date();
        System.out.println("date1 = " + date1);

        System.out.println("================");

        Date date2 = new Date(1000L);
        System.out.println("date2 = " + date2);
    }

1.void setTime(long time) 设置时间,传递毫秒值
2.long getTime() 获取时间对应的毫秒值

public void method01() {
        Date date = new Date();
        //1.void setTime(long time) 设置时间,传递毫秒值
        date.setTime(1725676180363L);
        System.out.println(date);
        //2.long getTime() 获取时间对应的毫秒值
        long time = date.getTime();
        System.out.println(time);
    }

第二章.Calendar日历类

1.概述:日历类,是一个抽象类
2.获取:
  static Calendar getInstance()
3.注意:
  Calendar中的月份是从0开始的,所以我们需要+1处理

 int get(int field) ->返回给定日历字段的值
  void set(int field, int value)  :将给定的日历字段设置为指定的值
  void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去指定的时间量
  Date getTime():将Calendar转成Date对象

扩展方法:void set(int year, int month, int date)  ->直接设置年月日
需求:
定义一个变量表示年份,判断这个年份是平年还是闰年?
   

public void method(){
     //1.定义一个变量,表示年份 -> year
     int year = 2023;
     //2.创建Calendar对象,调用set方法设置年月日 -> set(year,2,1)-> 表示的是3月1号
     Calendar calendar = Calendar.getInstance();
     calendar.set(year,2,1);
     //3.将表示的3月1号减1天,获取到的就是2月最后一天
     calendar.add(Calendar.DATE,-1);

     int day = calendar.get(Calendar.DATE);
     //4.判断,如果最后一天为29号,证明是闰年,否则就是平年
     if (day==29){
         System.out.println("闰年");
     }else{
         System.out.println("平年");
     }
 }

第三章.SimpleDateFormat日期格式化类

作用:
  a.将Date对象按照指定的格式转成String输出
  b.将符合格式的String转成Date对象

SimpleDateFormat常用方法

1.String format(Date date) -> 将Date对象按照指定的格式格式化
2.Date parse(String time) -> 将符合指定日期格式的字符串转成Date对象

 public void format() throws ParseException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //1.String format(Date date) -> 将Date对象按照指定的格式格式化
        String time = sdf.format(date);
        System.out.println("time = " + time);
        //2.Date parse(String time) -> 将符合指定日期格式的字符串转成Date对象
        String time2 ="2000-10-10 10:10:10";
        Date date1 = sdf.parse(time2);
        System.out.println("date1 = " + date1);
    }

第四章.JDK8新日期类

1. LocalDate 本地日期

1.概述:LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日
2.作用:操作年月日
3.获取:
  static LocalDate now()  获取当前系统时间
  static LocalDate of(int year, int month, int dayOfMonth)  -> 获取指定的时间对象 

public static void main(String[] args) {
        //static LocalDate now()  获取当前系统时间
        LocalDate local1 = LocalDate.now();
        System.out.println("local1 = " + local1);
        //static LocalDate of(int year, int month, int dayOfMonth)  -> 获取指定的时间对象
        LocalDate local2 = LocalDate.of(2000, 10, 10);
        System.out.println("local2 = " + local2);
    }

1.概述:LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒
2.作用:操作精准时间
3.获取:
  static LocalDateTime now()  获取当前系统时间
  static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)  

public static void main(String[] args) {
        //static LocalDateTime now()  获取当前系统时间
        LocalDateTime local = LocalDateTime.now();
        System.out.println("local = " + local);
        //static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
        LocalDateTime local2 = LocalDateTime.of(2000, 1, 1, 1, 1, 1);
        System.out.println("local2 = " + local2);
    }

获取日期字段的方法 : 名字是get开头

int getYear()->获取年份
int getMonthValue()->获取月份
int getDayOfMonth()->获取月中的第几天

设置日期字段的方法 : 名字是with开头

LocalDate withYear(int year):设置年份
LocalDate withMonth(int month):设置月份
LocalDate withDayOfMonth(int day):设置月中的天数

日期字段偏移

设置日期字段的偏移量,方法名plus开头,向后偏移
设置日期字段的偏移量,方法名minus开头,向前偏移

2.Period和Duration类

Period 计算日期之间的偏差

 static Period between(LocalDate d1,LocalDate d2):计算两个日期之间的差值
  
  getYears()->获取相差的年
  getMonths()->获取相差的月
  getDays()->获取相差的天

public static void main(String[] args) {
        LocalDate local1 = LocalDate.of(2023, 10, 10);
        LocalDate local2 = LocalDate.of(2024, 11, 9);

        Period period = Period.between(local1, local2);

        //getYears()->获取相差的年
        System.out.println(period.getYears());
        //getMonths()->获取相差的月
        System.out.println(period.getMonths());
        //getDays()->获取相差的天
        System.out.println(period.getDays());
    }

这里返回的是两个日期之间总的时间的年月日

Duration计算时间之间的偏差

1.static Duration between(Temporal startInclusive, Temporal endExclusive)  -> 计算时间差
2.Temporal : 是一个接口
  实现类:LocalDate LocalDateTime
3.参数需要传递 Temporal 的实现类对象, 注意要传递LocalDateTime
  因为Duration计算精确时间偏差,所以需要传递能操作精确时间的 LocalDateTime
4.利用Duration获取相差的时分秒 -> to开头
  toDays() :获取相差天数
  toHours(): 获取相差小时
  toMinutes():获取相差分钟
  toMillis():获取相差秒(毫秒)

 public static void main(String[] args) {
        LocalDateTime local1 = LocalDateTime.of(2023, 10, 10, 10, 10, 10);
        LocalDateTime local2 = LocalDateTime.of(2024, 11, 11, 11, 11, 11);

        Duration duration = Duration.between(local1, local2);
        // toDays() :获取相差天数
        System.out.println(duration.toDays());
        // toHours(): 获取相差小时
        System.out.println(duration.toHours());
        // toMinutes():获取相差分钟
        System.out.println(duration.toMinutes());
        // toMillis():获取相差秒(毫秒)
        System.out.println(duration.toMillis());
    }

而这里返回的每一个都总时间差

3.DateTimeFormatter日期格式化类

1.获取:
  static DateTimeFormatter ofPattern(String pattern)   -> 获取对象,指定格式
2.方法:
  String format(TemporalAccessor temporal)-> 将日期对象按照指定的规则转成String 
                TemporalAccessor:接口,子接口有Temporal
                Temporal的实现类:LocalDate LocalDateTime    
                    
  TemporalAccessor parse(CharSequence text)-> 将符合规则的字符串转成日期对象 
                   如果想将TemporalAccessor转成我们常见的LocalDateTime日期对象,就需要用到LocalDateTime中的静态方法:
                   static LocalDateTime from(TemporalAccessor temporal)  

public static void main(String[] args) {
        //format();
        parse();
    }

    private static void parse() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = "2000-10-10 10:10:10";
        TemporalAccessor temporalAccessor = dtf.parse(time);
        System.out.println(temporalAccessor);

        LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
        System.out.println("localDateTime = " + localDateTime);
    }

    private static void format() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String time = dtf.format(localDateTime);
        System.out.println("time = " + time);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值