【Java·API】:有关日期时间类的简单记录

1、写在前面

今天文章的内容是:

  • 日期时间类

2、内容

2.1、Date类

(1) 介绍

java.util包提供了Date类来封装当前的日期时间,表示特定的时刻,精度为毫秒。

(2) 构造器

Date类提供两个构造函数来实例化Date对象。

  • Date( ): 使用系统当前的日期和时间来初始化Date对象,精度为毫秒。
  • Date(long millisec): 有参构造器中的参数表示从 1970 年 11 月 11 日 起的毫秒数。

(3) 常用方法

  • getTime():
  • 返回自此Date对象表示的1970年11月11日00:00:0000:00:00 GMTGMT以来的毫秒数。
  • setTime():
  • 将此Date对象设置为表示格林尼治标准时间19701970年11月11日00:00:0000:00:00之后的time毫秒的时间点。
  • after(Date date):
  • 判断当前对象是否在指定日期之后,结果返回true或false。
  • before(Date date):
  • 判断当前对象是否在指定日期之前,结果返回true或false。
  • equals(Object date):
  • 判断当前对象是否和指定日期相等,结果返回true或false。
  • toString( ):
  • 将Date对象转换为字符串。

(4) 举例

举个例子:

import java.util.Date; public class Test { public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d);
    }
} // 运行结果: // Thu Oct 06 11:30:30 CST 2022 

2.2、SimpleDateFormat类

(1) 介绍

一般的,我们会使用SimpleDateFormat类来格式化日期以及分析日期。也就是说,SimpleDateFormat类可以将Date对象或时间毫秒值格式化成我们喜欢的时间形式,我们也可以将字符串的时间形式解析成日期对象。另外的,SimpleDateFormat类允许我们使用自定义的日期时间格式来运行出结果。

小结,SimpleDateFormat类的主要功能:

  • 格式化:日期→文本
  • 解析:文本→日期

(2) 构造器

  • SimpleDateFormat(): 构造一个SimpleDateFormat对象,使用默认格式。
  • SimpleDateFormat(String pattern): 构造一个SimpleDateFormat对象,使用指定格式。

(3) 常用方法

常用模式字符:

  • y: 年
  • M: 月
  • d: 日
  • H: 时
  • m: 分
  • s: 秒
  • a: AM / PM
  • E: 星期
  • D: 一年中的第一天

常用方法如下:

  • format(Date date): 将日期格式化成日期/时间字符串
  • format(Object time): 将时间毫秒值格式化为日期/时间字符串
  • parse(String source): 解析给定的时间字符串并生成日期对象

(4) 举例

举个例子:

import java.text.SimpleDateFormat; import java.util.Date;
public class Test {
    public static void main(String[] args) { // 创建 SimpleDateFormat 对象 sdf SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a"); // 1\. 格式化日期对象 Date d = new Date(); String rs1 = sdf.format(d);
        System.out.println(rs1); // 2\. 格式化时间毫秒值 long time = System.currentTimeMillis(); String rs2 = sdf.format(time);
        System.out.println(rs2);
    }
} // 运行结果: // 2022年10月06日 11:40:30 星期四 下午 // 2022年10月06日 11:40:30 星期四 下午 

2.3、扩展

(1) 介绍

从JDK8开始,java.time包提供了新的日期和时间API,严格区分了时刻、本地日期、本地时间以及对日期时间的运行会更加方便。


(2) 举例

LocalDate类

举个例子:

// LocalDate可获取与当天相关的信息 import java.time.LocalDate; public class Test { public static void main(String[] args) { // 1\. 获取本地日期对象 LocalDate nowDate = LocalDate.now(); // 2\. 输出对象信息 System.out.println("今天是:" + nowDate); // 3\. 分别获取对象 年 月 日 信息 System.out.println("年份: " + nowDate.getYear());
        System.out.println("月份: " + nowDate.getMonthValue());
        System.out.println("日期: " + nowDate.getDayOfMonth()); // 4\. 获取星期信息 System.out.println("星期:" + nowDate.getDayOfWeek()); // 5\. 获取月份信息 System.out.println("月份:" + nowDate.getMonth()); // 6\. 查看当年的第几天 System.out.println("今天是这一年的第"+ nowDate.getDayOfYear() +"天"); // 7\. 查看当周的第几天 System.out.println("这周已经过了" + nowDate.getDayOfWeek().getValue() + "天"); // 8\. 创建指定日期对象 LocalDate ld = LocalDate.of(2002, 11, 11);
        System.out.println("我的生日:" + ld);
    }
}

输出结果:

今天是:2022-10-06
年份: 2022
月份: 10
日期: 6
星期:THURSDAY
月份:OCTOBER
今天是这一年的第279天
这周已经过了4天
我的生日:2002-06-22
  • 以下是一些增减日期的方法
方法名 说明
plusDays() 添加指定的天数
plusWeeks() 添加指定周数
plusMonths() 添加指定的月数
plusYears() 添加指定的年数
minusDays() 减去指定的天数
minusWeeks() 减去指定的周数
minusMonths() 减去指定的月数
minusYears() 减去指定的年数

备注:以上方法都将返回LocalDate对象的副本。

LocalTime类

