台州网站建设企业,网络运维周报,wordpress 调用热门 文章,珠海网站建设q.479185700強新日期时间API出现的背景
jdk8之前时间日期API 如果我们可以跟别人说#xff1a;“我们在1502653933071见面#xff0c;别晚了#xff01;”那么就再简单不过了。但是我们希望时间与昼夜和四季有关#xff0c;于是事情就变复杂了。jdk 1.0中包含了一个java.util.Date类“我们在1502653933071见面别晚了”那么就再简单不过了。但是我们希望时间与昼夜和四季有关于是事情就变复杂了。jdk 1.0中包含了一个java.util.Date类但是它大多数方法已经在jdk 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是 可变性像日期和时间这样的类应该是不可变的。 偏移性Date中的年份是从1900开始的而月份都从0开始。 格式化格式化只对Date有用Calendar则不行。 此外它们也不是线程安全的不能处理闰秒等。 新时间日期API java 8吸收了 joda-Time 的精华以一个新的开始为java创建优秀的API。新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime)和持续时间(Duration) 的类。历史悠久的 Date 类新增了 toInstant() 方法用于把 Date 转换成新的表示形式。这些新增的本地化时间日期API大大简化了日期时间和本地化的管理。 注 joda-Time提供了一组java类包用于处理包括ISO8601标准在内的date和time可以利用它把jdk Date和Calendar类完全替换掉而且仍然能够提供很好的集成目前joda-Time已经纳入 jdk 8 的官方API了。 LocalDateLocalTImeLocalDateTime 1、LocalDateTime相较于LocalDate、localTime使用频率要高 2、类似于Calendar //now():获取当前日期、时间、日期时间 -----------实例化方式一LocalDate localDate LocalDate.now();LocalTime localTime LocalTime.now();LocalDateTime localDateTime LocalDateTime.now();System.out.println(localDate);//2020-04-14System.out.println(localTime);//15:31:53.209System.out.println(localDateTime);//2020-04-14T15:31:53.209//Of():设置指定的年月日时分秒,是没有偏移量的 -----------实例化方式二LocalDateTime localDateTime1 LocalDateTime.of(2012, 2, 2, 13, 14);System.out.println(localDateTime1);//2012-02-02T13:14//getXxx():获取相关属性System.out.println(localDateTime1.getDayOfMonth());//2System.out.println(localDateTime1.getDayOfWeek());//THURSDAYSystem.out.println(localDateTime1.getMonth());//FEBRUARYSystem.out.println(localDateTime1.getMinute());//14//体现不可变性//WithXxx():设置相关属性LocalDateTime localDateTime2 localDateTime1.withDayOfMonth(15);System.out.println(localDateTime1);//2012-02-02T13:14System.out.println(localDateTime2);//2012-02-15T13:14LocalDateTime localDateTime3 localDateTime1.withMinute(50);System.out.println(localDateTime3);//2012-02-02T13:50//plusXxx():加LocalDateTime localDateTime4 localDateTime1.plusMonths(5);System.out.println(localDateTime1);//2012-02-02T13:14System.out.println(localDateTime4);//2012-07-02T13:14//minusXxx():减LocalDateTime localDateTime5 localDateTime1.minusDays(1);System.out.println(localDateTime1);//2012-02-02T13:14System.out.println(localDateTime5);//2012-02-01T13:14瞬时Instant(类似于java.util.Date) Instant时间线上的一个瞬时点。这可能被用来记录应用程序中的事件时间戳。 在处理时间和日期的时候我们通常会想到年月日时分秒。然而这只是时间的一个模型是面向人类的。第二种通用模型是面向机器的或者说是连续的。在此模型中时间线中的一个点表示为一个很大的数这有利于计算机处理。在UNIX中这个数从1970年开始以秒为单位同样的在java中也是从1970年开始但以毫秒为单位。 java.time包通过值类型Instant提供机器视图不提供处理人类意义上的时间单位。Instant表示时间线上的一点而不需要任何上下文信息例如时区。概念上讲它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数。因为java.time包是基于纳秒计算的所以Instant的精度可以达到纳秒级。 public void test2(){//now():获取本初子午线对应的时间标准Instant instant Instant.now();System.out.println(instant);//2020-04-14T13:02:26.572Z 这个时间是本初子午线的时间与我们使用的不同需要添加偏移量//添加时间偏移量OffsetDateTime offsetDateTime instant.atOffset(ZoneOffset.ofHours(8));System.out.println(offsetDateTime);//2020-04-14T21:04:54.99208:00//toEpochMilli()获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ----Date类的getTime()long l instant.toEpochMilli();System.out.println(l);//1586869726432//ofEpochMilli():通过给定的毫秒数获取Instant ---Date(long millis)Instant instant1 Instant.ofEpochMilli(1586869726432L);System.out.println(instant1);}DateTimeFormatter格式化与解析日期或时间
public void test3(){//方式一不常用// 预定义的标准格式。如ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIMEDateTimeFormatter formatter DateTimeFormatter.ISO_LOCAL_DATE_TIME;//格式化:日期--字符串LocalDateTime localDateTime LocalDateTime.now();String str1 formatter.format(localDateTime);System.out.println(localDateTime);//2020-04-14T21:24:58.608System.out.println(str1);//2020-04-14T21:24:58.608//解析字符串 --日期TemporalAccessor parse formatter.parse(2020-04-14T21:24:58.608);System.out.println(parse);//方式二不常用//本地化相关的格式。如ofLocalizedDateTime()//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTimeDateTimeFormatter formatter1 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);//格式化String str2 formatter1.format(localDateTime);System.out.println(str2);//2020年4月14日 下午09时32分27秒//本地化相关的格式。如ofLocalizedDate()// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTimeDateTimeFormatter formatter2 DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);String str3 formatter2.format(LocalDate.now());System.out.println(str3);//2020年4月14日 星期二//方式三常用// 自定义的格式。如ofPattern(yyyy-MM-dd hh:mm:ss E)DateTimeFormatter formatter3 DateTimeFormatter.ofPattern(yyyy-MM-dd hh:mm:ss);//格式化String str4 formatter3.format(LocalDateTime.now());System.out.println(str4);//2020-04-14 09:38:41//解析TemporalAccessor accessor formatter3.parse(2020-04-14 09:38:41);System.out.println(accessor);//{MicroOfSecond0, MilliOfSecond0, SecondOfMinute41, HourOfAmPm9, MinuteOfHour38, NanoOfSecond0},ISO resolved to 2020-04-14}其他时间API
还有很多