Java8中的日期时间

来源:互联网 发布:围攻龙刃堡 mac 编辑:程序博客网 时间:2024/05/16 17:47

一.Java 8 日期时间的API

JDK 8中引入了一个新的Joda Time api,包含java.timejava.time包有几个重要的类.1.LocalDate:代表以日期为单位的日期,对于无时间表示日期很有用。2.LocalTime:表示当天的时间。3.LocalDateTime:处理日期和时间

二.使用LocalDate和LocalTime

LocalDate,LocalTime或LocalDateTime类不能用新的运算符实例化。要获取当前的Date和Time,我们可以使用LocalDate和LocalTime类的静态方法 now()获得。
package com.insping;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;public class DateTimeDemo {    public static void main(String[] args) {        LocalDate date = LocalDate.now();        System.out.println(date);        LocalTime time = LocalTime.now();        System.out.println(time);        LocalDateTime dateTime = LocalDateTime.now();        System.out.println(dateTime);    }}
结果:2017-06-1614:52:25.0882017-06-16T14:52:25.088

三.格式化日期和时间

LocalDate,LocalTime和LocalDateTime类提供了一个format()方法.String format(DateTimeFormatter fmt)其中DateTimeFormatter参数可以使用下列工厂方法:    static DateTimeFormatter ofLocalizedDate(FormatStyle style)    static DateTimeFormatter ofLocalizedTime(FormatStyle style)    static DateTimeFormatter ofLocalizedDateTime(FormatStyle style)其中FormatStyle参数是一个定义以下常量的枚举:FULL,LONG,MEDIUM,SHORT

示例:

package com.insping;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.format.DateTimeFormatter;import java.time.format.FormatStyle;public class DateTimeFormatDemo {    public static void main(String[] args) {        LocalDate date = LocalDate.now();        System.out.println(date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));        LocalTime time = LocalTime.now();        System.out.println(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)));        LocalDateTime dateTime = LocalDateTime.now();        System.out.println(dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));    }}

结果:

2017年6月16日 星期五下午2:582017-6-16 14:58:55

四.从指定的字符串获取日期

LocalDate类包含一个解析方法,我们可以使用它使用特定格式化程序从文本字符串中获取LocalDate的实例。
    public static LocalDate parse(CharSequence text)    public static LocalDate parse(CharSequence text,DateTimeFormatter formatter)
我们同样也可以在Time和LocalDateTime类中使用parse()方法来解析时间或日期时间。如果由于格式不正确而无法解析文本,则parse()方法会抛出DateTimeParseException异常。

示例:

import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class DateTimeFormatDemo {    public static void main(String[] args) {        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");        LocalDate date = LocalDate.parse("01/01/2016", dateFormatter);        System.out.println("Parsed date is " + date);        DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("MM dd yyyy HH:mm");        LocalDateTime dateTime = LocalDateTime.parse("01 01 2017 12:01", dateTimeformatter);        System.out.println("Parsed datetime is " + dateTime);    }}

结果:

Parsed date is 2016-01-01Parsed datetime is 2017-01-01T12:01

五.日期时间计算

LocalDate和LocalTime类具有内置的计算方法。

示例:

import java.time.LocalDate;import java.time.LocalTime;public class Test {    public static void main(String[] args) {        LocalDate date = LocalDate.now();        System.out.println("Today = " + date);        System.out.println("Today + 7 days = " + date.plusDays(7));        System.out.println("Today + 2 weeks = " + date.plusWeeks(2));        LocalTime time = LocalTime.now();        System.out.println("\nCurrent time = " + time);        System.out.println("Current Time +  2 hours = " + time.plusHours(2));        System.out.println("Current Time + 15 mins = " + time.plusMinutes(15));    }}

结果:

Today = 2017-06-16Today + 7 days = 2017-06-23Today + 2 weeks = 2017-06-30Current time = 15:12:33.941Current Time +  2 hours = 17:12:33.941Current Time + 15 mins = 15:27:33.941
原创粉丝点击