Java中常见的日期类、时间类的常用方法

Java中常用的日期、时间相关类

DateTimeFormatter用于时间的格式化和解析
Date日期类
SimpleDateFormat简单日期格式化
Calendar日历类
LocalDate年、月、日
LocalTime时、分、秒
LocalDateTime年、月、日、时、分、秒

Date类

Date对象记录的时间是用毫秒值来表示的。Java语言规定,1970年1月1日0时0分0秒认为是时间的起点,此时记作0,那么1000(1秒=1000毫秒)就表示1970年1月1日0时0分1秒,依次内推。

构造器和常见方法
在这里插入图片描述

SimpleDateFormat类

  • Date对象转换为指定格式的日期字符串这个操作,叫做日期格式化,
  • 反过来把指定格式的日期符串转换为Date对象的操作,叫做日期解析。

构造器和常见方法
在这里插入图片描述

Calendar类

alendar类表示日历,它提供了一些比Date类更好用的方法。
在这里插入图片描述
注意calendar是可变对象,一旦做出修改会使自身时间发生变化。

public class Test4Calendar {
        public static void main(String[] args) {
        //1:获取当前系统的日历对象
        Calendar now = Calendar.getInstance();
        System.out.println(now);
        //2:获取 日历中的 年
        int year = now.get(Calendar.YEAR);
//        int year = now.get(1);
        System.out.println(year);
        //获取 月中日
        int day1 = now.get(Calendar.DAY_OF_YEAR);//年中日
        int day2 = now.get(Calendar.DAY_OF_MONTH);//月中日
        int day3 = now.get(Calendar.DATE);// 日--月中日
        System.out.println(day1);//214
        System.out.println(day2);//2
        System.out.println(day3);//2
        System.out.println(now.get(Calendar.DAY_OF_WEEK));//周中日

        //3:将 日历对象转换日期对象
        Date date = now.getTime();
        System.out.println(date);

        //4:获取日历对象的毫秒值
        long time = now.getTimeInMillis();
        System.out.println(time);

        // 5:可以修改 指定日历信息
        now.set(Calendar.YEAR,2028);
        //月份改查10月
        now.set(Calendar.MONTH,9);//9是10   11是12  0 是 1
        System.out.println(now);

        // 2028年  10月
        //偏移
        now.add(Calendar.YEAR,-2000);// 对指定的日历信息 进行偏移 负数 左偏移  正数 右偏移
        System.out.println(now);

        now.set(2011,10,11);//设置指定的日历信息
        System.out.println(now);

    }
}

LocalDate、LocalTime、LocalDateTime(三兄弟)

  • LocalDate:代表本地日期(年、月、日、星期)
  • LocalTime:代表本地时间(时、分、秒、纳秒)
  • LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)
  • 转换相关的API
    LocalDate ld = ldt.toLocalDate();
    LocalTime lt = ldt.toLocalTime();
    LocalDateTime ldt10 = LocalDateTime.of(ld, lt);

LocalDate (年月日)

package com.itheima.new_three_bro;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;

public class LocalDateDemo {

    public static void main(String[] args) {
        //1.获取当前时间的日历对象(包含 年月日)
        LocalDate nowDate = LocalDate.now();
        System.out.println("今天的日期:" + nowDate);
       //2.获取指定的时间的日历对象
        LocalDate ldDate = LocalDate.of(2004, 2, 20);
        System.out.println("指定日期:" + ldDate);

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

     //3.get系列方法获取日历中的每一个属性值//获取年
        int year = ldDate.getYear();
        System.out.println("year: " + year);
       //获取月//方式一:
        Month m = ldDate.getMonth();
        System.out.println(m);
        System.out.println(m.getValue());

        //方式二:
        int month = ldDate.getMonthValue();
        System.out.println("month: " + month);

        //获取日
        int day = ldDate.getDayOfMonth();
        System.out.println("day:" + day);

        //获取一年的第几天
        int dayofYear = ldDate.getDayOfYear();
        System.out.println("dayOfYear:" + dayofYear);

        //获取星期
        DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
        System.out.println(dayOfWeek);
        System.out.println(dayOfWeek.getValue());

      //is开头的方法表示判断
        System.out.println("是不是在当前时间之前:"+ldDate.isBefore(nowDate));
        System.out.println("是不是在当前时间之后:"+ldDate.isAfter(nowDate));

       //with开头的方法表示修改,只能修改年月日
        LocalDate withLocalDate = ldDate.withYear(2000);//修改得到是新的对象 原对象不变
        System.out.println("修改年之后新的时间:"+withLocalDate);

        //minus开头的方法表示减少,只能减少年月日
        LocalDate minusLocalDate = ldDate.minusYears(1);
        System.out.println("减少1年之后"+minusLocalDate);


        //plus开头的方法表示增加,只能增加年月日
        LocalDate plusLocalDate = ldDate.plusDays(1);
        System.out.println("增加一天之后:"+plusLocalDate);

    //-------------
   // 判断今天是否是你的生日
        LocalDate birDate = LocalDate.of(2000, 2, 3);
        LocalDate nowDate1 = LocalDate.now();

        //  扩展 年月日对象 月中的天对象  9月28日
        MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
        MonthDay nowMd = MonthDay.from(nowDate1);// 2 3

        System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗?
    }
}

