本文主要是介绍java 8 LocalDateTime,DateTimeFormatter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
5. 【强制】SimpleDateFormat是线程不安全的类,一般不要定义为 static变量,如果定义为
static,必须加锁,或者使用 DateUtils工具类。
正例:注意线程安全,使用 DateUtils。亦推荐如下处理:
privatestatic final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
说明:如果是 JDK8的应用,可以使用Instant代替 Date,LocalDateTime代替 Calendar,
-
格式和解析模式
模式基于简单的字母和符号序列。 使用模式创建一个格式化器使用ofPattern(String)
和ofPattern(String, Locale)
方法。 例如,"d MMM uuuu"
将格式为2011-12-03,为“2011年12月3日”。 从模式创建的格式化程序可以根据需要多次使用,它是不可变的并且是线程安全的。例如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");String text = date.toString(formatter);LocalDate date = LocalDate.parse(text, formatter);
一.在Java 8中将Date转换为LocalDateTime
方法1:
将Date转换为LocalDatetime,我们可以使用以下方法:
- 1
- 2
- 3
示例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
结果:
- 1
- 2
- 3
方法2:
我们也可以使用LocalDateTime的FactoryInput()方法使用系统的默认时区。
- 1
二.在Java 8中将LocalDateTime转换为Date
要将LocalDateTime转换回java.util.Date,我们可以使用以下步骤:
- 1
- 2
- 3
示例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
结果:
在Java 8中如何处理日期和时间
常有人问我学习一个新库的最好方式是什么?我的答案是在实际项目中使用它。项目中有很多真正的需求驱使开发者去发掘并学习新库。简单得说就是任务驱动学习探 索。这对Java 8新日期时间API也不例外。我创建了20个基于任务的实例来学习Java 8的新特性。从最简单创建当天的日期开始,然后创建时间及时区,接着模拟一个日期提醒应用中的任务——计算重要日期的到期天数,例如生日、纪念日、账单 日、保费到期日、信用卡过期日等。
Java 8 中的 LocalDate 用于表示当天日期。和java.util.Date不同,它只有日期,不包含时间。当你仅需要表示日期时就用这个类。
1 2 | LocalDate today = LocalDate.now(); System.out.println( "Today's Local date : " + today); |
1 2 | Output Today's Local date : 2014-01-14 |
上面的代码创建了当天的日期,不含时间信息。打印出的日期格式非常友好,不像老的Date类打印出一堆没有格式化的信息。
示例 2、在Java 8中获取年、月、日信息
LocalDate类提供了获取年、月、日的快捷方法,其实例还包含很多其它的日期属性。通过调用这些方法就可以很方便的得到需要的日期信息,不用像以前一样需要依赖java.util.Calendar类了。
1 2 3 4 5 | LocalDate today = LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); System.out.printf( "Year : %d Month : %d day : %d t %n" , year, month, day); |
1 2 3 | Output Today's Local date : 2014-01-14 Year : 2014 Month : 1 day : 14 |
看到了吧,在Java 8 中得到年、月、日信息是这么简单直观,想用就用,没什么需要记的。对比看看以前Java是怎么处理年月日信息的吧。
示例 3、在Java 8中处理特定日期
在 第一个例子里,我们通过静态工厂方法now()非常容易地创建了当天日期,你还可以调用另一个有用的工厂方法LocalDate.of()创建任意日期, 该方法需要传入年、月、日做参数,返回对应的LocalDate实例。这个方法的好处是没再犯老API的设计错误,比如年度起始于1900,月份是从0开 始等等。日期所见即所得,就像下面这个例子表示了1月14日,没有任何隐藏机关。
1 2 | LocalDate dateOfBirth = LocalDate.of( 2010 , 01 , 14 ); System.out.println( "Your Date of birth is : " + dateOfBirth); |
1 | Output : Your Date of birth is : 2010-01-14 |
可以看到创建的日期完全符合预期,与你写入的2010年1月14日完全一致。
示例 4、在Java 8中判断两个日期是否相等
现 实生活中有一类时间处理就是判断两个日期是否相等。你常常会检查今天是不是个特殊的日子,比如生日、纪念日或非交易日。这时就需要把指定的日期与某个特定 日期做比较,例如判断这一天是否是假期。下面这个例子会帮助你用Java 8的方式去解决,你肯定已经想到了,LocalDate重载了equal方法,请看下面的例子:
1 2 3 4 | LocalDate date1 = LocalDate.of( 2014 , 01 , 14 ); if (date1.equals(today)){ System.out.printf( "Today %s and date1 %s are same date %n" , today, date1); } |
1 2 | Output today 2014-01-14 and date1 2014-01-14 are same date |
这个例子中我们比较的两个日期相同。注意,如果比较的日期是字符型的,需要先解析成日期对象再作判断。对比Java老的日期比较方式,你会感到清风拂面。
示例 5、在Java 8中检查像生日这种周期性事件
Java 中另一个日期时间的处理就是检查类似每月账单、结婚纪念日、EMI日或保险缴费日这些周期性事件。如果你在电子商务网站工作,那么一定会有一个模块用来在 圣诞节、感恩节这种节日时向客户发送问候邮件。Java中如何检查这些节日或其它周期性事件呢?答案就是MonthDay类。这个类组合了月份和日,去掉 了年,这意味着你可以用它判断每年都会发生事件。和这个类相似的还有一个YearMonth类。这些类也都是不可变并且线程安全的值类型。下面我们通过 MonthDay来检查周期性事件:
1 2 3 4 5 6 7 8 9 | LocalDate dateOfBirth = LocalDate.of( 2010 , 01 , 14 ); MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth()); MonthDay currentMonthDay = MonthDay.from(today); if (currentMonthDay.equals(birthday)){ System.out.println( "Many Many happy returns of the day !!" ); } else { System.out.println( "Sorry, today is not your birthday" ); } |
1 2 | Output: Many Many happy returns of the day !! |
只要当天的日期和生日匹配,无论是哪一年都会打印出祝贺信息。你可以把程序整合进系统时钟,看看生日时是否会受到提醒,或者写一个单元测试来检测代码是否运行正确。
示例 6、在Java 8中获取当前时间
与Java 8获取日期的例子很像,获取时间使用的是LocalTime类,一个只有时间没有日期的LocalDate近亲。可以调用静态工厂方法now()来获取当前时间。默认的格式是hh:mm:ss:nnn。对比一下Java 8之前获取当前时间的方式。
1 2 | LocalTime time = LocalTime.now(); System.out.println( "local time now : " + time); |
1 2 | Output local time now : 16:33:33.369 // in hour, minutes, seconds, nano seconds |
可以看到当前时间就只包含时间信息,没有日期。
示例 7、如何在现有的时间上增加小时
通过增加小时、分、秒来计算将来的时间很常见。Java 8除了不变类型和线程安全的好处之外,还提供了更好的plusHours()方法替换add(),并且是兼容的。注意,这些方法返回一个全新的LocalTime实例,由于其不可变性,返回后一定要用变量赋值。
1 2 3 | LocalTime time = LocalTime.now(); LocalTime newTime = time.plusHours( 2 ); // adding two hours System.out.println( "Time after 2 hours : " + newTime); |
1 2 | Output : Time after 2 hours : 18:33:33.369 |
可以看到,新的时间在当前时间16:33:33.369的基础上增加了2个小时。和旧版Java的增减时间的处理方式对比一下,看看哪种更好。
示例 8、如何计算一周后的日期
和上个例子计算两小时以后的时间类似,这个例子会计算一周后的日期。LocalDate日期不包含时间信息,它的plus()方法用来增加天、周、月,ChronoUnit类声明了这些时间单位。由于LocalDate也是不变类型,返回后一定要用变量赋值。
1 2 3 | LocalDate nextWeek = today.plus( 1 , ChronoUnit.WEEKS); System.out.println( "Today is : " + today); System.out.println( "Date after 1 week : " + nextWeek); |
1 2 3 | Output: Today is : 2014-01-14 Date after 1 week : 2014-01-21 |
可以看到新日期离当天日期是7天,也就是一周。你可以用同样的方法增加1个月、1年、1小时、1分钟甚至一个世纪,更多选项可以查看Java 8 API中的ChronoUnit类。
示例 9、计算一年前或一年后的日期
继续上面的例子,上个例子中我们通过LocalDate的plus()方法增加天数、周数或月数,这个例子我们利用minus()方法计算一年前的日期。
1 2 3 4 5 | LocalDate previousYear = today.minus( 1 , ChronoUnit.YEARS); System.out.println( "Date before 1 year : " + previousYear); LocalDate nextYear = today.plus( 1 , YEARS); System.out.println( "Date after 1 year : " + nextYear); |
1 2 3 | Output: Date before 1 year : 2013-01-14 Date after 1 year : 2015-01-14 |
例子结果得到了两个日期,一个2013年、一个2015年、分别是2014年的前一年和后一年。
示例 10、使用Java 8的Clock时钟类
Java 8增加了一个Clock时钟类用于获取当时的时间戳,或当前时区下的日期时间信息。以前用到System.currentTimeInMillis()和TimeZone.getDefault()的地方都可用Clock替换。
1 2 3 4 5 6 7 | // Returns the current time based on your system clock and set to UTC. Clock clock = Clock.systemUTC(); System.out.println( "Clock : " + clock); // Returns time based on system clock zone Clock defaultClock = Clock.systemDefaultZone(); System.out.println( "Clock : " + clock); |
1 2 3 | Output: Clock : SystemClock[Z] Clock : SystemClock[Z] |
还可以针对clock时钟做比较,像下面这个例子:
1 2 3 4 5 6 7 8 9 | public class MyClass { private Clock clock; // dependency inject ... public void process(LocalDate eventDate) { if (eventDate.isBefore(LocalDate.now(clock)) { ... } } } |
这种方式在不同时区下处理日期时会非常管用。
示例 11、如何用Java判断日期是早于还是晚于另一个日期
另一个工作中常见的操作就是如何判断给定的一个日期是大于某天还是小于某天?在Java 8中,LocalDate类有两类方法isBefore()和isAfter()用于比较日期。调用isBefore()方法时,如果给定日期小于当前日期则返回true。
1 2 3 4 5 6 7 8 9 10 | LocalDate tomorrow = LocalDate.of( 2014 , 1 , 15 ); if (tommorow.isAfter(today)){ System.out.println( "Tomorrow comes after today" ); } LocalDate yesterday = today.minus( 1 , DAYS); if (yesterday.isBefore(today)){ System.out.println( "Yesterday is day before today" ); } |
1 2 3 | Output: Tomorrow comes after today Yesterday is day before today |
在Java 8中比较日期非常方便,不需要使用额外的Calendar类来做这些基础工作了。
示例 12、在Java 8中处理时区
Java 8不仅分离了日期和时间,也把时区分离出来了。现在有一系列单独的类如ZoneId来处理特定时区,ZoneDateTime类来表示某时区下的时间。这在Java 8以前都是 GregorianCalendar类来做的。下面这个例子展示了如何把本时区的时间转换成另一个时区的时间。
1 2 3 4 5 | // Date and time with timezone in Java 8 ZoneId america = ZoneId.of( "America/New_York" ); LocalDateTime localtDateAndTime = LocalDateTime.now(); ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america ); System.out.println( "Current date and time in a particular timezone : " + dateAndTimeInNewYork); |
1 2 | Output : Current date and time in a particular timezone : 2014-01-14T16:33:33.373-05:00[America /New_York ] |
和以前使用GMT的方式转换本地时间对比一下。注意,在Java 8以前,一定要牢牢记住时区的名称,不然就会抛出下面的异常:
1 2 3 4 5 6 | Exception in thread "main" java. time .zone.ZoneRulesException: Unknown time -zone ID: ASIA /Tokyo at java. time .zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:272) at java. time .zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:227) at java. time .ZoneRegion.ofId(ZoneRegion.java:120) at java. time .ZoneId.of(ZoneId.java:403) at java. time .ZoneId.of(ZoneId.java:351) |
示例 13、如何表示信用卡到期这类固定日期,答案就在YearMonth
与 MonthDay检查重复事件的例子相似,YearMonth是另一个组合类,用于表示信用卡到期日、FD到期日、期货期权到期日等。还可以用这个类得到 当月共有多少天,YearMonth实例的lengthOfMonth()方法可以返回当月的天数,在判断2月有28天还是29天时非常有用。
1 2 3 4 | YearMonth currentYearMonth = YearMonth.now(); System.out.printf( "Days in month year %s: %d%n" , currentYearMonth, currentYearMonth.lengthOfMonth()); YearMonth creditCardExpiry = YearMonth.of( 2018 , Month.FEBRUARY); System.out.printf( "Your credit card expires on %s %n" , creditCardExpiry); |
1 2 3 | Output: Days in month year 2014-01: 31 Your credit card expires on 2018-02 |
根据上述数据,你可以提醒客户信用卡快要到期了,个人认为这个类非常有用。
示例 14、如何在Java 8中检查闰年
LocalDate类有一个很实用的方法isLeapYear()判断该实例是否是一个闰年,如果你还是想重新发明轮子,这有一个代码示例,纯Java逻辑编写的判断闰年的程序。
1 2 3 4 5 | if (today.isLeapYear()){ System.out.println( "This year is Leap year" ); } else { System.out.println( "2014 is not a Leap year" ); } |
1 2 | Output: 2014 is not a Leap year |
你可以多写几个日期来验证是否是闰年,最好是写JUnit单元测试做判断。
示例 15、计算两个日期之间的天数和月数
有一个常见日期操作是计算两个日期之间的天数、周数或月数。在Java 8中可以用java.time.Period类来做计算。下面这个例子中,我们计算了当天和将来某一天之间的月数。
1 2 3 4 | LocalDate java8Release = LocalDate.of( 2014 , Month.MARCH, 14 ); Period periodToNextJavaRelease = Period.between(today, java8Release); System.out.println( "Months left between today and Java 8 release : " + periodToNextJavaRelease.getMonths() ); |
1 2 | Output: Months left between today and Java 8 release : 2 |
从上面可以看到现在是一月,Java 8的发布日期是3月,中间相隔两个月。
示例 16、包含时差信息的日期和时间
在Java 8中,ZoneOffset类用来表示时区,举例来说印度与GMT或UTC标准时区相差+05:30,可以通过ZoneOffset.of()静态方法来 获取对应的时区。一旦得到了时差就可以通过传入LocalDateTime和ZoneOffset来创建一个OffSetDateTime对象。
1 2 3 4 | LocalDateTime datetime = LocalDateTime.of( 2014 , Month.JANUARY, 14 , 19 , 30 ); ZoneOffset offset = ZoneOffset.of( "+05:30" ); OffsetDateTime date = OffsetDateTime.of(datetime, offset); System.out.println( "Date and Time with timezone offset in Java : " + date); |
1 2 | Output : Date and Time with timezone offset in Java : 2014-01-14T19:30+05:30 |
现在的时间信息里已经包含了时区信息了。注意:OffSetDateTime是对计算机友好的,ZoneDateTime则对人更友好。
示例 17、在Java 8中获取当前的时间戳
如果你还记得Java 8以前是如何获得当前时间戳,那么现在你终于解脱了。Instant类有一个静态工厂方法now()会返回当前的时间戳,如下所示:
1 2 | Instant timestamp = Instant.now(); System.out.println( "What is value of this instant " + timestamp); |
1 2 | Output : What is value of this instant 2014-01-14T08:33:33.379Z |
时间戳信息里同时包含了日期和时间,这和java.util.Date很像。实际上Instant类确实等同于 Java 8之前的Date类,你可以使用Date类和Instant类各自的转换方法互相转换,例如:Date.from(Instant) 将Instant转换成java.util.Date,Date.toInstant()则是将Date类转换成Instant类。
示例 18、在Java 8中如何使用预定义的格式化工具去解析或格式化日期
在Java 8以前的世界里,日期和时间的格式化非常诡异,唯一的帮助类SimpleDateFormat也是非线程安全的,而且用作局部变量解析和格式化日期时显得很笨重。幸好线程局部变量能使它在多线程环境中变得可用,不过这都是过去时了。Java 8引入了全新的日期时间格式工具,线程安全而且使用方便。它自带了一些常用的内置格式化工具。下面这个例子使用了BASIC_ISO_DATE格式化工具将2014年1月14日格式化成20140114。
1 2 3 4 5 | String dayAfterTommorrow = "20140116" ; LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE); System.out.printf( "Date generated from String %s is %s %n" , dayAfterTommorrow, formatted); |
1 2 | Output : Date generated from String 20140116 is 2014-01-16 |
很明显的看出得到的日期和给出的日期是同一天,但是格式不同。
示例 19、如何在Java中使用自定义格式化工具解析日期
上 个例子使用了Java内置的格式化工具去解析日期字符串。尽管内置格式化工具很好用,有时还是需要定义特定的日期格式,下面这个例子展示了如何创建自定义 日期格式化工具。例子中的日期格式是“MMM dd yyyy”。可以调用DateTimeFormatter的 ofPattern()静态方法并传入任意格式返回其实例,格式中的字符和以前代表的一样,M 代表月,m代表分。如果格式不规范会抛出 DateTimeParseException异常,不过如果只是把M写成m这种逻辑错误是不会抛异常的。
1 2 3 4 5 6 7 8 9 | String goodFriday = "Apr 18 2014" ; try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MMM dd yyyy" ); LocalDate holiday = LocalDate.parse(goodFriday, formatter); System.out.printf( "Successfully parsed String %s, date is %s%n" , goodFriday, holiday); } catch (DateTimeParseException ex) { System.out.printf( "%s is not parsable!%n" , goodFriday); ex.printStackTrace(); } |
1 2 | Output : Successfully parsed String Apr 18 2014, date is 2014-04-18 |
日期值与传入的字符串是匹配的,只是格式不同而已。
示例 20、在Java 8中如何把日期转换成字符串
上 两个例子都用到了DateTimeFormatter类,主要是从字符串解析日期。现在我们反过来,把LocalDateTime日期实例转换成特定格式的字符串。这是迄今为止Java日期转字符串最为简单的方式了。下面的例子将返回一个代表日期的格式化字符串。和前面类似,还是需要创建 DateTimeFormatter实例并传入格式,但这回调用的是format()方法,而非parse()方法。这个方法会把传入的日期转化成指定格式的字符串。
1 2 3 4 5 6 7 8 9 | LocalDateTime arrivalDate = LocalDateTime.now(); try { DateTimeFormatter format = DateTimeFormatter.ofPattern( "MMM dd yyyy hh:mm a" ); String landing = arrivalDate.format(format); System.out.printf( "Arriving at : %s %n" , landing); } catch (DateTimeException ex) { System.out.printf( "%s can't be formatted!%n" , arrivalDate); ex.printStackTrace(); } |
1 | Output : Arriving at : Jan 14 2014 04:33 PM |
当前时间被指定的“MMM dd yyyy hh:mm a”格式格式化,格式包含3个代表月的字符串,时间后面带有AM和PM标记。
Java 8日期时间API的重点
通过这些例子,你肯定已经掌握了Java 8日期时间API的新知识点。现在我们来回顾一下这个优雅API的使用要点:
1)提供了javax.time.ZoneId 获取时区。
2)提供了LocalDate和LocalTime类。
3)Java 8 的所有日期和时间API都是不可变类并且线程安全,而现有的Date和Calendar API中的java.util.Date和SimpleDateFormat是非线程安全的。
4)主包是 java.time,包含了表示日期、时间、时间间隔的一些类。里面有两个子包java.time.format用于格式化, java.time.temporal用于更底层的操作。
5)时区代表了地球上某个区域内普遍使用的标准时间。每个时区都有一个代号,格式通常由区域/城市构成(Asia/Tokyo),在加上与格林威治或 UTC的时差。例如:东京的时差是+09:00。
6)OffsetDateTime类实际上组合了LocalDateTime类和ZoneOffset类。用来表示包含和格林威治或UTC时差的完整日期(年、月、日)和时间(时、分、秒、纳秒)信息。
7)DateTimeFormatter 类用来格式化和解析时间。与SimpleDateFormat不同,这个类不可变并且线程安全,需要时可以给静态常量赋值。 DateTimeFormatter类提供了大量的内置格式化工具,同时也允许你自定义。在转换方面也提供了parse()将字符串解析成日期,如果解析出错会抛出DateTimeParseException。DateTimeFormatter类同时还有format()用来格式化日期,如果出错会抛出DateTimeException异常。
8)再补充一点,日期格式“MMM d yyyy”和“MMM dd yyyy”有一些微妙的不同,第一个格式可以解析“Jan 2 2014”和“Jan 14 2014”,而第二个在解析“Jan 2 2014”就会抛异常,因为第二个格式里要求日必须是两位的。如果想修正,你必须在日期只有个位数时在前面补零,就是说“Jan 2 2014”应该写成 “Jan 02 2014”。
两者最大的区别是,Java8的DateTimeFormatter是线程安全的,而SimpleDateFormat并不是线程安全。
二 DateTimeFormatter完成格式化
1 代码示例
- import java.time.*;
- import java.time.format.*;
- public class NewFormatterTest
- {
- public static void main(String[] args)
- {
- DateTimeFormatter[] formatters = new DateTimeFormatter[]{
- // 直接使用常量创建DateTimeFormatter格式器
- DateTimeFormatter.ISO_LOCAL_DATE,
- DateTimeFormatter.ISO_LOCAL_TIME,
- DateTimeFormatter.ISO_LOCAL_DATE_TIME,
- // 使用本地化的不同风格来创建DateTimeFormatter格式器
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM),
- DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG),
- // 根据模式字符串来创建DateTimeFormatter格式器
- DateTimeFormatter.ofPattern("Gyyyy%%MMM%%dd HH:mm:ss")
- };
- LocalDateTime date = LocalDateTime.now();
- // 依次使用不同的格式器对LocalDateTime进行格式化
- for(int i = 0 ; i < formatters.length ; i++)
- {
- // 下面两行代码的作用相同
- System.out.println(date.format(formatters[i]));
- System.out.println(formatters[i].format(date));
- }
- }
- }
2 运行结果
2016-09-04
2016-09-04
12:18:33.557
12:18:33.557
2016-09-04T12:18:33.557
2016-09-04T12:18:33.557
2016年9月4日 星期日 12:18:33
2016年9月4日 星期日 12:18:33
下午12时18分33秒
下午12时18分33秒
公元2016%%九月%%04 12:18:33
公元2016%%九月%%04 12:18:33
3 代码说明
上面代码使用3种方式创建了6个DateTimeFormatter对象,然后程序中使用不同方式来格式化日期。
三 DateTimeFormatter解析字符串
1 代码示例
- import java.time.*;
- import java.time.format.*;
- public class NewFormatterParse
- {
- public static void main(String[] args)
- {
- // 定义一个任意格式的日期时间字符串
- String str1 = "2014==04==12 01时06分09秒";
- // 根据需要解析的日期、时间字符串定义解析所用的格式器
- DateTimeFormatter fomatter1 = DateTimeFormatter
- .ofPattern("yyyy==MM==dd HH时mm分ss秒");
- // 执行解析
- LocalDateTime dt1 = LocalDateTime.parse(str1, fomatter1);
- System.out.println(dt1); // 输出 2014-04-12T01:06:09
- // ---下面代码再次解析另一个字符串---
- String str2 = "2014$$$四月$$$13 20小时";
- DateTimeFormatter fomatter2 = DateTimeFormatter
- .ofPattern("yyy$$$MMM$$$dd HH小时");
- LocalDateTime dt2 = LocalDateTime.parse(str2, fomatter2);
- System.out.println(dt2); // 输出 2014-04-13T20:00
- }
- }
2 运行结果
2014-04-12T01:06:09
2014-04-13T20:00
3 代码说明
上面代码定义了两个不同格式日期、时间字符串。为了解析他们,代码分别使用对应的格式字符串创建了DateTimeFormatter对象,这样DateTimeFormatter即可按照格式化字符串将日期、时间字符串解析成LocalDateTime对象。
java.time.LocalDate:
LocalDate只提供日期不提供时间信息。它是不可变类且线程安全的。
package org.smarttechie;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
* */
public class DateTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
//Create date
LocalDate localDate = LocalDate.now();
System.out.println("The local date is :: " + localDate);
//Find the length of the month. That is, how many days are there for this month.
System.out.println("The number of days available for this month:: " + localDate.lengthOfMonth());
//Know the month name
System.out.println("What is the month name? :: " + localDate.getMonth().name());
//add 2 days to the today's date.
System.out.println(localDate.plus(2, ChronoUnit.DAYS));
//substract 2 days from today
System.out.println(localDate.minus(2, ChronoUnit.DAYS));
//Convert the string to date
System.out.println(localDate.parse("2017-04-07"));
}
}
运行结果:

