Date
在Java中,java.util.Date 类用于表示日期和时间。它以自1970年1月1日00:00:00 GMT 一来的毫秒数来存储日期和时间信息。
1、构造方法
标准基准时间(称为“历元(epoch)”:即1970年1月1日00:00:00 GMT。
tips: 由于中国处于东八区,所以中国的标准基准时间为08:00。
| 构造方法 | 说明 | 
|---|---|
| Date() | 创建Date对象,初始化时间为从运行程序的此时此刻到时间原点经历的毫秒值 | 
| Date(long millisec) | 创建Date对象,初始化时间为传递的毫秒值 | 
代码演示:
import java.util.Date;
public class Demo01Date {
    public static void main(String[] args) {
        // 创建日期对象,系统当前的时间
        System.out.println(new Date()); 
        // 创建日期对象,把当前的毫秒值转成日期对象
        System.out.println(new Date(0L)); 
    }
}2、常用方法
| 类型 | 方法名 | 说明 | 
|---|---|---|
| long | setTime(long time) | 修改当前对象的时间 | 
| long | getTime() | 返回自1970年1月1日00:00:00 GMT以来的毫秒数 | 
| public String | toString() | 将日期转换为字符串表示,形如Thu Jan 01 08:00:00 CST 1970 | 
代码演示:
public class DateDemo1 {
    public static void main(String[] args) {
        //在创建对象的时候也可以指定时间
        Date date1 = new Date(0L);
        long time = 0;
        time += 1000L * 60 * 60 * 24 *365;
        date1.setTime(time);
        System.out.println(date1);
    }
}
如果要计算和比较时间的话,都需要转换为毫秒值才可以。
SimpleDateFormat
SimpleDateFormat是日期/时间格式化类,可以实现在Date对象与String对象之间进行来回转换,主要是指定时间的格式。
1、构造方法
| 构造方法 | 说明 | 
|---|---|
| public SimpleDateFormat(String pattern) | 参数pattern是一个字符串,代表日期时间的自定义格式。 | 
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");常用的格式规则为:
| 标识字母(区分大小写) | 含义 | 
|---|---|
| y | 年 | 
| M | 月 | 
| d | 日 | 
| H | 时 | 
| m | 分 | 
| s | 秒 | 
2、常用方法
| 类型 | 方法名 | 说明 | 
|---|---|---|
| public String | format(Date date) | 将Date对象转换为指定格式的字符串 | 
| public Date | parse(String source) | 将字符串解析为Date对象 | 
需求:
             秒杀活动开始时间:2023年11月11日 0:0:0(毫秒值)
             秒杀活动结束时间:2023年11月11日 0:10:0(毫秒值)
            小贾下单并付款的时间为:2023年11月11日 0:01:0
             小皮下单并付款的时间为:2023年11月11日 0:11:0
             用代码说明这两位同学有没有参加上秒杀活动?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo2_case2 {
    public static void main(String[] args) throws ParseException {
        String startstr = "2023年11月11日 0:0:0";
        String endstr = "2023年11月11日 0:10:0";
        String order1str = "2023年11月11日 0:01:0";
        String order2str = "2023年11月11日 0:11:0";
        //以指定格式创建一个SimpleDateFormat对象
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //将字符串解析为Date对象,并获取毫秒值
        long starttime = simpleDateFormat1.parse(startstr).getTime();
        long endtime = simpleDateFormat1.parse(endstr).getTime();
        long order1time = simpleDateFormat1.parse(order1str).getTime();
        long order2time = simpleDateFormat1.parse(order2str).getTime();
        if ((order1time > starttime) && (order1time < endtime)) {
            System.out.println("小贾参加秒杀活动");
        } else {
            System.out.println("小贾没有参加秒杀活动");
        }
        if ((order2time > starttime) && (order2time < endtime)) {
            System.out.println("小皮参加秒杀活动");
        } else {
            System.out.println("小皮没有参加秒杀活动");
        }
    }
}Calendar
Calendar代表了与日历有关的类,可单独获取或者修改日历中的年月日。
Calebdar是一个抽象类,不能直接创建对象,可通过静态方法getInstance方法获取其子类的对象。
    Calendar calendar = Calendar.getInstance();常用方法
| 类型 | 方法 | 说明 | 
|---|---|---|
| public static | Calendar getInstance() | 获取一个它的子类GregorianCalendar对象。 | 
| public int | get(int filed) | 获取日历中某个字段的信息,返回值是数字 | 
| public void | set(int field, int value) | 修饰某个字段的信息 | 
| public void | add(int field, int amount) | 将某个字段的值减少amount | 
field参数与字段的对应规则:
| Calendar.YEAR | 年 | 
| Calendar.MONTH | 月 | 
| Calendar.DAY_OF_MONTH | 日 | 
| Calendar.HOUR | 小时 | 
| Calendar.MINUTE | 分钟 | 
| Calendar.SECOND | 秒 | 
| Calendar.DAY_OF_WEEK | 星期 | 
好像没看到获取指定时间的日历对象。
日历类中月份的范围为0~11,例如当值为0时表示的是一年中的第一个月,即一月份。
日历类中星期的特点:范围为1~7,星期日是一周中的第一天,例如当返回7时表示的是星期六。
    //查表法,查询星期几
    public static String getWeek(int w) {//w = 1 --- 7
        //做一个表(数组)
        String[] weekArray = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        //            索引      [0]      [1]       [2]      [3]       [4]      [5]      [6]
        //查表
        return weekArray[w - 1];
    }LocalDate / LocalTime / LocalDateTime
1、now/of
是使用类的静态方法,用来获取时间对象
以LocalDate类为例:
| 静态方法 | 说明 | 
|---|---|
| static LocalDate now() | 获取表示当前日期的LocalDate对象。 | 
| static LocalDate of(int year, int month, int day) | 获取具有指定年、月、日的LocalDate对象。 | 
  public void test1() {
        // now() 获取当前时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDate:" + localDate);
        System.out.println("localTime:" + localTime);
        System.out.println("localDateTime:" + localDateTime);
        // of 根据指定年月日时分秒创建指定的时间
        LocalDate customDate = LocalDate.of(2022, 5, 3);
        LocalTime customTime = LocalTime.of(21, 43, 0);
        LocalDateTime customDateTime = LocalDateTime.of(2022, 5, 3, 21, 43, 0);
        System.out.println("customDate:" + customDate);
        System.out.println("customTime:" + customTime);
        System.out.println("customDateTime:" + customDateTime);
    }结果:

2、 getXxx
get系列方法获取日期和时间中的属性值。
其中LocalDate只描述日期,LocalTime描述时间,LocalDateTime描述日期和时间,在使用方法的时候要注意。
| 类型 | 方法 | 说明 | 
|---|---|---|
| int | getYear() | 年 | 
| Month | getMonth() | 月 | 
| int | getDayOfMonth() | 日 | 
| int | getHour() | 时 | 
| int | getMinute() | 分 | 
| int | getSecond() | 秒 | 
| DayOfWeek | getDayOfWeek() | 星期几 | 
public void test2() {
        // 获取时间参数的年、月、日(
        System.out.println("获取时间参数的年、月、日:");
        LocalDateTime param = LocalDateTime.now();
        System.out.println("year:" + param.getYear());
        System.out.println("month:" + param.getMonth());
        System.out.println("day:" + param.getDayOfMonth());
        System.out.println("hour:" + param.getHour());
        System.out.println("minute:" + param.getMinute());
        System.out.println("second:" + param.getSecond() + "\n");
}输出:
 
 
3、isXxx
is开头的方法表示判断。
| 类型 | 方法 | 说明 | 
|---|---|---|
| boolean | isAfter(LocalDate date) | 判断一个日期是否在另一个日期的后面 | 
| boolean | isBefore(LocalDate date) | 判断一个日期是否在另一个日期的前面 | 
| boolean | isEquals() | 判断两个日期是否相同 | 
public class test3 {
    public static void main(String[] args) {
        //获取当前对象
        LocalDate time1 = LocalDate.now();
        System.out.println(time1);    //2024-07-13
        //获取指定时间对象
        LocalDate time2 = LocalDate.of(2024, 12, 26);
        System.out.println(time2);      //2024-12-26
        //比较两个LocalDate
        boolean after = time1.isAfter(time2);     //false
        System.out.println(after);
        boolean before = time1.isBefore(time2);       //true
        System.out.println(before);
    }
}输出:
 
 
4、plus/minus
增加减少时间
细节:JDK8新增的时间对象都是不可变的,如果我们修改了时间,那么调用者不会改变,而是产生一个新的时间对象。
public class test4 {
    public static void main(String[] args) {
        //增加时间量的方法 plusXXX系类的方法 返回的是一个新的日期对象
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.getYear());
        //可以给当前的日期增加时间量
        LocalDateTime newDate= now.plusYears(1);
        int year = newDate.getYear();
        System.out.println(year);
        System.out.println("================================");
        //减去时间量的方法minusXXX 系列的方法 返回的是一个新的日期对象
        LocalDate now1 = LocalDate.now();
        System.out.println(now1.getDayOfMonth());
        LocalDate newDate2 = now1.minusDays(10);
        int dayOfMonth = newDate2.getDayOfMonth();
        System.out.println(dayOfMonth);
    }
}
输出:
 
