码迷,mamicode.com
首页 > 编程语言 > 详细

Java8 本地DateTime API

时间:2017-08-14 11:32:18      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:详细   string   操作   blank   编辑   print   ted   选择   ring   

LocalDate/本地时间和LocalDateTime类简化时区不需要开发。

让我们来看看他们操作。

选择使用任何编辑器创建以下java程序在 C:/> JAVA

Java8Tester.java

import java.time.LocalDate;import java.time.LocalTime;import java.time.LocalDateTime;import java.time.Month;public class Java8Tester {
   public static void main(String args[]){
      Java8Tester java8tester = new Java8Tester();
      java8tester.testLocalDateTime(); 
   }

   public void testLocalDateTime(){
      // Get the current date and time
      LocalDateTime currentTime = LocalDateTime.now();     
      System.out.println("Current DateTime: " + currentTime);

      LocalDate date1 = currentTime.toLocalDate();
      System.out.println("date1: " + date1);
      Month month = currentTime.getMonth();
      int day = currentTime.getDayOfMonth();
      int seconds = currentTime.getSecond();
      System.out.println("Month: " + month         +"day: " + day         +"seconds: " + seconds      );

      LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
      System.out.println("date2: " + date2);

      //12 december 2014
      LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12); 
      System.out.println("date3: " + date3);

      //22 hour 15 minutes
      LocalTime date4 = LocalTime.of(22, 15); 
      System.out.println("date4: " + date4);

      //parse a string
      LocalTime date5 = LocalTime.parse("20:15:30"); 
      System.out.println("date5: " + date5);
   }	}

验证结果

 

Java8 本地DateTime API

标签:详细   string   操作   blank   编辑   print   ted   选择   ring   

原文地址:http://www.cnblogs.com/hane/p/7356610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!