Java8之新时间日期API

来源:互联网 发布:马尔可夫算法的应用 编辑:程序博客网 时间:2024/05/24 04:07
一、使用LocalDate、LocalTime、LocalDateTime
LocalDate、LocalTime、LocalDateTime类的实例是不可变的对象,分别表示使用ISO-8601日历系统的日期、时间、日期和时间。他们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
注:ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表达法
方法
描述
now()
静态方法,根据当前时间创建对象
of()
静态方法,根据指定日期/时间创建对象
plusDays,plusWeeks,
plusMonths,plusYears
向当前LocalDate对象添加几天、几周、几个月、几年
minusDays,minusWeeks,
minusMonths,minusYears
从当前LocalDate对象减去几天、几周、几个月、几年
plus/minus
添加或减少一个Duration或Period
withDayOfMonth,
withDayOfYear,
withMonth,
withYear
将月份天数、年份天数、月份、年份修改成指定的值并返回新的LocalDate对象
getDayOfMonth
获取月份天数(1-31)
getDayOfYear
获取年份天数(1-366)
getDayOfWeek
获得星期几(返回一个DayOfWeek枚举值)
getMonth
获得月份,返回一个Month枚举值
getMonthValue
获得月份(1-12)
getYear
获得年份
until
获得两个日期之间的Period对象,或者指定ChronoUnits的数字
isBefore.isAfter
比较两个LocalDate
isLeapYear
判断是否是闰年
//LocalDate、LocalTime、LocalDateTime
@Test
public void test1(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);

LocalDateTime localDateTime1 = LocalDateTime.of(2018,1,1,1,1,1);
System.out.println(localDateTime1);

LocalDateTime localDateTime2 = localDateTime.plusYears(2);
System.out.println(localDateTime2);

LocalDateTime localDateTime3 = localDateTime.minusDays(2);
System.out.println(localDateTime3);

System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfYear());
}



二、Instant时间戳
  • ​用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算
//2. Instant : 时间戳。 (使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
@Test
public void test2(){
Instant instant = Instant.now(); //默认使用UTC时区
System.out.println(instant);
//2017-12-18T07:15:33.523Z
// 2017-12-18T15:15:33.523+08:00
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);

System.out.println(instant.getNano());//纳秒
System.out.println(instant.toEpochMilli());//毫秒 1513581333523

Instant instant1 = Instant.ofEpochSecond(5);//1970-01-01T00:00:05Z
System.out.println(instant1);
}


三、Duration和Period
  • Duration:用于计算两个“时间”间隔
  • Period:用于计算两个“日期”间隔
/*
* Duration:用于计算两个“时间”间隔
* Period:用于计算两个“日期”间隔
*/
@Test
public void test3() {
Instant instant = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
Instant instant1 = Instant.now();

Duration duration = Duration.between(instant, instant1);
System.out.println(duration.toMillis());

System.out.println("------------------------");

LocalDate localDate = LocalDate.now();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

LocalDate localDate1 = LocalDate.of(2015, 1, 1);

/*
P-2Y-11M-17D
-2
-11
-17
*/
Period period = Period.between(localDate, localDate1);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());


}


四、日期的操作
  • TemporalAdjuster:时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。
  • TemporalAdjusters:该类通过静态方法提供了大量的常用TemporalAdjuster的实现
  • 例如获取下个周日:

@Test
public void test4(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);

LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
System.out.println(localDateTime1);

LocalDateTime localDateTime2 = localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println(localDateTime2);

//自定义:下一个工作日
LocalDateTime localDateTime3 = localDateTime.with((e) -> {
LocalDateTime localDateTime4 = (LocalDateTime)e;

DayOfWeek dayOfWeek = localDateTime4.getDayOfWeek();

if (dayOfWeek.equals(DayOfWeek.FRIDAY)){
return localDateTime4.plusDays(3);
}else if (dayOfWeek.equals(DayOfWeek.SATURDAY)){
return localDateTime4.plusDays(2);
}else {
return localDateTime4.plusDays(1);
}
});
System.out.println(localDateTime3);
}



五、解析与格式化
​java.time.format.DateTimeFormatter类:提供了三种格式化方法:
  • 预定义的标准格式
  • 语言环境相关的格式
  • 自定义的格式
@Test
public void test5(){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;

DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

LocalDateTime localDateTime = LocalDateTime.now();
String D = localDateTime.format(dateTimeFormatter);
System.out.println(D);
String Date = localDateTime.format(dateTimeFormatter1);
System.out.println(Date);
}


六、时区的处理
Java8 中加入了对时区的支持,带时区的时间为分别为:
ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式
例如:Asia/Shanghai 等
ZoneId:该类中包含了所有的时区信息
getAvailableZoneIds() : 可以获取所有时区时区信息
of(id) : 用指定的时区信息获取ZoneId 对象
//6.ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期
    @Test
    public void test7(){
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(ldt);
        
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("US/Pacific"));
        System.out.println(zdt);
    }
    
    @Test
    public void test6(){
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);
    }



六、与传统日期处理的转换


原创粉丝点击