5、with
修改时间
public class test5 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("today:" + now);
        // 修改日期为本月的某天
        LocalDateTime newDate = now.withYear(2025);
        System.out.println("newDate:" + newDate);
    }
}输出:
 
 
ZoneId
常用方法
| 类型 | 方法 | 说明 | 
|---|---|---|
| static Set<string> | getAvailableZoneIds() | 获取所有时区 | 
| static ZoneId | systemDefault() | 获取系统默认时区 | 
| static Zoneld | of(string zoneld) | 获取一个指定时区 | 
Instant
用于表示时间戳的类。时间戳是距离1970年1月1日UTC时间的秒数或纳秒数。
- Instant类提供了一组方法,用于处理和操作时间戳。它可以表示从1970年1月1日起的精确时间,以纳秒为单位。Instant类是不可变的,也就是说,一旦创建了一个Instant对象,就不能修改它的值。
- 通过Instant类,可以获取当前的系统时间戳,也可以根据指定的时间戳值创建Instant对象。它还支持与其他日期和时间类的相互转换,可以将Instant对象转换为特定时区下的日期和时间,或者将特定时区下的日期和时间转换为Instant对象。
需要注意的是,Instant类是与计算机系统的时间概念相关的,它与时区无关,并以协调世界时(Coordinated Universal Time,UTC)为基准。
常用方法
静态方法:用于获取Instant对象。
| 静态方法 | 描述 | 
|---|---|
| static now() | 获取当前标准时间的Instant对象 | 
| static ofEpochMilli(long epochMilli) | 根据指定的毫秒数获取Instant对象 | 
| static ofEpochSecond(long epochSecond) | 根据指定的秒数获取Instant对象 | 
| static ofEpochSecond(long epochSecond, long nanoAdjustment) | 根据指定的秒数和纳秒数获取Instant对象 | 
实例方法:用于处理和操作时间戳。
| 类型 | 方法 | 说明 | 
|---|---|---|
| ZonedDateTime | atZone(ZoneId zone) | 将Instant对象转换为指定时区的ZonedDateTime对象 | 
| boolean | isXxx(Instant otherInstant) | 判断当前Instant对象是否满足指定条件 | 
| Instant | minusXxx(long millisToSubtract) | 从当前Instant对象减去指定的时间间隔 | 
| Instant | plusXxx(long millisToAdd) | 在当前Instant对象上增加指定的时间间隔 | 
ZoneDateTime
ZonedDateTime类:带时区的时间。
常用方法
静态方法:用于获取ZonedDateTime对象。
实例方法:用于处理和操作时间。
| 类型 | 方法 | 说明 | 
|---|---|---|
| ZonedDateTime | withXxx(时间) | 修改年、月、日、时、分、秒等,并返回一个新的ZonedDateTime对象。 | 
| ZonedDateTime | minusXxx(时间) | 减少年、月、日、时、分、秒等,并返回一个新的ZonedDateTime对象。 | 
| ZonedDateTime | plusXxx(时间) | 增加年、月、日、时、分、秒等,并返回一个新的ZonedDateTime对象。 | 
注意:上述"时间"指的是一定的时间量,可以是年、月、日、时、分、秒等。具体的时间量可根据具体方法而定,例如withYear(int year)修改年份,minusHours(long hours)减少小时数,plusMinutes(long minutes)增加分钟数等。
Duration
用于计算两个“时间”间隔的类。
静态方法 between():计算两个时间的间隔,默认是秒
Period
用于计算两个“日期”间隔的类。
静态方法 between():计算两个日期之间的间隔
3. ChronoUnit类
ChronoUnit类是Java中的一个枚举类,用于表示日期和时间的不同单位。
DateTimeFormatter
解析和格式化日期或时间的类。
常用方法
| 类型 | 方法 | 说明 | 
|---|---|---|
| static | ofPattern("yyyy-MM-dd") | 通过给定格式获取对象 | 
| public String | format() | 把一个日期对象的默认格式 格式化成指定的格式的字符串 | 
public class test6 {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //必须得为1999-09-21,不能是1999-9-21
        LocalDate localDate = LocalDate.parse("2022-05-03", formatter);
        System.out.println("localDate:" + localDate);
        String format1 = formatter.format(LocalDate.now());
        System.out.println("format1:"+format1);
    }
}输出:
 
 







![【代码随想录】【算法训练营】【第66天】 [卡码95]城市间货物运输II [卡码96]城市间货物运输III](https://i-blog.csdnimg.cn/direct/7564fefb43424f40b8f10ee7505efd0b.png)











