红薯 Java 8 的日期时间新用法

http://insightfullogic.com/blog/2012/dec/14/date-and-time-java-8-1/
标签: <无>

代码片段(5)

[代码] [Java]代码

01 // The current date and time
02 LocalDateTime.now();
03  
04 // construct from values
05 LocalDate.of(2012, 12, 12);
06 LocalDate.of(2012, Month.DECEMBER, 12);
07  
08 // Somewhere in the middle of 1970
09 LocalDate.ofEpochDay(150);
10  
11 // the train I took home today
12 LocalTime.of(17, 18);
13  
14 // From a String
15 LocalTime.parse("10:15:30");

[代码] [Java]代码

01 LocalDateTime timePoint = ...
02  
03 LocalDate theDate = timePoint.getDate();
04  
05 int monthAsInt = timePoint.getMonthValue();
06 Month month = timePoint.getMonth();
07  
08 int day = timePoint.getDayOfMonth();
09     day = timePoint.getDayOfYear();
10  
11 timePoint.getSecond();
12 timePoint.getNano();

[代码] [Java]代码

1 LocalDateTime timePoint = ...
2  
3 // Set the value, returning a new object
4 LocalDateTime another = timePoint.withDayOfMonth(10).withYear(2010);
5  
6 // You can use direct manipulation methods, or pass a value and field pair
7 LocalDateTime yetAnother = another.plusWeeks(3).plus(3, WEEKS);

[代码] [Java]代码

01 import static javax.time.calendrical.DateTimeAdjusters.*;
02  
03 LocalDateTime timePoint = ...
04  
05 // Statically imported (see above)
06 foo = timePoint.with(lastDayOfMonth());
07 bar = timePoint.with(firstDayOfYear());
08  
09 // Adjusters can also be parameterised
10 timePoint.with(lastInMonth(TUESDAY));
11 timePoint.with(previousOrSame(WEDNESDAY));
12  
13 // Using value classes as adjusters
14 timePoint.with(LocalTime.now());

[代码] [Java]代码

1 LocalDate date = ...
2 date.truncatedTo(DAYS);
3  
4 LocalTime time = ...
5 time.truncatedTo(MICROS);
6 time.truncatedTo(SECONDS);
原文地址:https://www.cnblogs.com/shihao/p/2818934.html