System.currentTimeMillis()
(java.util包下)
Date表示当前日期和时间的日期对象
Calendar日历
TimeZone该类表示时区偏移量
SimpleDateFormat格式化Date,建议使用jdk8后的DateTimeFormatter
(java.time包下)
TemporalUnit时间单位这个概念,这个是个接口,唯一的实现是枚举类ChronoUnit
TemporalAmount时间数额时间范围这个概念,这个是接口,主要实现类是Period,Duration
TemporalAccessor时间辅助存储(直接使用很少,这个类也是个接口)
TemporalField时间域,时间字段(直接使用很少,这个类也是个接口)
TemporalAdjuster时间调节器
Clock(时钟)
Instant(时间点,和中国时区相差8小时)
ChronoUnit
LocalDate(年月日)
LocalTime(时分秒)
LocalDateTime(年月日时分秒)
DateTimeFormatter(日期时间格式工具类)
Year(年)
Month(月)
YearMonth(年月)
MonthDay(月日)
时间差值计算工具下
Period(年月日)
Duration(秒,纳秒)
ChronoUnit计算时间差单位
until方法
TemporalAdjusters时间调节器工具类(返回时间调节器)
带时差时区信息的时间
ZonedDateTime带时区(城市)的日期和时间
OffsetDateTime带时区(时差)的日期和时间
ZoneId(时区)
ZoneOffset(时差)
OffsetTime
一 、 System.currentTimeMillis()
返回Unix时间戳(毫秒) ,从unix时间元年1970-01-01 00:00:00 000 到现在的毫秒数。
(java.util包下,详细介绍)
二 、 Date 表示当前日期和时间的日期对象
Date date = new Date();
Date date1 = new Date(123+1900, 0, 1, 8, 21, 56 );
year – the year minus 1900.
month – the month between 0-11.
date – the day of the month between 1-31.
hrs – the hours between 0-23.
min – the minutes between 0-59.
sec – the seconds between 0-59.
把date转Instant
date.toInstant()
建议使用jdk8后的Instant来替代。
三 、 Calendar 日历工具
四 、 TimeZone 该类表示时区偏移量
五 、 SimpleDateFormat(在java.text包中)
主要用于jdk8以前的date的格式化
(java.time包下,详细介绍)
TemporalUnit时间单位这个概念,这个是个接口,唯一的实现是枚举类ChronoUnit
TemporalAmount时间数额时间范围这个概念,这个是接口,主要实现类是Period,Duration
TemporalAccessor时间辅助存储(直接使用很少,这个类也是个接口)
TemporalField时间域,时间字段(直接使用很少,这个类也是个接口)
TemporalAdjuster时间调节器
六 、Clock(时钟)
Clock.systemDefaultZone(); 返回一个Clock对象,基于当前默认时区
Clock.systemUTC();返回一个Clock对象,基于UTC时区,(ZoneOffset.UTC),
七 、 Instant(瞬时,时间点,和中国时区相差8小时)
它简单表示自1970年1月1日0时0分0秒(UTC)开始的秒数,当然精度可以到纳秒。
now(); 静态方法,返回默认UTC时区的Instant类的对象
ofEpochMilli(long epochMilli); 静态方法,返回在1970-01-01 00:00:00基础之上的指定毫秒数之后的Instant类对象
atOffset(ZoneOffset offset); 结合偏移来创建一个OffsetDateTime
toEpochMilli(); 返回1970-01-01 00:00:00到当前时间的毫秒数,即时间戳
八 、 LocalDate(年月日)
LocalDate.now();
LocalDate.now(ZoneId.of("Asia/Tokyo"));
LocalDate.now(Clock); 这里的Clock也包含了时区信息
LocalDate.of(int year, int month, int dayOfMonth);
LocalDate.of(int year, int month, int dayOfMonth);
其他同样是常见的加减,of,parse等。
九 、 LocalTime(时分秒)
LocalTime.now();
LocalTime.now(ZoneId.of("Asia/Tokyo"));
LocalTime.now(Clock); 这里的Clock也包含了时区信息
LocalDateTime.of(int hour, int minute, int seconed);
LocalDateTime.of(int hour, int minute, int seconed, nonaOfSeconed);
其他都是常见的加减,of,parse等。
十 、 LocalDateTime(年月日时分秒)
LocalDateTime包含LocalDate,LocalTime;
它还可以基于Instant瞬时时间点 + 偏移时区 来构建
LocalDateTime.now();
LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
LocalDateTime.now(Clock); 这里的Clock也包含了时区信息
LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int seconed);
LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int seconed, nonaOfSeconed);
LocalDateTime转换成Instant时需要提供对应的时区偏移信息
localDateTime.toInstant(ZoneOffset.of("+08:00:00"));
十一 、 DateTimeFormatter(日期时间格式工具类)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
//本地化相关的格式。如:ofLocalizedDateTime()
//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//本地化相关的格式。如:ofLocalizedDate()
//FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
getLocate(), withLocate(), getZone(), withZone() 或者formatter的本地信息和ZoneId信息。
十二 、 Year
Year.now(); 返回Year对象
Year(2022);
of,from,parse等来获取
需要说明的是LocalDate.getYear()返回的是一个int格式。
十三 、 Month
这个是个表示月份的枚举类对象;1-12,分别表示1-12月,内部使用了-1
public static Month of(int month) {
if (month < 1 || month > 12) {
throw new DateTimeException("Invalid value for MonthOfYear: " + month);
}
return ENUMS[month - 1];
}
public enum Month implements TemporalAccessor, TemporalAdjuster {
/**
* The singleton instance for the month of January with 31 days.
* This has the numeric value of {@code 1}.
*/
JANUARY,
/**
* The singleton instance for the month of February with 28 days, or 29 in a leap year.
* This has the numeric value of {@code 2}.
*/
FEBRUARY,
/**
* The singleton instance for the month of March with 31 days.
* This has the numeric value of {@code 3}.
*/
MARCH,
/**
* The singleton instance for the month of April with 30 days.
* This has the numeric value of {@code 4}.
*/
APRIL,
/**
* The singleton instance for the month of May with 31 days.
* This has the numeric value of {@code 5}.
*/
MAY,
/**
* The singleton instance for the month of June with 30 days.
* This has the numeric value of {@code 6}.
*/
JUNE,
/**
* The singleton instance for the month of July with 31 days.
* This has the numeric value of {@code 7}.
*/
JULY,
/**
* The singleton instance for the month of August with 31 days.
* This has the numeric value of {@code 8}.
*/
AUGUST,
/**
* The singleton instance for the month of September with 30 days.
* This has the numeric value of {@code 9}.
*/
SEPTEMBER,
/**
* The singleton instance for the month of October with 31 days.
* This has the numeric value of {@code 10}.
*/
OCTOBER,
/**
* The singleton instance for the month of November with 30 days.
* This has the numeric value of {@code 11}.
*/
NOVEMBER,
/**
* The singleton instance for the month of December with 31 days.
* This has the numeric value of {@code 12}.
*/
DECEMBER;
。。。。。。。。。。。。。
}
Month.of(1);表示1月
Month.of(12);表示12月
同样需要说localDate.getMonth()获取是Month对象,getMonthValue()获取的才是对应的值
十四 、 YearMonth(年月)
year – the year to represent, validated from MIN_YEAR to MAX_YEAR
month – the month-of-year to represent, validated from 1 (January) to 12 (December)
他的构建主要来自now,of,parse.from等函数生成。
他的格式化是:uuuu-MM
Obtains an instance of YearMonth from a text string such as 2007-12.
The string must represent a valid year-month. The format must be uuuu-MM. Years outside the range
FIELD_MAP.put('y', ChronoField.YEAR_OF_ERA); // SDF, LDML
FIELD_MAP.put('u', ChronoField.YEAR); // LDML (different in SDF)
十五 、 MonthDay(月日)
生日,信用卡账单等,都可能用到该单位
他的构建生成主要来自now,of,from,parse等函数
month – the month-of-year to represent, validated from 1 to 12
dayOfMonth – the day-of-month to represent, validated from 1 to 29-31
他的格式化是:--MM-dd
Obtains an instance of MonthDay from a text string such as --12-03.
The string must represent a valid month-day. The format is --MM-dd.
时间差值计算工具下,详细介绍
十六 、 Period(年月日)
Period可以应用于存储两个日期之间的日期差,存储年月日(比如相差3年2个月21天)
public static void main(String[] args) {
LocalDate startTime = LocalDate.now();
System.err.println("startTime : " + startTime);
LocalDate endTime = LocalDate.now().plusMonths(18);
System.err.println("endTime : " + endTime);
Period p = Period.between(startTime, endTime);
System.err.printf("时间间隔 : %d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());
}
运行结果:
startTime : 2022-05-12
endTime : 2023-11-12
时间间隔 : 1 年 6 月 0 日
十七 、 Duration(秒,纳秒)
Duraction可以应用于存储两个时间之间的时间差
可以存储(日,时,分,秒)也可以理解(几天几小时几分钟几秒钟)。但他可以把这个时间差,转换成某个具体单位的值,如多分钟,多少秒钟,多少小时,或者多少天;
LocalDateTime start = LocalDateTime.of(2022,1,1,8,0,0);
LocalDateTime end = LocalDateTime.of(2022,1,2,8,30,30);
Duration duration = Duration.between(start, end);
parse(“PnDTnHnMn.nS”)
Duration duration = Duration.parse("PnDTnHnMn.nS");
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
Duration fromChar2 = Duration.parse("PT10M");
格式说明
采用ISO-8601时间格式。格式为:PnYnMnDTnHnMnS (n为个数)
例如:P1Y2M10DT2H30M15.03S
P:开始标记
1Y:一年
2M:两个月
10D:十天
T:日期和时间的分割标记
2H:两个小时
30M:三十分钟
15S:15.02秒
Examples:
"PT20.345S" -- parses as "20.345 seconds"
"PT15M" -- parses as "15 minutes" (where a minute is 60 seconds)
"PT10H" -- parses as "10 hours" (where an hour is 3600 seconds)
"P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
"P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
"P-6H3M" -- parses as "-6 hours and +3 minutes"
"-P6H3M" -- parses as "-6 hours and -3 minutes"
"-P-6H+3M" -- parses as "+6 hours and -3 minutes"
Duration.of(long amount, TemporalUnit unit)
Duration duration = Duration.of(2, ChronoUnit.DAYS);
Duration duration = Duration.ofDays(2);
类似方法:ofDays、ofHours、ofMinutes、ofSeconds、ofMillis、ofNanos
//纳秒
NANOS("Nanos", Duration.ofNanos(1)),
//微秒
MICROS("Micros", Duration.ofNanos(1000)),
//毫秒
MILLIS("Millis", Duration.ofNanos(1000_000)),
//秒
SECONDS("Seconds", Duration.ofSeconds(1)),
//分
MINUTES("Minutes", Duration.ofSeconds(60)),
//小时
HOURS("Hours", Duration.ofSeconds(3600)),
//半天
HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
//天
DAYS("Days", Duration.ofSeconds(86400)),
//周
WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
//月
MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
//年
YEARS("Years", Duration.ofSeconds(31556952L)),
//十年
DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
//世纪(百年)
CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
//千年
MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
//纪元(好多年)
ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));
十八 、 ChronoUnit计算时间差单位
ChronoUnit类计算有年、月、周、日、时、分、秒、毫秒
计算的是LocalDate和LocalDateTime两个时间分别间隔的年、月、周、日、时、分、秒、毫秒
public static void main(String[] args) {
LocalDateTime startTime = LocalDateTime.now();
System.err.println("startTime : " + startTime);
LocalDateTime endTime = LocalDateTime.now().plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
System.err.println("endTime : " + endTime);
long years = ChronoUnit.YEARS.between(startTime, endTime);
System.err.println("年 "+years);
long months = ChronoUnit.MONTHS.between(startTime,endTime);
System.err.println("月 "+months);
long weeks = ChronoUnit.WEEKS.between(startTime,endTime);
System.err.println("周 "+weeks);
long days = ChronoUnit.DAYS.between(startTime,endTime);
System.err.println("日 "+days);
long hours = ChronoUnit.HOURS.between(startTime,endTime);
System.err.println("时 "+hours);
long minutes = ChronoUnit.MINUTES.between(startTime,endTime);
System.err.println("分 "+minutes);
long seconds = ChronoUnit.SECONDS.between(startTime,endTime);
System.err.println("秒 "+seconds);
long millis = ChronoUnit.MILLIS.between(startTime,endTime);
System.err.println("毫秒 "+millis);
System.err.printf("时间间隔 : %d 年 %d 月 %d 周 %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", years,months,weeks,days,hours,minutes,seconds,millis);
}
运行结果:
startTime : 2022-05-12T17:57:05.379
endTime : 2023-06-20T18:59:05.380
年 1
月 13
周 57
日 404
时 9697
分 581822
秒 34909320
毫秒 34909320001
时间间隔 : 1 年 13 月 57 周 404 日 9697 时 581822 分 34909320 秒 34909320001 毫秒
十九 、 until方法
until同ChronoUnit类一样,计算有年、月、周、日、时、分、秒、毫秒
计算的是LocalDate和LocalDateTime两个时间分别间隔的年、月、周、日、时、分、秒、毫秒
public static void main(String[] args) {
LocalDateTime startTime = LocalDateTime.now();
System.err.println("startTime : " + startTime);
LocalDateTime endTime = LocalDateTime.now().plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
System.err.println("endTime : " + endTime);
long years = startTime.until(endTime, ChronoUnit.YEARS);
System.err.println("年 "+years);
long months = startTime.until(endTime, ChronoUnit.MONTHS);
System.err.println("月 "+months);
long weeks = startTime.until(endTime, ChronoUnit.WEEKS);
System.err.println("周 "+weeks);
long days = startTime.until(endTime, ChronoUnit.DAYS);
System.err.println("日 "+days);
long hours = startTime.until(endTime, ChronoUnit.HOURS);
System.err.println("时 "+hours);
long minutes = startTime.until(endTime, ChronoUnit.MINUTES);
System.err.println("分 "+minutes);
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
System.err.println("秒 "+seconds);
long millis = startTime.until(endTime, ChronoUnit.MILLIS);
System.err.println("毫秒 "+months);
System.err.printf("时间间隔 : %d 年 %d 月 %d 周 %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", years,months,weeks,days,hours,minutes,seconds,millis);
}
运行结果:
startTime : 2022-05-12T18:01:45.622
endTime : 2023-06-20T19:03:45.623
日 1
月 13
周 57
日 404
时 9697
分 581822
秒 34909320
毫秒 34909320001
时间间隔 : 1 年 13 月 57 周 404 日 9697 时 581822 分 34909320 秒 34909320001 毫秒
二十 、 TemporalAdjusters时间调节器工具类
TemporalAdjusters时间调节器工具类(返回时间调节器)
firstDayOfMonth() 本月的最初一天
lastDayOfMonth() 本月的最后一天
firstDayOfNextMonth() 下个月的最初一天
firstDayOfYear() 这年的最初一天
lastDayOfYear() 这年的最后一天
firstDayOfNextYear() 下一年的第一天
firstInMonth(DayOfWeek) 最初一个星期几
lastInMonth(DayOfWeek) 最后一个星期几
dayOfWeekInMonth(int,DayOfWeek)
next(DayOfWeek) 下一个周几
nextOrSame(DayOfWeek) 下一个周几,可以是当天
previous(DayOfWeek) 上一个周几
previousOrSame(DayOfWeek) 上一个周几,可以是当天
带时差时区信息的时间,详细介绍
二十一 、 ZonedDateTime带时区(城市)的日期和时间
二十二 、 OffsetDateTime带时区(时差)的日期和时间
二十三 、 ZoneId(时区)
ZoneId.systemDefault()
ZoneId.of("+8")
ZoneId.of("Asia/Tokyo");
二十四 、 ZoneOffset(时差)
ZoneOffset.of("+8")
ZoneOffset.of("+08:00:00");