一、LocalDate
1、LocalDate并不包含当天的时间信息,也不附带任何与时区有关的信息。
2、创建方法
(1)通过静态工厂方法创建一个LocalDate实例。
LocalDate date = LocalDate.of(2014,3,8)
(2)LocalDate today = LocalDate.now();
(3)LocalDate date = LocalDate.parse("2014-03-18");
3、读取LocalDate对象的值
(1)直接读取
LocalDate date = LocalDate.of(2014,3,18);
int year = date.getYear();//2014
Month month = date.getMonth(); //MARCH
int day = date.getDayOfMonth(); //18
DayOfWeek dow = date.getDayOfWeek();//TUESDAY
int len = date.lengthOfMonth(); //31
boolean leap = date.isLeapYear(); //false
(2)使用TemporalField
LocalDate date = LocalDate.of(2014,3,18);
int year = date.get(ChronoField.YEAR);//2014
int month = date.get(ChronoField.MONTH_OF_YEAR); // 3
int day = date.get(ChronoField.DAY_OF_MONTH); //18
二、LocalTime
1、创建方法
(1)LocalTime time1 = LocalTime.of(13,3);//时,分
(2)LocalTime time2 = LocalTime.of(13,3,18);//时,分,秒
(3)LocalTime time3 = LocalTime.parse("13:45:50");
2、读取LocalTime的值
int hour = time3.getHour();
int minute = time3.getMinute();
int second = time3.getSecond();
三、合并日期和时间
LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time = LocalTime.of(13,3);//时,分
LocalDateTime dt1 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
LocalDateTime dt2 = LocalDateTime.of(date,time);
LocalDateTime dt3 = date.atTime(time);
LocalDateTime dt4 = date.atTime(13,3);
LocalDateTime dt5 = date.atTime(13,3,30);
LocalDateTime dt6 = time.atDate(date);
LocalDate date1 = dt6.toLocalDate();
LocalTime time3 = dt6.toLocalTime();
四、机器的日期和时间格式
1、创建方法
Instant.ofEpochSecond(3);
Instant.ofEpochSecond(3,0);
Instant.ofEpochSecond(2,1000000000);
Instant.ofEpochSecond(4,-1000000000);
Instant.now();
五、Duration
用来表示两个LocalDateTime,两个LocalTime,两个Instant之间的间隔时长。
1、创建方法
Instant instant1 = Instant.ofEpochSecond(3);
Instant instant2 = Instant.ofEpochSecond(3,0);
LocalTime time1 = LocalTime.of(13,3);//时,分
LocalTime time2 = LocalTime.of(13,3);//时,分
LocalDateTime dt1 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
LocalDateTime dt2 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
Duration d1 = Duration.between(instant1,instant2);
Duration d2 = Duration.between(time1,time2);
Duration d3 = Duration.between(dt1,dt2);
2、直接创建对应实例
Duration threeMinutes = Duration.ofMinutes(3);
Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
六、Period
以年月日的方式对多个时间单位建模。该类的工厂方法between,可以得到两个LocalDate之间的时长
1、创建方法
LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = LocalDate.parse("2014-03-18");
Period period = Period.between(date2,date1);
2、直接创建对应实例
Period tenDays = Period.ofDays(10);
Period tenWeeks = Period.ofWeeks(10);
Period tenYearsSixMonthOneDay = Period.of(10,6,1);
七、操作日期
使用如下方法操作日期后,返回一个修改了属性的对象。它们都不会修改原来的对象。
1、以比较直观的方式操作LocalDate的属性
LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = date1.withYear(2011);//2011-03-18
LocalDate date3 = date1.withDayOfMonth(24);//2011-03-24
LocalDate date4 = date1.with(ChronoField.MICRO_OF_DAY,9);//2011-09-24
2、采用with方法,它接收一个TemporalField对象,所有日期和时间API都实现了get和with方法,它们将TemporalField的读取和修改区分开。
LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate startDate =date1.with(TemporalAdjusters.firstDayOfMonth());
LocalDate endtDate = date1.with(TemporalAdjusters.firstDayOfMonth());
3、以相对方式修改LocalDate对象的属性
LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = date1.plusWeeks(1);//2014-03-25
LocalDate date3 = date1.minusYears(3);//2011-03-25
LocalDate date4 = date1.plus(6,ChronoUnit.MONTHS);//2011-09-25
八、更复杂的操作
比如将日期调整到下个周日,下个工作日,或者本月的最后一天,,可使用重载版本的with,向其传递一个提供了更多定制化选择的TemporalAdjuster对象,更加灵活的处理日期。
LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = date1.with(nextOrSame(DayOfWeek.SATURDAY));//2014-03-23
LocalDate date3 = date1.with(lastDayOfMonth());//2011-03-31
九、打印输出及解析日期-时间对象
1、使用两个不同的格式器生成字符串
LocalDate date = LocalDate.of(2014,3,18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
2、使用工厂方法parse达到重绘该日期对象的目的
LocalDate date1 = LocalDate.parse("20140318",DateTimeFormatter.BASIC_ISO_DATE);
LocalDate date2 = LocalDate.parse("2014-03-18",DateTimeFormatter.ISO_LOCAL_DATE);
3、DateTimeFormatter类还支持一个静态工厂方法,它可以按照某个特定的模式创建格式器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
LocalDate date3 = LocalDate.of(2014,3,18);
String formattedDate = date3.format(formatter);
LocalDate date4 = LocalDate.parse(formattedDate,formatter);
4、ofPattern方法提供了一个重载的版本,使用它你可以创建某个Local的格式器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.MMMM yyyy", Locale.ITALIAN);
LocalDate date3 = LocalDate.of(2014,3,18);
String formattedDate = date3.format(formatter);
LocalDate date4 = LocalDate.parse(formattedDate,formatter);