获取时间
方法 | 介绍 |
---|---|
LocalDate | 专门表示日期 |
LocalTime | 专门表示时间 |
LocalDateTime | 时间和日期 |
LocalDate、LocalTime、LocalDateTime类的实例时不可变对象
LocalDateTime.now()
/**
* LocalDateTime.now() 获取当前时间
*/
@Test
public void test1() {
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
// now = 2019-11-08T18:54:32.200
}
LocalDateTime.of()
/**
* LocalDateTime.of() 可以指定年月日时分秒
*/
@Test
public void test2() {
LocalDateTime of = LocalDateTime.of(2019, 11, 8, 21, 03, 10);
System.out.println("of = " + of);
//of = 2019-11-08T21:03:10
}
对时间日期的运算
time.plusDays(2)
/**
* function:对时间日期的运算
*
* time.plusDays(2); 在时间time的基础上加两天
*
* 产生一个新的实例
*/
@Test
public void test3() {
LocalDateTime now = LocalDateTime.now();
//加两天
LocalDateTime localDateTime = now.plusDays(2);
System.out.println("localDateTime = " + localDateTime);
//减三小时
LocalDateTime localDateTime1 = now.minusHours(3);
System.out.println("localDateTime1 = " + localDateTime1);
}
time.getYear()
/**
* function: time.getYear(); 获取时间的值
*/
@Test
public void test4() {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
System.out.println("year = " + year);
//得到月份对象,可以继续对月份进行操作
Month month = now.getMonth();
System.out.println("month = " + month); //NOVEMBER
//得到月份的值
int monthValue = now.getMonthValue();
System.out.println("monthValue = " + monthValue); //月
System.out.println(now.getDayOfMonth()); //日
System.out.println(now.getHour()); //时
System.out.println(now.getMinute());//分
System.out.println(now.getSecond());//秒
}
Instant 时间戳
- 以Unix元年 1970年1月1日0点0分0秒 到某个时间的毫秒值
- 这是计算机认识的时间
/**
* function:
*/
@Test
public void test5() {
Instant now = Instant.now();
System.out.println("now = " + now);
//这不是获取的系统时间
//默认获取的UTC时区
//与北京时间有8个小时的时差,
//
// 可以做个偏量运算
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println("offsetDateTime = " + offsetDateTime);
//2019-11-08T21:19:15.883+08:00 后面有个+8小时
//偏移了8个时区后 就是当前北京时间
}
/**
* function:
*/
@Test
public void test6() {
Instant now = Instant.now();
//当前毫秒值
long milli = now.toEpochMilli();
System.out.println("milli = " + milli);
Instant instant = Instant.ofEpochSecond(1);
System.out.println("instant = " + instant);
//instant = 1970-01-01T00:00:01Z 在元年加1秒
}
Duration
/**
* function:Duration:计算两个时间之间的间隔
*/
@Test
public void test7() throws InterruptedException {
Instant now = Instant.now();
Thread.sleep(1000);
Instant now1 = Instant.now();
Duration between = Duration.between(now, now1);
System.out.println("between = " + between.toMillis()); //between = 1000
System.out.println("between = " + between.getSeconds()); //between = 1
//这里注意,获取时间有to 和 get 都要试一下,有点坑
}
Period
/**
* function:Period:计算两个日期之间的间隔
*/
@Test
public void test8() {
LocalDate ld1 = LocalDate.of(2018, 9, 9);
LocalDate ld2 = LocalDate.now();
Period between = Period.between(ld1, ld2);
System.out.println("getDays = " + between.getDays());
System.out.println("getMonths = " + between.getMonths());
System.out.println("getYears = " + between.getYears());
}
结果:
getDays = 0
getMonths = 2
getYears = 1
时间矫正器
TemporalAdjuster
- 时间校正器。
- 有时我们可能需要获取例如:将日期调整到“下个周日”等操作
TemporalAdjusters
-
该类通过静态方法提供了大量的常用TemporalAdjuster的实现
-
例如获取下个周日: LocalDate nextSunday = LocalDate.now().with( TemporalAdjusters.next(DayOfWeek.SUNDAY) );
time.with…
/**
* function: time.with..
* <p>
* with(TemporalAdjuster adjuster)
* 可连点
*/
@Test
public void test9() {
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
LocalDateTime localDateTime = now.withDayOfMonth(10);
System.out.println("localDateTime = " + localDateTime);
//就把时间指定成当月的十号
//入参时间矫正器,比如下个周日 下个月,下一个生日
//他是一个接口,用TemporalAdjusters工具类
LocalDateTime nextSunday = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("下个星期日 = " + nextSunday);
}
格式化时间 / 日期
DateTimeFormatter
/**
* function:
*/
@Test
public void test10() {
DateTimeFormatter sdf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime ldt = LocalDateTime.now();
String format = ldt.format(sdf);
System.out.println("format = " + format);
//2019-11-09T09:55:55.113
String format2 = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("format2 = " + format2);
//2019-11-09
String format3 = ldt.format(DateTimeFormatter.ISO_DATE);
System.out.println("format3 = " + format3);
//2019-11-09
//也可以定义自己的类型
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String dateStr = now.format(dtf);
System.out.println("dateStr = " + dateStr);
//dateStr = 2019-11-09 10:08:03
//把时间字符串解析回 LocalDateTime 类型
// LocalDateTime.parse(String,pattern)
//要指定原来的格式
LocalDateTime parse = LocalDateTime.parse(dateStr, dtf);
System.out.println("parse = " + parse);
//parse = 2019-11-09T10:13:06
}
时区的处理
- java8中加入了对时间的支持,带时区的时间分别为:
- ZonedDate、ZonedTime、ZonedDateTime
- 其中每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式,例如Asia/Shanghai等
- ZoneId:该类中包含了所有的时区信息
- getAvailableZonelds():可以获取多有时区信息
of(id):用指定的时区信息获取ZoneId对象
获取所有时区
/**
* function:获取所有时区
*/
@Test
public void test11() {
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println("zoneIds = " + zoneIds);
}
创建时区
/**
* function: 在创建时间的时候可以创建时区
*/
@Test
public void test12() {
//获取上海时区的时间
LocalDateTime now = LocalDateTime.now(ZoneId.of("Hongkong"));
System.out.println("now = " + now);
//now = 2019-11-09T10:30:09.933
//构建好时间以后,可以再次设置 atZone()
LocalDateTime now1 = LocalDateTime.now();
ZonedDateTime hebron = now1.atZone(ZoneId.of("Asia/Hebron"));
System.out.println("hebron = " + hebron);
//hebron = 2019-11-09T10:33:52.609+02:00[Asia/Hebron]
//跟UTC标准有两个小时的时差
ZonedDateTime shanghai = now1.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println("shanghai = " + shanghai);
//shanghai = 2019-11-09T10:35:37.405+08:00[Asia/Shanghai] 8个小时时差
}