java.util.Date 是从 JDK 1.0 开始提供,易用性差
JDK 1.1 废弃了 Date 中很多方法,新增了并建议使用 java.util.Calendar 类
为了解决 JDK 中时间与日期较难使用的问题,JDK 1.8 开始,吸收了 Joda-Time 很多功能,新增 java.time 包,加了新特性:
LocalDate 本地日期
LocalTime 本地时间
LocalDateTime 本地日期+时间
Instant 时间戳,适合机器时间计算
Duration 时间差
Period 年、月、日差
ZoneOffset 时区偏移量
ZonedDateTime 带时区的日期时间
Clock 时钟,获取其他地区时钟
DateTimeFormatter 时间格式化
Temporal 日期-时间获取值的字段
TemporalAdjuster emporal 对象转换,实现自定义
ChronoLocalDate 日历系统接口
获取当前日期
LocalDate.now()
创建日期
LocalDate date = LocalDate.of(2020, 9, 21)
获取年份
date.getYear()
//通过 TemporalField 接口的实现枚举类 ChronoField.YEAR 获取年份
date.get(ChronoField.YEAR)
获取月份
date.getMonth().getValue()
//通过 TemporalField 接口的实现枚举类 ChronoField.MONTH_OF_YEAR 获取月份
date.get(ChronoField.MONTH_OF_YEAR)
获取日
date.getDayOfMonth()
//通过 TemporalField 接口的实现枚举类 ChronoField.DAY_OF_MONTH 获取日
date.get(ChronoField.DAY_OF_MONTH)
获取周几
date.getDayOfWeek()
获取当前月多少天
date.lengthOfMonth()
获取当前年是否为闰年
date.isLeapYear()
当前时间
LocalTime nowTime = LocalTime.now()
10、创建时间
LocalTime.of(23, 59, 59)
11、获取时
nowTime.getHour()
12、获取分
nowTime.getMinute()
13、获取秒
nowTime.getSecond()
14、获取毫秒
nowTime.getLong(ChronoField.MILLI_OF_SECOND)
15、获取纳秒
nowTime.getNano()
16、创建日期时间对象
LocalDateTime.of(2020, 9, 21, 1, 2, 3);
LocalDateTime.of(date, nowTime);
17、获取当前日期时间对象
LocalDateTime.now()
18、通过 LocalDate 创建日期时间对象
date.atTime(1, 2, 3)
19、通过 LocalTime 创建日期时间对象
nowTime.atDate(date)
20、通过 LocalDateTime 获取 LocalDate 对象
LocalDateTime.now().toLocalDate()
21、通过 LocalDateTime 获取 LocalTime 对象
LocalDateTime.now().toLocalTime()
22、解析日期字符串
LocalDate.parse("2020-09-21")
23、解析时间字符串
LocalTime.parse("01:02:03")
24、解析日期时间字符串
LocalDateTime.parse("2020-09-21T01:02:03", DateTimeFormatter.ISO_LOCAL_DATE_TIME)
25、方便时间建模、机器处理的时间处理类 Instant,起始时间 1970-01-01 00:00:00
//起始时间 + 3 秒
Instant.ofEpochSecond(3)
//起始时间 + 3 秒 + 100 万纳秒
Instant.ofEpochSecond(3, 1_000_000_000)
//起始时间 + 3 秒 - 100 万纳秒
Instant.ofEpochSecond(3, -1_000_000_000))
//距离 1970-01-01 00:00:00 毫秒数
Instant.now().toEpochMilli()
26、Duration:LocalTime、LocalDateTime、Intant 的时间差处理
Duration.between(LocalTime.parse("01:02:03"), LocalTime.parse("02:03:04"))
Duration.between(LocalDateTime.parse("2020-09-21T01:02:03"), LocalDateTime.parse("2020-09-22T02:03:04"))
Duration.between(Instant.ofEpochMilli(1600623455080L), Instant.now())
27、日期时间,前、后、相等比较
//2020-09-21 在 2020-09-18 前?
LocalDate.parse("2020-09-21").isBefore(LocalDate.parse("2020-09-18"))
//01:02:03 在 02:03:04 后?
LocalTime.parse("01:02:03").isAfter(LocalTime.parse("02:03:04"))
28、修改日期、时间对象,返回副本
//修改日期返回副本
LocalDate.now().withYear(2019).withMonth(9).withDayOfMonth(9)
LocalDate date4Cal = LocalDate.now();
//加一周
date4Cal.plusWeeks(1)
//减两个月
date4Cal.minusMonths(2)
//减三年
date4Cal.minusYears(3)
29、格式化
//格式化当前日期
LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
//指定格式,格式化当前日期
LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
指定格式,格式化当前日期时间
//格式化当前日期时间
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"))
30、解析
//日期解析
LocalDate.parse("2020-09-20")
//指定格式,日期解析
LocalDate.parse("2020/09/20", DateTimeFormatter.ofPattern("yyyy/MM/dd"))
//指定格式,日期时间解析
LocalDateTime.parse("2020/09/20 01:01:03", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))
31、时区
//上海时区
ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
//设置日期为上海时区
LocalDate.now().atStartOfDay(shanghaiZone)
//设置日期时间为上海时区
LocalDateTime.now().atZone(shanghaiZone)
//设置 Instant 为上海时区
Instant.now().atZone(shanghaiZone)
32、子午线时间差
//时间差减 1 小时
ZoneOffset offset = ZoneOffset.of("-01:00");
//设置时间差
OffsetDateTime.of(LocalDateTime.now(), offset
java.time.DayOfWeek的getDisplayName()方法是Java中的一个内置函数,它根据指定的Locale类参数和TextStyle返回day-of-week的文本表示形式。 TextStyle定义了三个元素“ FULL”,“ SHORT”和“ NARROW”。语言环境类别代表世界上特定的语言和地区。
方法声明:
public String getDisplayName(TextStyle style, Locale locale)
用法:
String text = dayOfWeekObject.getDisplayName(TextStyle style, Locale locale)
参数:此方法有两个参数:
返回值:该函数返回根据指定的Locale类参数和TextStyle返回day-of-week的文本表示形式。
以下示例程序旨在说明上述方法:
// Java Program Demonstrate getDisplayName()
// method of DayOfWeek
import java.time.*;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
// Get textual representation of the
// day-of-week in FULL style
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
// Get textual representation of the
// day-of-week in SHORT style
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
// Get textual representation of the
// day-of-week in NARROW style
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
// Printing the textual names of the day-of-week
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
输出
Monday
Mon
M
// Java Program Demonstrate getDisplayName()
// method of DayOfWeek
import java.time.*;
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
// Get textual representation of the
// day-of-week in FULL style
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
// Get textual representation of the
// day-of-week in SHORT style
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
// Get textual representation of the
// day-of-week in NARROW style
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
// Printing the textual names of the day-of-week
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
输出
Wednesday
Wed
W