概述
java.time包中提供了许多新类。
LocalDate:获取日期
LocalDate date1 = LocalDate.of(2017, 8, 11);
LocalDate date2 = LocalDate.now();
int year = date1.getYear();
int month = date1.getMonthValue();
int day = date1.getDayOfMonth();
DayOfWeek dayOfWeek = date1.getDayOfWeek();
Month month2 = date1.getMonth();
还可以使用get方法得到值,参数为TemporalField接口,可以传入实现类ChronoField枚举类型。
LocalDate date1 = LocalDate.of(2017, 8, 11);
int year = date1.get(ChronoField.YEAR);
int month = date1.get(ChronoField.MONTH_OF_YEAR);
int day = date1.get(ChronoField.DAY_OF_MONTH);
int dayOfWeek = date1.get(ChronoField.DAY_OF_WEEK);
LocalTime:获取时间
LocalTime time1 = LocalTime.of(12, 12, 12);
LocalTime time2 = LocalTime.now();
int hour = time1.get(ChronoField.HOUR_OF_DAY);
int minute = time1.get(ChronoField.MINUTE_OF_HOUR);
int second = time1.get(ChronoField.SECOND_OF_MINUTE);
int h = time2.getHour();
int m = time2.getMinute();
int s = time2.getSecond();
int n = time2.getNano();
还可以把字符串转为LocalDate和LocalTime:
LocalDate date = LocalDate.parse("2017-01-12");
LocalTime time = LocalTime.parse("11:11:11");
parse方法还可以传递DateTimeFormatter参数。
LocalDateTime:日期和时间
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime1 = LocalDateTime.now();
LocalDateTime dateTime2 = LocalDateTime.of(date, time);
LocalDateTime dateTime3 = LocalDateTime.parse("2017-08-11 11:11:11");
LocalDateTime dateTime4 = date.atTime(time);
LocalDateTime dateTime5 = time.atDate(date);
LocalDate date1 = dateTime1.toLocalDate();
LocalTime time1 = dateTime1.toLocalTime();
Instant类表示的是1970-1-1到现在的秒数。
Instant instant = Instant.now();
Instant instant2 = Instant.ofEpochMilli(System.currentTimeMillis());
Instant instant3 = Instant.ofEpochSecond(instant.getEpochSecond());
Instant instant4 = Instant.parse("2017-01-01T11:11:11.12Z");
Duration和Period是用来计算间隔的。Duration一般用于计算秒与纳秒之间的间隔。Period用于计算日期间隔。
Duration duration1 = Duration.between(LocalTime.of(11, 11), LocalTime.of(11, 12));
Duration duration2 = Duration.ofMinutes(3);
Duration duration3 = Duration.parse("PT15M");//15分钟
Period period1 = Period.between(LocalDate.of(2017, 7, 1), LocalDate.now());
Period period2 = Period.of(1, 1, 1);
Period period3 = Period.ofDays(2);
Period period4 = Period.parse("P-6M3D");//-6月+3天
上面的类都是不可改变对象,和String对象一样。每次修改操作都会生成一个新的对象。
日期时间修改:
LocalDate date = LocalDate.now();
date = date.withDayOfMonth(3);<!--day变为3-->
date = date.with(ChronoField.DAY_OF_MONTH, 11);<!--day为11-->
date = date.minusDays(5);<!--当前日期减去5天-->
date = date.plusDays(12);<!--当前日期加上12天--><!--其他类都有类似操作-->
用TemporalAdjusters类的方法操作日期时间,可以更加灵活的处理日期时间。例如把日期变为这个月的第一天,或者把日期变为下一个星期4
LocalDate date = LocalDate.now();
date = date.with(TemporalAdjusters.lastDayOfMonth());
date = date.with(TemporalAdjusters.dayOfWeekInMonth(7, DayOfWeek.SATURDAY));
时间日期格式化:
LocalDate date = LocalDate.now();
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);//20170811
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);//2017-08-11
String s3 = date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));//11/08/2017
LocalDate date2 = LocalDate.parse("11/08/2017", DateTimeFormatter.ofPattern("dd/MM/yyyy"));
最后
以上就是怕孤独砖头为你收集整理的java8 新的时期和时间API的全部内容,希望文章能够帮你解决java8 新的时期和时间API所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复