Java8 new Time Api

Java8 new Time Api

JAVA8以前Date/Times Api的問題

  1. 線程安全問題
  2. Api 不易理解
  3. Api 轉換複雜

1. Main class

  • LocalDate
  • LocalTime
  • LocalDateTime
  • ZonedDateTime
  • Period
  • Duration

LocalDate 通用API

LocalDate LocalDate = LocalDate.now() // 根據系統時間獲取當前日期的實例
	LocaDate LocalDate = LocalDate.of(2015,02,20) // of 方法獲取LocalDate實例
	LocaDate LocalDate =LocalDate.parse("2015-02-20");  String --> LocalDate實例
	LocalDate tomorrow = LocalDate.now().plusDays(1); //獲取表示明天的LocalDate實例
	DayOfWeek sunday = LocalDate.parse("2016-06-12").getDayOfWeek();
 	int twelve = LocalDate.parse("2016-06-12").getDayOfMonth();
 	boolean leapYear = LocalDate.now().isLeapYear();
 	boolean notBefore = LocalDate.parse("2016-06-12").isBefore(LocalDate.parse("2016-06-11"));
 	boolean isAfter = LocalDate.parse("2016-06-12").isAfter(LocalDate.parse("2016-06-11"));
 	LocalDateTime beginningOfDay = LocalDate.parse("2016-06-12").atStartOfDay();
 	LocalDate firstDayOfMonth = LocalDate.parse("2016-06-12").with(TemporalAdjusters.firstDayOfMonth();

LocalTime 通用API

LocalTime now = LocalTime.now();
	LocalTime sixThirty = LocalTime.parse("06:30");
	LocalTime sixThirty = LocalTime.of(6, 30);
	LocalTime sevenThirty = LocalTime.parse("06:30").plus(1, ChronoUnit.HOURS);
	int six = LocalTime.parse("06:30").getHour();
	boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30"));
	LocalTime maxTime = LocalTime.MAX

LocalDateTime 通用API

LocalDateTime.now();
	LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
	LocalDateTime.parse("2015-02-20T06:30:00");
	localDateTime.plusDays(1);
	localDateTime.minusHours(2);
	localDateTime.getMonth();

Period and Duration 通用API

// Period -->LocalDate的 時間段
	LocalDate initialDate = LocalDate.parse("2007-05-10");
	LocalDate finalDate = initialDate.plus(Period.ofDays(5));
	int five = Period.between(finalDate, initialDate).getDays();
	int five = ChronoUnit.DAYS.between(initialDate , initialDate);


	//Durataion ---> LocalTime的時間段

	LocalTime initialTime = LocalTime.of(6, 30, 0);
 
	LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));
	int thirty = Duration.between(finalTime, initialTime).getSeconds();
	int thirty = ChronoUnit.SECONDS.between(finalTime, initialTime);

與舊API-- Date Calendar兼容

LocalDateTime.ofInstant(dateNow.toInstant(),ZoneId.systemDefault());
	 LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
	// java.util.Date -->LocalDate

    LocalDate nowLocalDate = dateNow.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
    // java.util.Date -->java.sql.Date --> LocalDate

    LocalDate nowLocalDate = new java.sql.Date(dateToConvert.getTime()).toLocalDate();

    //Date --> LocalDateTime

    public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
	}
 
	ocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
	    return new java.sql.Timestamp(
	      dateToConvert.getTime()).toLocalDateTime();
	}


	// LocalDate --- > java.util.Date
	public Date convertToDateViaSqlDate(LocalDate dateToConvert) {
    	return java.sql.Date.valueOf(dateToConvert);
	}	

	public Date convertToDateViaInstant(LocalDate dateToConvert) {
	    return java.util.Date.from(dateToConvert.atStartOfDay()
	      .atZone(ZoneId.systemDefault())
	      .toInstant());
	}


	// LocalDateTime --- > java.util.Date
	public Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
    	
	}

	Date convertToDateViaInstant(LocalDateTime dateToConvert) {
	    return java.util.Date
	      .from(dateToConvert.atZone(ZoneId.systemDefault())
	      .toInstant());
	}


	// java9

	public LocalDate convertToLocalDate(Date dateToConvert) {
	    return LocalDate.ofInstant(
	      dateToConvert.toInstant(), ZoneId.systemDefault());
	}
 
	public LocalDateTime convertToLocalDateTime(Date dateToConvert) {
		return LocalDateTime.ofInstant(
		  dateToConvert.toInstant(), ZoneId.systemDefault());
	}




#### LocalDate/LocalDateTime/LocalTime ---->格式化表示或者輸出

```java
	
	// LocalDate LocalDateTime--> format String
	LocalDate date = LocalDate.now();
	date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu"))
	date.format(DateTimeFormatter.BASIC_ISO_DATE)

	LocalDateTime dateTime = LocalDateTime.now();
	dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"))

	//string --> LocalDateTime

	LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48",
				DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"));

reference:

https://www.baeldung.com/java-8-date-time-introjava

https://www.baeldung.com/java-date-to-localdate-and-localdatetimesql

https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant https://www.kancloud.cn/wizardforcel/java8-new-features/148349安全

相關文章
相關標籤/搜索