这里列出了Java8中新增的有关日期和时间的类,以及类的方法,如果想更进一步了解,可以参考Java8手册,我没有将输出语句添加到程序中,就是让大家动动手,自己添加,看看这些类的具体使用,毕竟自己实践比看效果好一些哦!
package com.dong.java8;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalField;
import static java.time.temporal.TemporalAdjusters.*;
import java.time.format.*;
public class testDate {
public static void main(String[] args) {
//localDate提供了简单的日期,不含当天的时间信息
LocalDate date = LocalDate.of(2018, 10, 23);
//获取年份
int year = date.getYear();
//获取月份
Month month = date.getMonth();
//获取几号
int day = date.getDayOfMonth();
//获取星期几
DayOfWeek dow = date.getDayOfWeek();
//月份时长
int 冷= date.lengthOfMonth();
//获取是否瑞年
boolean leap = date.isLeapYear();
//获取当前日期
LocalDate today = LocalDate.now();
//LocalDate有一个int get(TemporalField field)方法,接受TemporalField接口实现类,TemporalField接口的实现类
//是enum ChronoField implements TemporalField,是一个枚举,所以可以和私用get方法得到枚举元素的值,
int yearr = date.get(ChronoField.YEAR);
int monthh = date.get(ChronoField.MONTH_OF_YEAR);
int dayy = date.get(ChronoField.DAY_OF_MONTH);
//通过解析字符串创建LocalDate对象,
LocalDate datee = LocalDate.parse("2018-10-23");
//访问时间可以使用LocalTime类
LocalTime time = LocalTime.of(14, 56,40);
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
//通过解析字符串创建LocalTime对象
LocalTime timee = LocalTime.parse("14:59:10");
//既包含日期,又包含时间的符合类
LocalDateTime dt1 = LocalDateTime.of(2018,10,23,15,1,10);
LocalDateTime dt2 = LocalDateTime.of(datee, timee);
LocalDateTime dt3 = date.atTime(15,2,10);
LocalDateTime dt4 = date.atTime(time);
LocalDateTime dt5 = time.atDate(date);
//从LocalDateTime 中提取LocalDate, 或 LocalTime
LocalDate date1 = dt1.toLocalDate();
LocalTime time1 = dt1.toLocalTime();
//Instant 类对时间的建模是一个大整型数,它是以Unix元年时间开始所经历的秒数进行计算;
Instant ins = Instant.ofEpochSecond(3);
Instant ins2 = Instant.ofEpochSecond(3, 0);
Instant ins3 = Instant.ofEpochSecond(5, -100000000); //5秒之前的1亿纳秒
//Duration , Period 类;
Duration d1 = Duration.between(time, timee);
Duration d2 = Duration.between(date, datee);
Duration d3 = Duration.between(ins, ins2);
//操纵,修改和格式化日期
//LocalDate withYear(int year)
//LocalDate withMonth(int month)
//LocalDate withDayOfMonth(int dayOfMonth)
//LocalDate withDayOfYear(int dayOfYear)
//withAttribute方法会创建对象的一个副本,并按照需求修改它的属性,都不会修改原来的对象:
LocalDate datel = LocalDate.of(2018,10,23);
LocalDate datel1 = datel.withYear(2019);
LocalDate datel2 = datel.withMonth(12);
LocalDate datel3 = datel.withDayOfMonth(30);
LocalDate datel4 = datel.with(ChronoField.MONTH_OF_YEAR, 6);
LocalDate datel5 = date1.plusMonths(1);
LocalDate datel6 = datel.minusWeeks(10);
LocalDate datel7 = datel.plus(6, ChronoUnit.MONTHS);
//TemporalAdjuster
//进行更复杂的操作,例如下个周几,最后一天。。。
//导入TemporalAdjusters.* 包中的所有方法和变量
LocalDate datel8 = datel.with(lastDayOfYear());
LocalDate datel9 = datel.with(firstDayOfYear());
//打印输出以及解析日期-时间对象
//java.time.format包中包含解析和打印的方法
String s1 = datel.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = datel.format(DateTimeFormatter.ISO_DATE);
//解析字符串重新创建对象
LocalDate datell = LocalDate.parse("20181023154312", DateTimeFormatter.ISO_DATE_TIME);
//按照某个模式穿点DateTimeFormatter对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate datelf = LocalDate.parse(s1, formatter);
}
}
内容参考《Java 8 实战》 ,,由衷感谢此书作者为我们提供学习内容