首页 > 代码库 > Java 8 的日期时间新用法

Java 8 的日期时间新用法

1.// The current date and time
LocalDateTime.now();
// construct from values
LocalDate.of(2012, 12, 12);
LocalDate.of(2012, Month.DECEMBER, 12);
// Somewhere in the middle of 1970
LocalDate.ofEpochDay(150);
// the train I took home today
LocalTime.of(17, 18);
// From a String
2.LocalDateTime timePoint = ...
LocalDate theDate = timePoint.getDate();
int monthAsInt = timePoint.getMonthValue();
Month month = timePoint.getMonth();
int day = timePoint.getDayOfMonth();
day = timePoint.getDayOfYear();
timePoint.getSecond();http://www.huiyi8.com/jiaoben/
timePoint.getNano(); javascript特效 
3.LocalDateTime timePoint = ...
// Set the value, returning a new object
LocalDateTime another = timePoint.withDayOfMonth(10).withYear(2010);
// You can use direct manipulation methods, or pass a value and field pair
LocalDateTime yetAnother = another.plusWeeks(3).plus(3, WEEKS);
4.import static javax.time.calendrical.DateTimeAdjusters.*;
LocalDateTime timePoint = ...
// Statically imported (see above)
foo = timePoint.with(lastDayOfMonth());
bar = timePoint.with(firstDayOfYear());
// Adjusters can also be parameterised
timePoint.with(lastInMonth(TUESDAY));
timePoint.with(previousOrSame(WEDNESDAY));
// Using value classes as adjusters
timePoint.with(LocalTime.now());
5.LocalDate date = ...
date.truncatedTo(DAYS);
LocalTime time = ...
time.truncatedTo(MICROS);
time.truncatedTo(SECONDS)