原文:http://coderbee.net/index.php/java/20131015/503
包概述
java.time 包是在JDK8新引入的,提供了用于日期、时间、实例和周期的主要API。
java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。
所有类都是不可变的、线程安全的。
一些类的介绍
-
LocalDateTime:存储了日期和时间,如:
2013-10-15T14:43:14.539
。 -
LocalDate:存储了日期,如:
2013-10-15
。 -
LocalTime:存储了时间,如:
14:43:14.539
。
上面的类可以由下面的类组合来:
类之间转换的示例:
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime :" + localDateTime);
LocalDate localDate = LocalDate.now();
System.out.println("localDate :" + localDate);
LocalTime localtime = LocalTime.now();
System.out.println("localtime :" + localtime);
// 获取当前年份
Year year = Year.now();
System.out.println("year :" + year);
// 从Year获取LocalDate
LocalDate localDate1 = year.atDay(59);
System.out.println("localDate1 :" + localDate1);
// 把LocalTime关联到一个LocalDate得到一个LocalDateTime
LocalDateTime localDateTime1 = localtime.atDate(localDate1);
System.out.println("localDateTime1 :" + localDateTime1);
// 用指定的年获取一个Year
Year year1 = Year.of(2012);
System.out.println("year1 :" + year1);
// 从Year获取YearMonth
YearMonth yearMonth = year1.atMonth(2);
System.out.println("yearMonth :" + yearMonth);
// YearMonth指定日得到 LocalDate
LocalDate localDate2 = yearMonth.atDay(29);
System.out.println("localDate2 :" + localDate2);
// 判断是否是闰年
System.out.println("isLeapYear :" + localDate2.isLeapYear());
//自动处理闰年的2月日期
//创建一个 MonthDay
MonthDay monthDay = MonthDay.of(2, 29);
LocalDate leapYear = monthDay.atYear(2012);
System.out.println("leapYear :" + leapYear);
//同一个 MonthDay 关联到另一个年份上
LocalDate nonLeapYear = monthDay.atYear(2011);
System.out.println("nonLeapYear :" + nonLeapYear);
上面代码的输出结果为:
localDateTime :2013-10-15T15:11:57.489 localDate :2013-10-15 localtime :15:11:57.489 year :2013 localDate1 :2013-02-28 localDateTime1 :2013-02-28T15:11:57.489 year1 :2012 yearMonth :2012-02 localDate2 :2012-02-29 isLeapYear :true leapYear :2012-02-29 nonLeapYear :2011-02-28
格式化与时间计算
-
DateTimeFormatter:在日期对象与字符串之间进行转换。
-
ChronoUnit:计算出两个时间点之间的时间距离,可按多种时间单位计算。
-
TemporalAdjuster:各种日期计算功能。
续前面的代码:
DayOfWeek dayOfWeek = DayOfWeek.of(1);
System.out.println("dayOfWeek :" + dayOfWeek);
//计算两个日期之间的天数,还可以按其他时间单位计算两个时间点之间的间隔。
long between = ChronoUnit.DAYS.between(localDate, leapYear);
System.out.println("between :" + between);
// 线程安全的格式化类,不用每次都new个SimpleDateFormat
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu MM dd");
// 把日期时间转换为字符串标识
System.out.println("date formatter :" + dateTimeFormatter.format(nonLeapYear));
// 解析字符串形式的日期时间
TemporalAccessor temporalAccessor = dateTimeFormatter.parse("2013 01 15");
System.out.println("temporalAccessor :" + LocalDate.from(temporalAccessor));
Instant instant = Instant.now(); // 时间戳
System.out.println("instant :" + instant);
//计算某月的第一天的日期
LocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth());
System.out.println("with :" + with);
// 计算某月的第一个星期一的日期
TemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY);
LocalDate with1 = localDate.with(temporalAdjuster);
System.out.println("with1 :" + with1);
// 计算localDate的下一个星期一的日期
LocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY));
System.out.println("with2 :" + with2);
输出:
dayOfWeek :MONDAY between :-594 date formatter :2011 02 28 temporalAccessor :2013-01-15 instant :2013-10-15T07:55:30.964Z with :2011-02-01 with1 :2013-10-07 with2 :2013-10-21
java.util.Date到新库类的转换
转换可通过下面的方法进行。
Date.toInstant()
Date.from(Instant)
Calendar.toInstant()
方法概览
该包的API提供了大量相关的方法,这些方法一般有一致的方法前缀:
- of:静态工厂方法。
- parse:静态工厂方法,关注于解析。
- get:获取某些东西的值。
- is:检查某些东西的是否是true。
- with:不可变的setter等价物。
- plus:加一些量到某个对象。
- minus:从某个对象减去一些量。
- to:转换到另一个类型。
- at:把这个对象与另一个对象组合起来,例如:
date.atTime(time)
。
想起以前做报表都是在存储过程里处理日期的,因为Java的日期操作确实太弱了。有了Java 8,妈妈再也不用担心我处理日期了。
欢迎关注我的微信公众号: coderbee笔记。
相关推荐
赠送jar包:thymeleaf-extras-java8time-3.0.1.RELEASE.jar; 赠送原API文档:thymeleaf-extras-java8time-3.0.1.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-extras-java8time-3.0.1.RELEASE-sources.jar; 赠送...
赠送jar包:thymeleaf-extras-java8time-3.0.4.RELEASE.jar; 赠送原API文档:thymeleaf-extras-java8time-3.0.4.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-extras-java8time-3.0.4.RELEASE-sources.jar; 赠送...
thymeleaf-extras-java8time-3.0.4.RELEASE.jar
赠送jar包:thymeleaf-extras-java8time-3.0.1.RELEASE.jar; 赠送原API文档:thymeleaf-extras-java8time-3.0.1.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-extras-java8time-3.0.1.RELEASE-sources.jar; 赠送...
赠送jar包:thymeleaf-extras-java8time-3.0.4.RELEASE.jar; 赠送原API文档:thymeleaf-extras-java8time-3.0.4.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-extras-java8time-3.0.4.RELEASE-sources.jar; 赠送...
世界范围内的时区列表。由 java.util.TimeZone 类导出
基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发...
scala-js-java-time, 在JDK8中,java.time的Scala.js 实现 scalajs-java-time scalajs-java-time 是用于的java.time API的bsd许可 reimplementation,它支持在 Scala.js 项目中使用这里 API 。用法只需将以
of time reading the Java source code to learn more about some of the Java topics. Sometimes it took a few months to research a topic before I could write the first sentence on it. It was always fun to...
Concurrent.and.Real.Time.Programming.in.Java.eBook-LiB.chm
这个库包含各种 Kotlin 扩展 Java 8 java.time API
解决mongo数据插入时 报错问题 mogodb插入数据时报错Can't find a codec for class java.math.BigDecimal
Java支持mysql8.0x的jdbc驱动和c3p0库,包含如下三个jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.15.jar mysql-connector-java-8.0.12.jar JDBC驱动类库名称变化 driverClass 由原来的 ...
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 ...org.apache.commons.lang.time.StopWatch.class
Joda-Time是一个强大、易用、高效的日期和时间库,可以利用它把JDK Date和Calendar类完全替换掉无论是计算日期,打印日期、...附件中有最新的jar包,然后有对JodaTime详细的解释,以及对JodaTime一些常见API的调用。
主要给大家分享了关于JAVA String转化成java.sql.date和java.sql.time的方法,文中给出了详细的示例代码,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
This book covers Android app design fundamentals in Android Studio using Java programming language. The author assumes you have no experience in app development. The book starts with the installation ...
java生成二维码(qrcodeutils.java)