JDK1.8日期时间API
LocalDate、 LocalTime、 LocalDateTime类的实例是不可变的对象,不能创建对象,但是可以使用类名调用其中的静态方法,它们分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。
注: ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法
这些新增的日期时间API都在 java.time包下
LocalDateTime类
方式1通过静态方法 now();
例如:LocalDateTime ldt = LocalDateTime.now();
方式2通过静态方法of()方法参数可以指定年月日时分秒
例如:LocalDateTime of = LocalDateTime.of(2018, 12, 30, 20, 20, 20);
public class MyTest1 {
public static void main(String[] args) {
// LocalDate 表示年月日
// LocalTime 时分秒
// LocalDateTime 年月日 时分秒
//获取当前的年月日时分秒
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDate now1 = LocalDate.now();
System.out.println(now1);
LocalTime now2 = LocalTime.now();
System.out.println(now2);
System.out.println("======================");
//指定年月日,时分秒
LocalDateTime of = LocalDateTime.of(2010, 10, 10, 14, 20, 34);
LocalDate of1 = LocalDate.of(2011, 01, 20);
LocalTime of2 = LocalTime.of(10, 23, 45);
}
}
1.与获取相关的方法:get系类的方法
ldt.getYear();获取年
ldt.getMinute();获取分钟
ldt.getHour();获取小时
getDayOfMonth 获得月份天数(1-31)
getDayOfYear 获得年份天数(1-366)
getDayOfWeek 获得星期几(返回一个 DayOfWeek枚举值)
getMonth 获得月份, 返回一个 Month 枚举值
getMonthValue 获得月份(1-12)
getYear 获得年份
public class MyTest2 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
int year = now.getYear();
Month month = now.getMonth();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
DayOfWeek dayOfWeek = now.getDayOfWeek();
int dayOfYear = now.getDayOfYear();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println(year);
System.out.println(month);
System.out.println(monthValue);
System.out.println(dayOfMonth);
System.out.println(dayOfWeek);
System.out.println(dayOfYear);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
}
2.格式化日期日期字符串的方法 format()
例如:String yyyy = ldt.format(DateTimeFormatter.ofPattern(“yyyy”));
public class MyTest3 {
public static void main(String[] args) {
//格式化日期
LocalDate now = LocalDate.now();
System.out.println(now);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format = now.format(dateTimeFormatter);
System.out.println(format);
System.out.println("==========================");
LocalTime now1 = LocalTime.now();
System.out.println(now1);
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("HH时mm分ss秒");
String format1 = now1.format(dateTimeFormatter1);
System.out.println(format1);
System.out.println("=======================");
LocalDateTime now2 = LocalDateTime.now();
System.out.println(now2);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
String format2 = now2.format(dateTimeFormatter2);
System.out.println(format2);
}
}
3.转换的方法 toLocalDate();toLocalTime();
例如:LocalDate localDate = ldt.toLocalDate();
例如:LocalTime localTime = ldt.toLocalTime();
public class MyTest4 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//转成年月日的这个类
LocalDate localDate = now.toLocalDate();
System.out.println(localDate);
//转成时分秒这个类
LocalTime localTime = now.toLocalTime();
System.out.println(localTime);
}
}
4.判断的方法
isAfter()判断一个日期是否在指定日期之后
isBefore()判断一个日期是否在指定日期之前
isEqual(); 判断两个日期是否相同
isLeapYear()判断是否是闰年注意是LocalDate类中的方法
例如: boolean after = ldt.isAfter(LocalDateTime.of(2024, 1, 1, 2, 3));
例如 boolean b= LocalDate.now().isLeapYear();
public class MyTest {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate of = LocalDate.of(2018, 2, 3);
//判断一个日期在不在另一个日期之后
boolean b = now.isAfter(of);
System.out.println(b);
//判断一个日期在不在另一个日期之前
boolean before = of.isBefore(now);
System.out.println(before);
//判断两个日期是否相同
boolean equal = now.isEqual(of);
System.out.println(equal);
//判断一个年份是不是闰年
boolean leapYear = now.isLeapYear();
System.out.println(leapYear);
}
}
5.解析的静态方法parse(“2007-12-03T10:15:30”);
paser() 将一个日期字符串解析成日期对象,注意字符串日期的写法的格式要正确,否则解析失败
例如:LocalDateTime parse = LocalDateTime.parse(“2007-12-03T10:15:30”);
public class MyTest2 {
public static void main(String[] args) {
//解析日期:把日期字符串转换成日期对象
//按照默认格式去解析的
String str="2020-10-10";
LocalDate parse = LocalDate.parse(str);
System.out.println(parse);
System.out.println("==================");
String time="20:30:30";
LocalTime parse1 = LocalTime.parse(time);
System.out.println(parse1);
System.out.println("==================");
//按照默认格式去解析的 默认格式 2020-10-10T20:30:30
String s="2020-10-10T20:30:30";
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println("=========================");
//按照指定格式解析
String str2="2020年10月10日";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate parse2 = LocalDate.parse(str2, dateTimeFormatter);
System.out.println(parse2);
System.out.println("====================");
String ss = "2020-10-10 20:30:30";
DateTimeFormatter formatter33 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse3 = LocalDateTime.parse(ss, formatter33);
System.out.println(parse3);
}
}
6.添加年月日时分秒的方法 plus系列的方法 都会返回一个新的LocalDateTime的对象
LocalDateTime localDateTime = ldt.plusYears(1);
LocalDateTime localDateTime1 = ldt.plusMonths(3);
LocalDateTime localDateTime2=ldt.plusHours(10);
7.减去年月日时分秒的方法 minus 系列的方法 注意都会返回一个新的LocalDateTime的对象
例如:LocalDateTime localDateTime2 = ldt.minusYears(8);
public class MyTest3 {
public static void main(String[] args) {
//给日期增加时间量的方法
LocalDateTime now = LocalDateTime.now();
//plusXXX系列的方法,增加完时间量后,会返回一个新的对象
LocalDateTime localDateTime = now.plusYears(1);
System.out.println(localDateTime);
LocalDateTime localDateTime1 = now.plusMonths(2);
System.out.println(localDateTime1);
System.out.println("===========================");
//给日期减去相应的时间量 minusXXX 系列方法,调用完毕返回一个新的日期对象
LocalDateTime now1 = LocalDateTime.now();
LocalDateTime localDateTime2 = now1.minusYears(2);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = now1.minusDays(10);
System.out.println(localDateTime3);
/*System.out.println("=================");
Calendar instance = Calendar.getInstance();
System.out.println(instance);
int i = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(i);
instance.add(Calendar.YEAR,-1);
System.out.println(instance);*/
}
}
8.指定年月日时分秒的方法 with系列的方法 注意都会返回一个新的LocalDateTime的对象
例如 LocalDateTime localDateTime3 = ldt.withYear(1998);
//获取这个月的第几个星期几是几号,比如 TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY) 代表的意思是这个月的第二个星期五是几号
public class MyTest {
public static void main(String[] args) {
//LocalDate of = LocalDate.of(2011, 10, 3);
LocalDate now = LocalDate.now();
//withXXX方法,指定日期,调用完,返回的是一个新的日期对象。
LocalDate localDate = now.withYear(2015);
System.out.println(localDate);
System.out.println(now.withMonth(10));
System.out.println(now.withDayOfMonth(20));
System.out.println("========================");
LocalDate now1 = LocalDate.now();
//TemporalAdjuster 接口
//下周一
TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.MONDAY);
LocalDate with = now1.with(next);
System.out.println(with);
System.out.println("==========================");
//这个月的第一个星期五
TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY);
LocalDate with1 = now1.with(temporalAdjuster);
System.out.println(with1);
System.out.println("======================");
//过去的前一个周一
TemporalAdjuster temporalAdjuster1 = TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);
LocalDate with2 = now1.with(temporalAdjuster1);
System.out.println(with2);
System.out.println("==========================");
TemporalAdjuster temporalAdjuster2 = TemporalAdjusters.firstDayOfNextMonth();
LocalDate with3 = now1.with(temporalAdjuster2);
System.out.println(with3);
System.out.println("=======================");
//dayOfWeekInMonth(4, DayOfWeek.FRIDAY); 本月的第四个星期五
TemporalAdjuster temporalAdjuster3 = TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.FRIDAY);
LocalDate with4 = now1.with(temporalAdjuster3);
System.out.println(with4);
}
}
Instant类 时间戳类从1970-01-01 00:00:00 截止到当前时间的毫秒值
1获取对象的方法 now()
注意默认获取出来的是当前的美国时间和我们相差八个小时
Instant ins = Instant.now();
System.out.println(ins);
我们在东八区 所以可以加8个小时 就是我们的北京时间
public class MyTest1 {
public static void main(String[] args) {
long l = System.currentTimeMillis();
//Instant 时间戳类从1970 -01 - 01 00:00:00 截止到当前时间的毫秒值
//获取 Instant 对象 now()
Instant now = Instant.now();
System.out.println(now);
//获取毫秒值
long l1 = now.toEpochMilli();
System.out.println(l);
//获取间隔的秒值 getEpochSecond()
long epochSecond = now.getEpochSecond();
System.out.println(epochSecond);
System.out.println(l1/1000);
}
}
- Instant中设置偏移量的方法:atOffset() 设置偏移量
OffsetDateTime time = ins.atOffset(ZoneOffset.ofHours(8));
System.out.println(time);
public class MyTest2 {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
//now.atOffset(ZoneOffset.ofHours(8)); 偏移8个小时 得到一个偏移的时间 OffsetDateTime
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
}
}
3.获取系统默认时区时间的方法atZone()
方法的参数是要一个时区的编号可以通过时区编号类获取出来
ZoneId.systemDefault()获取本地的默认时区ID
ZonedDateTime zonedDateTime = ins.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
4.get系列的方法
getEpochSecond() 获取从1970-01-01 00:00:00到当前时间的秒值
toEpochMilli();获取从1970-01-01 00:00:00到当前时间的毫秒值
- ofEpochSecond()方法 给计算机元年增加秒数
ofEpochMilli() 给计算机元年增加毫秒数
例如 Instant instant = Instant.ofEpochSecond(5);
System.out.println(instant);
public class MyTest3 {
public static void main(String[] args) {
// ZoneId 时间时区类
//systemDefault(); 获取系统默认的时区变化
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
System.out.println("==========================");
//获取世界所有的时区编号
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
for (String availableZoneId : availableZoneIds) {
System.out.println(availableZoneId);
}
System.out.println("===================");
//获取某个时区的时间
ZoneId of = ZoneId.of("America/Bogota");
LocalDateTime now = LocalDateTime.now(of);
System.out.println(now);
System.out.println("================");
//使用的就是系统默认的时区
LocalDateTime now1 = LocalDateTime.now();
System.out.println(now1);
LocalDateTime now2 = LocalDateTime.now(ZoneId.systemDefault());
System.out.println(now2);
System.out.println("====================");
//ofEpochSecond() 方法 给计算机元年增加秒数
//ofEpochMilli() 给计算机元年增加毫秒数
/* Date date = new Date(1000 * 60 * 60);
System.out.println(date);*/
//给计算机元年增添一小时 1970-01-01 01:00:00
Instant instant = Instant.ofEpochMilli(1000 * 60 * 60);
System.out.println(instant);
//给计算机元年增加一天 1970-01-02 00:00:00
Instant instant1 = Instant.ofEpochSecond(60 * 60 * 24);
System.out.println(instant1);
}
}
Duration : 用于计算两个“时间”间隔的类
Duration类中静态方法between()
Instant start = Instant.now();
for(int i=0;i<1000L;i++){
System.out.println(“循环内容”);
}
Instant end = Instant.now();
静态方法:between() 计算两个时间的间隔,默认是秒
Duration between = Duration.between(start, end);
Duration中的toMillis()方法:将秒转成毫秒
System.out.println(between.toMillis());
public class MyTest1 {
public static void main(String[] args) {
// long start = System.currentTimeMillis();
Instant start = Instant.now();
for (int i = 0; i < 20000; i++) {
System.out.println(i);
}
Instant end = Instant.now();
// long end = System.currentTimeMillis();
//System.out.println("耗时"+(end-start)+"毫秒");
/* Duration:
用于计算两个“时间”间隔的类*/
Duration between = Duration.between(start, end);
long l = between.toMillis();
long l1 = between.toMinutes();
long l2 = between.toHours();
System.out.println("耗时" +l+ "毫秒");
}
}
Period : 用于计算两个“日期”间隔的类
Period类 中的静态方法between()
计算两个日期之间的间隔
LocalDate s = LocalDate.of(1985, 03, 05);
LocalDate now = LocalDate.now();
Period be = Period.between(s, now);
System.out.println(be.getYears());间隔了多少年
System.out.println(be.getMonths());间隔了多少月
System.out.println(be.getDays());间隔多少天
public class MyTest2 {
public static void main(String[] args) {
LocalDate birthday = LocalDate.of(1997, 01, 11);
LocalDate now = LocalDate.now();
// Period:用于计算两个“日期”间隔的类
Period between = Period.between(birthday, now);
int years = between.getYears();
int months = between.getMonths();
int days = between.getDays();
System.out.println(years);
System.out.println(months);
System.out.println(days);
}
}
TemporalAdjuster : 时间校正器,是个接口
采用自定义的方式来指定日期 比如指定下个工作日
//采用自定义的方式来指定日期 比如指定下个工作日
public class MyTest1 {
public static void main(String[] args) {
LocalDate now1 = LocalDate.now();
LocalDate with1 = now1.with(new TemporalAdjuster() {
@Override
//参数 temporal 当前的日期
public Temporal adjustInto(Temporal temporal) {
//如果这天是星期五。下个工作日,是当前时间加3天
//如果是星期6,下个工作日是当前时间加2天
//其余星期加1天
//向下转型
LocalDate localDate = (LocalDate) temporal;
//获取今天是一周中的星期几
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
LocalDate localDate1 = localDate.plusDays(3);
return localDate1;
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
LocalDate localDate2 = localDate.plusDays(2);
return localDate2;
} else {
LocalDate localDate3 = localDate.plusDays(1);
return localDate3;
}
}
});
System.out.println(with1);
}
}
对当前日期进行格式化的两种方法
1.format()方法把一个日期对象的默认格式 格式化成指定的格式
String format1 = dateFormat.format(now);
System.out.println(format1);
2.格式化日期 方式2使用日期类中的format方法 传入一个日期格式化类对象
LocalDateTime now1 = LocalDateTime.now();
使用日期类中的format方法 传入一个日期格式化类对象,使用DateTimeFormatter中提供好的日期格式常量
now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
public class MyTest2 {
public static void main(String[] args) {
// DateTimeFormatter JDK1.8 提供的格式化日期的类
//对当前日期进行格式化有两种方式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate now = LocalDate.now();
String format = now.format(dateTimeFormatter);
System.out.println(format);
System.out.println("===========");
String format1 = dateTimeFormatter.format(now);
System.out.println(format1);
}
}
ZonedDate,ZonedTime、ZonedDateTime : 带时区的时间或日期
用法和 LocalDate、 LocalTime、 LocalDateTime 一样 只不过ZonedDate,ZonedTime、ZonedDateTime 这三个带有当前系统的默认时区
ZoneID 世界时区类
1.获取世界各个地方的时区的集合 的方法getAvailableZoneIds()
使用ZoneID中的静态方法getAvailableZoneIds();来获取
例如:Set availableZoneIds = ZoneId.getAvailableZoneIds();
2.获取系统默认时区的ID
ZoneId zoneId = ZoneId.systemDefault(); //Asia/Shanghai
3.获取带有时区的日期时间对象
//创建日期对象
LocalDateTime now = LocalDateTime.now();
//获取不同国家的日期时间
//根据各个地区的时区ID名创建对象
ZoneId timeID = ZoneId.of(“Asia/Shanghai”);
//根据时区ID获取带有时区的日期时间对象
ZonedDateTime time = now.atZone(timeID);
System.out.println(time);
//方式2 通过时区ID 获取日期对象
LocalDateTime now2 = LocalDateTime.now(ZoneId.of(“Asia/Shanghai”));
System.out.println(now2);
public class MyTest4 {
public static void main(String[] args) {
//ZoneId
ZoneId zoneId = ZoneId.systemDefault();
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
/* for (String availableZoneId : availableZoneIds) {
System.out.println(availableZoneId);
}*/
//获取America/Toronto 多伦多现在的时间
ZoneId of = ZoneId.of("America/Toronto");
LocalDateTime now = LocalDateTime.now(of);
System.out.println(now);
}
}