SoftwareEngineering

Java API

java.time

LocalDateTime

truncatedTo メソッド

  • example
    LocalDateTime localDateTime = LocalDateTime.now();
    
    System.out.println("localDateTime                                :" + localDateTime);
    System.out.println("localDateTime.truncatedTo(ChronoUnit.DAYS)   :" + localDateTime.truncatedTo(ChronoUnit.DAYS));
    System.out.println("localDateTime.truncatedTo(ChronoUnit.HOURS)  :" + localDateTime.truncatedTo(ChronoUnit.HOURS));
    System.out.println("localDateTime.truncatedTo(ChronoUnit.MINUTES):" + localDateTime.truncatedTo(ChronoUnit.MINUTES));
    System.out.println("localDateTime.truncatedTo(ChronoUnit.SECONDS):" + localDateTime.truncatedTo(ChronoUnit.SECONDS));
    System.out.println("localDateTime.truncatedTo(ChronoUnit.MILLIS) :" + localDateTime.truncatedTo(ChronoUnit.MILLIS));
  • result
    localDateTime                                :2019-06-15T17:35:16.723566900
    localDateTime.truncatedTo(ChronoUnit.DAYS)   :2019-06-15T00:00
    localDateTime.truncatedTo(ChronoUnit.HOURS)  :2019-06-15T17:00
    localDateTime.truncatedTo(ChronoUnit.MINUTES):2019-06-15T17:35
    localDateTime.truncatedTo(ChronoUnit.SECONDS):2019-06-15T17:35:16
    localDateTime.truncatedTo(ChronoUnit.MILLIS) :2019-06-15T17:35:16.723

型変換

Date => LocalDateTime

  • example
    Date date = new Date();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    
    System.out.println("date         :" + date);
    System.out.println("localDateTime:" + localDateTime);
  • result
    date         :Sat Jun 15 17:51:36 JST 2019
    localDateTime:2019-06-15T17:51:36.151

Date => LocalDate

  • example
    Date date = new Date();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    LocalDate localDate = localDateTime.toLocalDate();
    
    System.out.println("date         :" + date);
    System.out.println("localDateTime:" + localDateTime);
    System.out.println("localDate    :" + localDate);
  • result
    date         :Sat Jun 15 23:08:37 JST 2019
    localDateTime:2019-06-15T23:08:37.014
    localDate    :2019-06-15

LocalDateTime => LocalDate

  • example
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDate localDate = localDateTime.toLocalDate();
    
    System.out.println("localDateTime:" + localDateTime);
    System.out.println("localDate    :" + localDate);
  • result
    localDateTime:2019-06-15T17:51:36.193991600
    localDate    :2019-06-15

LocalDateTime => Date

  • example
    LocalDateTime localDateTime = LocalDateTime.now();
    Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    
    System.out.println("localDateTime:" + localDateTime);
    System.out.println("date         :" + date);
  • result
    localDateTime:2019-06-15T23:08:37.015564600
    date         :Sat Jun 15 23:08:37 JST 2019

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-06-15 (土) 23:12:36 (66d)