JDK14が出ても、姿勢で対処するJdk8時間は約分からないのですか?

現在の時刻:2019年10月24日。何日14時からは、JDK(2020年3月17日)をリリース?

// 距离JDK 14 发布还有多少天?
LocalDate jdk14 = LocalDate.of(2020, 3, 17);
LocalDate nowDate = LocalDate.now();
System.out.println("距离JDK 14 发布还有:"+nowDate.until(jdk14,ChronoUnit.DAYS)+"天");
复制代码

JDK 8が今以上5年が経過しているされていますから、2014年3月18日に正式に利用されています。五年、多くの企業がJDK 8、来年三月JDK14を入れているあなたは本当にまだそれを使用し、JDK 8の新機能を歩んでいますか?私は8 JDKを開始し、一連の記事の使用新機能の概要を記述するつもりだ、フォローアップのJDKが必要な場合は、あなたが公共のブログや数を集中したいと思うかもしれ、更新していきます。

1.処理時間クラス

時間の前に欠陥のための新たな治療ツールをもたらす代わりの処理にJdk8時間。もっとシンプルで使いやすい新しい時間処理前に比べて。

DJK8の授業時間
いくつかの一般的に使用されるクラスのカテゴリがあります。

時間関連クラス 入門
localdatetiへ 時間処理するクラス、最寄りナノ秒まで
LOCALDATE 時間処理クラス、最高精度の日
DateTimeFormatter 時刻形式
するzoneid クラスのタイムゾーンの設定

2.時間の取得

異なるクラスは異なる精度の時間を利用して取得することができます。

/**
 * 时间获取
*/
@Test
public void nowTimeTest() {
    // 当前精确时间
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前精确时间:" + now);
    System.out.println("当前精确时间:" + now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + " " + now.getHour() + "-" + now.getMinute() + "-" + now.getSecond());

    // 获取当前日期
    LocalDate localDate = LocalDate.now();
    System.out.println("当前日期:" + localDate);
    System.out.println("当前日期:" + localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());

    // 获取当天时间
    LocalTime localTime = LocalTime.now();
    System.out.println("当天时间:" + localTime);
    System.out.println("当天时间:" + localTime.getHour() + ":" + localTime.getMinute() + ":" + localTime.getSecond());

    // 有时区的当前精确时间
    ZonedDateTime nowZone = LocalDateTime.now().atZone(ZoneId.systemDefault());
    System.out.println("当前精确时间(有时区):" + nowZone);
    System.out.println("当前精确时间(有时区):" + nowZone.getYear() + "-" + nowZone.getMonthValue() + "-" + nowZone.getDayOfMonth() + " " + nowZone.getHour() + "-" + nowZone.getMinute() + "-" + nowZone.getSecond());
} 
复制代码

取得した時間:

当前精确时间:2019-10-24T00:26:41.724
当前精确时间:2019-10-24 0-26-41
当前日期:2019-10-24
当前日期:2019-10-24
当前精确时间(有时区):2019-10-24T00:26:41.725+08:00[GMT+08:00]
当前精确时间(有时区):2019-10-24 0-26-41
当天时间:00:26:41.725
当天时间:0:26:41
复制代码

時間を作成します3。

毎分クラスは、日付を指定することができ、時間を作成し、また、時間に直接文字列を変換するために使用することができます。

/**
 * 时间创建
 */
@Test
public void createTime() {
    LocalDateTime ofTime = LocalDateTime.of(2019, 10, 1, 8, 8, 8);
    System.out.println("当前精确时间:" + ofTime);

    LocalDate localDate = LocalDate.of(2019, 10, 01);
    System.out.println("当前日期:" + localDate);

    LocalTime localTime = LocalTime.of(12, 01, 01);
    System.out.println("当天时间:" + localTime);
}
复制代码

時間を作成します:

当前精确时间:2019-10-01T08:08:08
当前日期:2019-10-01
当天时间:12:01:01
复制代码

4.時間の変換

/**
* 日期转换
*/
@Test
public void convertTimeTest() {
    LocalDateTime parseTime = LocalDateTime.parse("2019-10-01T22:22:22.222");
    System.out.println("字符串时间转换:" + parseTime);

    LocalDate formatted = LocalDate.parse("20190101", DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println("字符串时间转换-指定格式:" + formatted);

    // Date 转换成 LocalDateTime
    Date date = new Date();
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println("Date 转换成 LocalDateTime:" + LocalDateTime.ofInstant(date.toInstant(), zoneId));

    // LocalDateTime 转换成 Date
    LocalDateTime localDateTime = LocalDateTime.now();
    Date toDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    System.out.println("LocalDateTime 转换成 Date:" + toDate);\
        
    // 当前时间转时间戳
    long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    System.out.println("当前时间转时间戳:" + epochMilli);
    // 时间戳转换成时间
    LocalDateTime epochMilliTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    System.out.println("时间戳转换成时间:" + epochMilliTime);
}
复制代码

変換結果:

字符串时间转换:2019-10-01T22:22:22.222
字符串时间转换-指定格式:2019-01-01
Date 转换成 LocalDateTime:2019-10-24T00:46:41.251
LocalDateTime 转换成 Date:Thu Oct 24 00:46:41 GMT+08:00 2019
当前时间转时间戳:1571849201258
时间戳转换成时间:2019-10-24T00:46:41.258
复制代码

5.時刻のフォーマット

/**
 * 日期格式化
 */
@Test
public void formatTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss")));
}
复制代码