import java.time.LocalTime; public class Test { public static void main(String[] args) { // 1\. 获取本地时间对象 LocalTime nowTime = LocalTime.now(); // 2\. 输出当前具体时间 System.out.println("当前时间:" + nowTime); // 3\. 输出 时 分 秒 纳秒 System.out.println("hour: " + nowTime.getHour());
        System.out.println("minute: " + nowTime.getMinute());
        System.out.println("second: " + nowTime.getSecond());
        System.out.println("nano: " + nowTime.getNano()); // 4\. 输出指定时间 System.out.println(LocalTime.of(11, 11)); // 时分 System.out.println(LocalTime.of(11, 11, 11)); // 时分秒 System.out.println(LocalTime.of(11, 11, 11, 111)); // 时分秒纳秒 }
}

输出结果:

当前时间:11:30:28.368 hour: 11 minute: 30 second: 28 nano: 368000000
11:11 11:11:11 11:11:11.000000111 

LocalDateTime类

import java.time.LocalDateTime; public class Test { public static void main(String[] args) { // 1\. 获取日期时间对象 LocalDateTime now = LocalDateTime.now(); // 2\. 输出日期时间对象 System.out.println("今天是:"+now);
        System.out.println("----------"); // 3\. 输出日期信息 System.out.println("年份: " + now.getYear());
        System.out.println("月份: " + now.getMonthValue());
        System.out.println("日期: " + now.getDayOfMonth());
        System.out.println("星期:" + now.getDayOfWeek());
        System.out.println("月份:" + now.getMonth());
        System.out.println("----------"); // 4\. 输出时间信息 System.out.println("hour: " + now.getHour());
        System.out.println("minute: " + now.getMinute());
        System.out.println("second: " + now.getSecond());
        System.out.println("nano: " + now.getNano());
    }
}

输出结果:

今天是:2022-10-06T11:30:40.189 ---------- 年份: 2022
月份: 10
日期: 6
星期:THURSDAY 月份:OCTOBER ---------- hour: 11 minute: 30 second: 40 nano: 189000000

可以说,LocalDateTime综合了LocalTime和LocalDate的相关方法,我们还可以将LocalDateTime转换成日期对象或时间对象,如下所示:

  • public LocalDate toLocalDate(): 将LocalDateTime对象转换成一个LocalDate对象
  • public LocalTime toLocalTime(): 将LocalDateTime对象转换成一个LocalTime对象

举个例子:

import java.time.LocalDateTime; public class Test { public static void main(String[] args) { // 1\. 获取日期时间对象 LocalDateTime now = LocalDateTime.now(); // 2\. 转换成日期对象 System.out.println("当前日期:" + now.toLocalDate()); // 3\. 转换成时间对象 System.out.println("当前时间:" + now.toLocalTime());
    }
}

输出结果:

当前日期:2022-10-06 当前时间:11:30:30.333 

DateTimeFormatter类

DateTimeFormatter类是一个新的日期时间格式化类。

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Test { public static void main(String[] args) { // 1\. 创建本地日期时间对象, 并输出对象内容 LocalDateTime now = LocalDateTime.now();
        System.out.println("格式化前:"+now); // 2\. 创建格式化器 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a"); // 3\. 开始格式化日期时间对象 System.out.println("格式化后:" + now.format(dtf)); // dtf.format(now)的效果也一样 }
}

输出结果:

格式化前:2022-10-06T11:30:30.866 格式化后:2022-10-06 11:30:30 星期四 下午

Period类

  • 利用Period类,我们可以来计算日期间隔,一般用于LocalDate对象之前的比较。

比如between方法的使用:

public static Period between (LocalDate startDateInclusive, LocalDate endDateExclusive) 

举个例子:

import java.time.LocalDate; import java.time.Period; public class Test { public static void main(String[] args) { // 1\. 创建当前日期对象 LocalDate now = LocalDate.now();
        System.out.println("今天日期: " + now); // 2\. 创建指定日期对象 LocalDate birthDate = LocalDate.of(2002, 6, 22);
        System.out.println("我的生日: " + birthDate); // 3\. 计算日期间隔 Period between = Period.between(birthDate, now);
        System.out.printf("我已经生活了%d年%d月%d天", between.getYears(),between.getMonths(),between.getDays());
    }
}

输出结果:

今天日期: 2022-10-06
我的生日: 2002-06-22
我已经生活了20年3月14天

Duration类

  • 利用Duration类,我们可以更加方便地计算两个时间之间的间隔。

比如between方法的使用,表示两个时间对象之间的持续时间:

public static Duration between (Temporal startInclusive, Temporal endExclusive) 

举个例子:

import java.time.Duration; import java.time.LocalDateTime; public class Test { public static void main(String[] args) { // 1\. 创建当前日期时间对象 LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时刻: " + now); // 2\. 创建指定日期时间对象 LocalDateTime birthDate = LocalDateTime.of(2002, 6, 22, 00, 00, 00);
        System.out.println("我的生日: " + birthDate); // 3\. 计算间隔时间 Duration between = Duration.between(birthDate, now);
        System.out.printf("我已经生活了%d天\n", between.toDays());
    }
}

输出结果:

当前时刻: 2022-10-06T11:30:30.268 我的生日: 2002-06-22T00:00 我已经生活了7411天
#Java##程序员#
全部评论
写的非常详细,感谢分享
点赞 回复 分享
发布于 2022-10-14 19:04 陕西

相关推荐

程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务