LocalTime (时分秒)

package com.itheima.new_three_bro;

import java.time.LocalTime;

public class LocalTimeDemo {

    public static void main(String[] args) {
        // 获取本地时间的日历对象。(包含 时分秒)  精确到纳秒
        LocalTime nowTime = LocalTime.now();
        System.out.println("今天的时间:" + nowTime);

        int hour = nowTime.getHour();//时
        System.out.println("hour: " + hour);

        int minute = nowTime.getMinute();//分
        System.out.println("minute: " + minute);

        int second = nowTime.getSecond();//秒
        System.out.println("second:" + second);

        int nano = nowTime.getNano();//纳秒
        System.out.println("nano:" + nano);
        System.out.println("------------------------------------");
        System.out.println(LocalTime.of(8, 20));//时分
        System.out.println(LocalTime.of(8, 20, 30));//时分秒
        System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
        LocalTime mTime = LocalTime.of(8, 20, 30, 150);

//is系列的方法
        System.out.println("当前时分秒 是不是 在 mTime 之前:"+nowTime.isBefore(mTime));
        System.out.println("当前时分秒 是不是 在 mTime 之后:"+nowTime.isAfter(mTime));

//with系列的方法,只能修改时、分、秒
        System.out.println(nowTime.withHour(10));

//plus系列的方法,只能修改时、分、秒
        System.out.println(nowTime.plusHours(10));
    }
}

LocalDateTime (年月日时分秒)

package com.itheima.new_three_bro;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateTimeDemo {

    public static void main(String[] args) {
        // 当前时间的的日历对象(包含年月日时分秒)
        LocalDateTime nowDateTime = LocalDateTime.now();//获取 年月日时分秒 精确到纳秒

        System.out.println("今天是:" + nowDateTime);//今天是:今天是:2024-02-03T11:32:25.514610600
        System.out.println(nowDateTime.getYear());//年 2024
        System.out.println(nowDateTime.getMonthValue());//月
        System.out.println(nowDateTime.getDayOfMonth());//日
        System.out.println(nowDateTime.getHour());//时
        System.out.println(nowDateTime.getMinute());//分
        System.out.println(nowDateTime.getSecond());//秒
        System.out.println(nowDateTime.getNano());//纳秒
// 日:当年的第几天
        System.out.println("dayofYear:" + nowDateTime.getDayOfYear());
//星期
        System.out.println(nowDateTime.getDayOfWeek());
        System.out.println(nowDateTime.getDayOfWeek().getValue());
//月份
        System.out.println(nowDateTime.getMonth());
        System.out.println(nowDateTime.getMonth().getValue());

        //  把  年月日 时分秒   可以把它 拆成 LocalDate  LocalTime
        LocalDate ld = nowDateTime.toLocalDate();
        System.out.println(ld);

        LocalTime lt = nowDateTime.toLocalTime();
        System.out.println(lt.getHour());
        System.out.println(lt.getMinute());
        System.out.println(lt.getSecond());
    }
}

线程安全问题*

主要是传统的时期处理类Date、Calendar不是多线程安全的,
而LocalDate 线程安全的,所以不用担心并发问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一条小传传

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

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

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

打赏作者

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

抵扣说明:

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

余额充值