フォーマット後:

当前时间:2019-10-24T00:37:44.867
格式化后:2019-10-24T00:37:44.867
格式化后:2019-10-24
格式化后:00:37:44.867
格式化后:2019-10-24 12:37:44
复制代码

時間の6比較

/**
 * 时间比较
 */
@Test
public void diffTest() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime yestory = now.minusDays(1);
    System.out.println(now + "在" + yestory + "之后吗?" + now.isAfter(yestory));
    System.out.println(now + "在" + yestory + "之前吗?" + now.isBefore(yestory));

    // 时间差
    long day = yestory.until(now, ChronoUnit.DAYS);
    long month = yestory.until(now, ChronoUnit.MONTHS);
    long hours = yestory.until(now, ChronoUnit.HOURS);
    long minutes = yestory.until(now, ChronoUnit.MINUTES);
    System.out.println("相差月份" + month);
    System.out.println("相差天数" + day);
    System.out.println("相差小时" + hours);
    System.out.println("相差分钟" + minutes);

    // 距离JDK 14 发布还有多少天?
    LocalDate jdk14 = LocalDate.of(2020, 3, 17);
    LocalDate nowDate = LocalDate.now();
    System.out.println("距离JDK 14 发布还有:" + nowDate.until(jdk14, ChronoUnit.DAYS) + "天");
}
复制代码

結果の比較:

2019-10-24T00:39:01.589在2019-10-23T00:39:01.589之后吗?true
2019-10-24T00:39:01.589在2019-10-23T00:39:01.589之前吗?false
相差月份0
相差天数1
相差小时24
相差分钟1440
距离JDK 14 发布还有:145天
复制代码

7.加算または減算

/**
 * 日期加减
 */
@Test
public void calcTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:"+now);
    LocalDateTime plusTime = now.plusMonths(1).plusDays(1).plusHours(1).plusMinutes(1).plusSeconds(1);
    System.out.println("增加1月1天1小时1分钟1秒时间后:" + plusTime);
    LocalDateTime minusTime = now.minusMonths(2);
    System.out.println("减少2个月时间后:" + minusTime);
}
复制代码

結果:

当前时间:2019-10-24T00:41:08.877
增加1月1天1小时1分钟1秒时间后:2019-11-25T01:42:09.877
减少2个月时间后:2019-08-24T00:41:08.877	
复制代码

拡散時間の8 A法

/**
 * 时间方法
 */
@Test
public void timeFunctionTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    // 第一天
    LocalDateTime firstDay = now.withDayOfMonth(1);
    System.out.println("本月第一天:" + firstDay);
    // 当天最后一秒
    LocalDateTime lastSecondOfDay = now.withHour(23).withMinute(59).withSecond(59);
    System.out.println("当天最后一秒:" + lastSecondOfDay);
    LocalDateTime lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("This month last day:" + lastDay);
    // 是否闰年
    System.out.println("今年是否run年:" + Year.isLeap(now.getYear()));
  
}
复制代码

出力:

当前时间:2019-10-24T00:43:28.296
本月第一天:2019-10-01T00:43:28.296
当天最后一秒:2019-10-24T23:59:59.296
This month last day:2019-10-31T00:43:28.296
今年是否run年:false
复制代码

クラスは、より便利で簡単な使用する前に比べて、JDK 8新しい時間。

JDK8時間クラスの前に

JDK 8は、独立したパッケージとして処理時間に入れて、クラスを区別するために別の名前を使用します。むしろ同じクラス名別のパッケージ以前より。より明確に使用するように。

コードはにアップロードされたのGithub(https://github.com/niumoo/jdk-feature)

<エンド>
ウェブサイト:www.codingme.net
あなたがこの記事を楽しんでいる場合、あなたが一緒に成長、公共の数を集中することができます。ノー世間の注目は、コアJavaのインタビュー仕上げ&コアな情報のネットワーク全体最もホットな知識のリソースへのアクセスルーチンに応答することはできません。

いいえ公共ありません

おすすめ

転載: juejin.im/post/5dce0848518825107d3e4068