java.time.LocalTime:
LocalTime只提供时间而不提供日期信息,它是不可变类且线程安全的。
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
* */
public class DateTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
//Get local time
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
//Get the hour of the day
System.out.println("The hour of the day:: " + localTime.getHour());
//add 2 hours to the time.
System.out.println(localTime.plus(2, ChronoUnit.HOURS));
//add 6 minutes to the time.
System.out.println(localTime.plusMinutes(6));
//substract 2 hours from current time
System.out.println(localTime.minus(2, ChronoUnit.HOURS));
}
}
运行结果:

java.time.LocalDateTime:
LocalDateTime提供时间和日期的信息,它是不可变类且线程安全的
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
*
*/
public class DateTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
//Get LocalDateTime object
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
//Find the length of month. That is, how many days are there for this month.
System.out.println("The number of days available for this month:: " + localDateTime.getMonth().length(true));
//Know the month name
System.out.println("What is the month name? :: " + localDateTime.getMonth().name());
//add 2 days to today's date.
System.out.println(localDateTime.plus(2, ChronoUnit.DAYS));
//substract 2 days from today
System.out.println(localDateTime.minus(2, ChronoUnit.DAYS));
}
}
运行结果:

java.time.Year:
Year提供年的信息,它是不可变类且线程安全的。
import java.time.Year;
import java.time.temporal.ChronoUnit;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
*
*/
public class DateTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
//Get year
Year year = Year.now();
System.out.println("Year ::" + year);
//know the year is leap year or not
System.out.println("Is year[" +year+"] leap year?"+ year.isLeap());
}
}
运行结果:

java.time.Duration:
Duration是用来计算两个给定的日期之间包含多少秒,多少毫秒,它是不可变类且线程安全的
java.time.Period:
Period是用来计算两个给定的日期之间包含多少天,多少月或者多少年,它是不可变类且线程安全的
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
/**
* This class demonstrates JAVA 8 data and time API
* @author Siva Prasad Rao Janapati
*
*/
public class DateTimeDemonstration {
/**
* @param args
*/
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
Period period = Period.between(localDate, localDate.plus(2, ChronoUnit.DAYS));
System.out.println(period.getDays());
}
}
运行结果:

这篇关于java 8 LocalDateTime,DateTimeFormatter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!