传统的处理接口设计并不是很友好,不易使用。终于,Java 8 借鉴第三方优秀开源库 Joda-time,重新设计了一套 API,这里记录一下自己的学习过程
package dateandtime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.TimeZone;
import static java.time.temporal.TemporalAdjusters.*;
public class DateAndTimeAPI {
public static void print(int i, Object obj) {
System.out.println(String.format("%d: " + obj, i));
}
public static void main(String[] args) {
/**
* 通过静态方法of创建一个LocalDate实例,LocalDate实例提供了多种方法
* 来读取常用的值,比如年份、月份、星期几
*/
LocalDate date = LocalDate.of(2019, 6, 18);
print(0, date.getDayOfWeek());
print(1, date);
print(2, date.getYear());
print(3, date.getDayOfMonth());
print(4, date.lengthOfMonth());
print(5, date.isLeapYear());
print(6, date.format(DateTimeFormatter.ISO_WEEK_DATE));
System.out.println();
/**
* 静态方法获取现在的日期
*/
LocalDate today = LocalDate.now();
print(7, today);
/**
* 静态方法获取现在的时间,同样也有静态方法of创建时间
*/
LocalTime time = LocalTime.now();
print(8, time);
print(9, time.getHour());
print(10, time.getMinute());
print(11, time.getSecond());
print(12, time.getNano());
/**
* 静态方法解析字符串,如果时间格式错误将解析失败抛出异常,其中月份必须是两位数的
*/
LocalDate ld = LocalDate.parse("2019-06-18");
LocalTime lt = LocalTime.parse("10:14:30");
print(13, ld);
print(14, lt);
System.out.println();
/**
* 合并时间和日期
*/
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.JUNE, 18, 10, 18, 23);
LocalDateTime localDateTime2 = LocalDateTime.of(date, time);
print(15, localDateTime);
print(16, localDateTime1);
print(17, localDateTime2);
print(18, localDateTime.getHour());
/**
* 在日期(时间)基础上加上时间(日期),并返回LocalDateTime
*/
print(19, date.atTime(time));
print(20, time.atDate(date));
System.out.println();
/**
* Instant: 机器的日期和时间格式,已Unix运年时间(UTC时区1970年1月1日午夜时分)开始所经历的描述进行计算
*/
Instant instant = Instant.now();
print(21, instant);
Instant instant1 = Instant.ofEpochSecond(3);
print(22, instant1);
print(23, Instant.ofEpochSecond(3, 0));
print(24, Instant.ofEpochSecond(2, 1_000_000_000));
System.out.println();
/**
* Duration 和 Period 可以用来表示时间间隔
*/
/**Duration.between 可以
* 计算 localDateTime1 和 localDateTime2 之间的时间差值
* 计算 localTime1 和 localTime2 之间的时间差值
* 计算 instant1 和 instant2 之间的时间差值
* 不能 计算 localDate1 和 localDte2 之间的时间差值
*/
Duration duration = Duration.between(localDateTime1, localDateTime2);
print(25, duration);
/**
* Period.between 可以计算 localDate1 和 localDte2 之间的时间差值
*/
Period tendays = Period.between(LocalDate.of(2019, 06, 1),
LocalDate.of(2019, 06, 11));
print(26, tendays);
/**
* Duration 和 Period 提供了很多方便的工厂类,直接创建其实例。
*/
Duration threeMinutes = Duration.ofMinutes(3);
print(27, threeMinutes);
Duration twoDays = Duration.of(2, ChronoUnit.DAYS);
print(28, twoDays);
Period nineDays = Period.ofDays(9);
print(29, nineDays);
Period threeWeeks = Period.ofWeeks(3);
print(30, threeWeeks);
print(31, Period.of(2, 1, 3));
print(32, Period.ofYears(2));
System.out.println();
/**
* 操纵、解析和格式化日期
*/
LocalDate date1 = LocalDate.of(2018, 2, 6);
print(33, date1.withYear(2019));
print(34, date1.withMonth(6));
print(35, date1.withDayOfMonth(19));
print(36, date1.with(ChronoField.DAY_OF_MONTH, 2));
print(37, date1.plusWeeks(1));
print(38, date1.plusYears(1));
print(39, date1.plusDays(4));
print(40, date1.plus(4, ChronoUnit.MONTHS));
print(41, date1.with(next(DayOfWeek.SUNDAY)));
print(42, date1.with(nextOrSame(DayOfWeek.SUNDAY)));//与next的唯一区别是有可能当天符合要求,就返回当天
print(43, date1.with(lastInMonth(DayOfWeek.SUNDAY)));
print(44, date1.format(DateTimeFormatter.ISO_DATE));
print(45, date1.format(DateTimeFormatter.BASIC_ISO_DATE));
print(46, LocalDate.parse("2019-06-19", DateTimeFormatter.ISO_DATE));
print(47, LocalDate.parse("20190619", DateTimeFormatter.BASIC_ISO_DATE));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.d");
LocalDate date2 = LocalDate.now();
String formattedDate = date2.format(formatter);
print(48, formattedDate);
LocalDate date3 = LocalDate.parse(formattedDate, formatter);
print(49, date3);
ZoneId zonedId = TimeZone.getDefault().toZoneId();
ZonedDateTime zdt1 = date1.atStartOfDay(zonedId);
print(50, zdt1);
ZonedDateTime zdt2 = localDateTime2.atZone(zonedId);
print(51, zdt2);
ZonedDateTime zdt3 = instant.atZone(zonedId);
print(52, zdt3 );
}
}
执行结果: