Java 8 中处理日期和时间示例
package com.demo.test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* @Author:Dai Zhipeng
* @Description: Java 8 中处理日期和时间示例
* @Date: Created in 9:44 2019/2/26
* @Modified by:
*/
public class TestJava8Time {
public static void main(String args[]) {
TestJava8Time test =new TestJava8Time();
test.dealSpecificTime();
System.out.println("**********");
test.checkBirthday();
System.out.println("**********");
test.getCurrentTime();
System.out.println("**********");
test.compareDate();
System.out.println("**********");
test.dealDifferentZoneDateTime();
System.out.println("**********");
test.compareTwoDate();
System.out.println("**********");
test.analyzeDate();
System.out.println("**********");
test.timeConvertDate();
}
/**
* 获取今天的日期(指年月日)和按照指定日期,进行相应操作
*/
private void dealSpecificTime() {
LocalDate todayDate = LocalDate.now();
System.out.println("今天的日期:" + todayDate);
//获取今年当前这个月的第1天
LocalDate firstDay = todayDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("今年的当前月份的第一天:" + firstDay);
//获取今年当前这个月的第1天,另外一种写法
LocalDate firstDay2 = todayDate.withDayOfMonth(1);
System.out.println("今年的当前月份的第一天(另一种写法):" + firstDay2);
//获取今年当前这个月的最后1天,不用考虑大月,小月,平年,闰年
LocalDate lastDay = todayDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("今年当前这个月的最后1天:" + lastDay);
//当前日期+1天
LocalDate tomorrow = todayDate.plusDays(1);
System.out.println("N天后的日期为:" + tomorrow);
//判断是否为闰年
boolean isLeapYear = todayDate.isLeapYear();
System.out.println("是否为闰年:" + isLeapYear);
}
/**
* 判断当天是否为某人的生日,当天是否为账单日
*/
private void checkBirthday() {
boolean flag =false;
LocalDate todayDate = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, 10, 20);
MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
MonthDay today = MonthDay.from(LocalDate.of(todayDate.getYear(), todayDate.getMonth(), todayDate.getDayOfMonth()));
if (today.equals(birthdayMd)) {
flag =true;
}
System.out.println("是否今天过生日:" + flag);
}
/**
* 获取当前时间用LocalTime
*/
private void getCurrentTime() {
//获取当前时间(包含毫秒)
LocalTime currentTime = LocalTime.now();
System.out.println("获取当前时间(包含毫秒)为:" + currentTime);
//获取当前时间(不含毫秒)
LocalTime currentTime2 = LocalTime.now().withNano(0);
System.out.println("获取当前时间(不包含毫秒)为:" + currentTime2);
//指定时间
LocalTime time = LocalTime.of(14, 10, 21);
System.out.println("指定时间1为:" + time);
LocalTime time2 = LocalTime.parse("12:00:01");
System.out.println("指定时间2为:" + time2);
//当前时间增加2小时(方式一)
LocalTime nowTimePlus2Hour = currentTime.plusHours(2);
System.out.println("当前时间增加2小时(方式一)为:" + nowTimePlus2Hour.withNano(0));
//当前时间增加2小时(方式二)
LocalTime nowTimePlus2Hour2 = currentTime.plus(2, ChronoUnit.HOURS);
System.out.println("当前时间增加2小时(方式二)为:" + nowTimePlus2Hour2.withNano(0));
}
/**
* 比较2个日期哪个在前,哪个在后;isAfter(),isBefore()
*/
private void compareDate() {
LocalDate today = LocalDate.now();
LocalDate specifyDate1 = LocalDate.of(2015, 10, 20);
System.out.println("当前日期是否在指定日期1后面:" + today.isAfter(specifyDate1));
LocalDate specifyDate2 = LocalDate.of(2019, 10, 20);
System.out.println("当前日期是否在指定日期2前面:" + today.isBefore(specifyDate2));
}
/**
* 处理不同时区的时间
*/
private void dealDifferentZoneDateTime() {
//查看当前的时区
ZoneId defaultZone = ZoneId.systemDefault();
//Asia/Shanghai
System.out.println("查看当前的时区:" + defaultZone);
//查看美国纽约当前的时间
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime shanghaiTime = LocalDateTime.now();
LocalDateTime americaDateTime = LocalDateTime.now(america);
System.out.println("上海时间为:" + shanghaiTime);
System.out.println("美国时间为:" + americaDateTime);
//带有时区的时间
ZonedDateTime americaZoneDateTime = ZonedDateTime.now(america);
//2019-02-26T00:49:14.910-05:00[America/New_York]
System.out.println("带有时区的美国时间为:" + americaZoneDateTime);
}
/**
* 比较两个日期之前时间差:在项目中,经常需要比较两个日期之间相差几天,
* 或者相隔几个月,我们可以使用java8的Period来进行处理;
* 我们使用Period类比较天数,比较奇怪,他返回的值,并不是2个日期之间总共的天数差,
* 而是一个相对天数差,比如,5月1日,和10月2日,他比较的是仅仅2个天之间的差,那1号和2号,相差1天,
* 而实际上,因为中间相差了好几个月,所以真正的天数差肯定不是1天,
* 所以我们可以使用until,并指明精度单位是days,就可以计算真正的天数差了
*/
private void compareTwoDate() {
LocalDate today = LocalDate.now();
LocalDate specifyDate = LocalDate.of(2015, 10, 2);
Period period = Period.between(specifyDate, today);
//天数间隔
System.out.println(period.getDays());
//月份间隔
System.out.println(period.getMonths());
//天数差
System.out.println(specifyDate.until(today, ChronoUnit.DAYS));
}
/**
* 日期时间格式解析、格式化
*/
private void analyzeDate() {
String specifyDate ="20151011";
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate formatted = LocalDate.parse(specifyDate, formatter);
System.out.println("yyyy-MM-dd的转换格式为:" + formatted);
//自定义时间转换格式
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("YYYY MM dd");
System.out.println("yyyy MM dd的转换格式为:" + formatter2.format(LocalDate.now()));
}
/**
* java8 时间类与Date类的相互转化:因为java8之前Date是包含日期和时间的,
* 而LocalDate只包含日期,LocalTime只包含时间,
* 所以与Date在互转中,势必会丢失日期或者时间,或者会使用起始时间
*/
private void timeConvertDate() {
//Date与Instant的相互转化
Instant instant = Instant.now();
Date date = Date.from(instant);
Instant instant2 = date.toInstant();
//Date转为LocalDateTime
Date date2 =new Date();
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault());
//LocalDateTime转Date
LocalDateTime localDateTime3 = LocalDateTime.now();
Instant instant3 = localDateTime3.atZone(ZoneId.systemDefault()).toInstant();
Date date3 = Date.from(instant);
//LocalDate转Date
//因为LocalDate不包含时间,所以转Date时,会默认转为当天的起始时间,00:00:00
LocalDate localDate4 = LocalDate.now();
Instant instant4 = localDate4.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date date4 = Date.from(instant);
}
}