Date Time series features Java API packages in java.time new date and time API classes 7 ----- Jdk8

1. invariance

The new date / time API, all classes are immutable, which is good for multi-threaded environment.

For example: LocalDateTime

 

 

 

2. Separation of Concerns

The new API human-readable and machine time date and time (unix timestamp) a clear separation, which is the date (a Date), the time (Time), the date and time (the DateTime), timestamp (unix timestamp) and defines a different time zone class.

At different times broken down into various categories, such as: LocalDate, LocalTime, LocalDateTime, Instant, Year, Month, YearMonth, MonthDay, DayOfWeek, etc., to meet the various needs of different scenarios.

 

3. Clear

In all classes, methods are clearly defined in order to accomplish the same behavior.

For example, to get the current instance, we can use now () method, in all the classes are defined in the format () and parse () method, rather than have a separate special class as before. Methods clear, clear, unified, easy to remember.

 

4. Practical Operation

(Equivalent to a lot of tools methods, we no longer need to own wraps): All new date / time API classes implement a range of methods to perform common tasks, such as: addition, subtraction, formatting, parsing, from the date / time extracting separate portions, and the like.

For example: LocalDateTime, contains very rich practical operation (conversion, obtaining each field, field modification, increase and decrease, etc.).

 

 

5.TemporalAdjuster allows you to use a more sophisticated way to manipulate dates

You can only change it is no longer limited to a value, and you can define your own date converter according to demand. For example: the change of date to next Sunday, the next working day, or the last day of the month.

lastDayOfMonth create a new date, the last day of the month of its value.

 

6. Comparison disadvantage Jdk7 previous date and time and improved class

6.1 a Date and Calendar are inconvenient to use problem

(1)new Date(2019,01,01)实际是3919年2月。因为Date的构造函数 的年份表示的始于1900年的差值。

  LocalDate创建实例:

LocalDate localDate = LocalDate.of(2019, 1, 1);

(2)month是从0开始的。

LocalDate month是从1开始的:

        LocalDate localDate = LocalDate.of(2019, 1, 1);
        System.out.println(localDate.getMonthValue());

输出是1

(3)DAY_OF_WEEK 的取值,是从周日(1)开始的。

LocalDate week是从周一(1)开始的:

        LocalDate localDate = LocalDate.of(2019, 1, 1);
        System.out.println(localDate.getDayOfWeek());
        System.out.println(localDate.getDayOfWeek().getValue());

输出

TUESDAY
2

 

(4)Date如果不格式化,打印出的日期可读性差。

LocalDate的输出,清晰。

        LocalDate localDate = LocalDate.of(2019, 1, 1);
        System.out.println(localDate.getMonthValue());
        System.out.println(localDate.toString());

输出:

2019-01-01

(5)日期类并不提供国际化,没有时区支持

java8的时间类都支持了时区操作。

例如:LocalDateTime

         //中国时间,输出时不包含时区
         LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
         System.out.println(ldt);
        //意大利罗马时间,输出时包含时区
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Rome"));
        System.out.println(zdt);

输出

2019-12-20T23:17:07.914
2019-12-20T16:17:07.915+01:00[Europe/Rome]

6.2 线程安全问题

(1)Date、Calendar可变性,线程安全问题

java8中所有类都是final修饰的,每次修改都会生成新的副本。

(2)DateFormat和SimpleDateFormat线程安全问题

java8中的DateTimeFormatter也是不可变的,源码:

 

 6.3 java8对日期api进行系统的设计,增加了许多实用方便的操作,几乎不用再使用dateutil额外工具类。

比如,增加一天。

        LocalDate localDate = LocalDate.of(2019, 1, 1);
        LocalDate localDate2 = localDate.plusDays(1);
        System.out.println("localDate:"+localDate);
        System.out.println("localDate2:"+localDate2);

输出:

localDate:2019-01-01
localDate2:2019-01-02

 

 

参考:https://blog.csdn.net/wangsun300/article/details/103403490

Guess you like

Origin www.cnblogs.com/xkzhangsanx/p/12075575.html