1. 程式人生 > >Java8_日期,時間類

Java8_日期,時間類

 這裡列出了Java8中新增的有關日期和時間的類,以及類的方法,如果想更進一步瞭解,可以參考Java8手冊,我沒有將輸出語句新增到程式中,就是讓大家動動手,自己新增,看看這些類的具體使用,畢竟自己實踐比看效果好一些哦!

package com.dong.java8;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalField;
import static java.time.temporal.TemporalAdjusters.*;
import java.time.format.*;

public class testDate {
	public static void main(String[] args) {
		//localDate提供了簡單的日期,不含當天的時間資訊
		LocalDate date = LocalDate.of(2018, 10, 23);
		//獲取年份
		int year = date.getYear();
		//獲取月份
		Month month = date.getMonth();
		//獲取幾號
		int day = date.getDayOfMonth();
		//獲取星期幾
		DayOfWeek dow = date.getDayOfWeek();
		//月份時長
		int 冷= date.lengthOfMonth();
		//獲取是否瑞年
		boolean leap = date.isLeapYear();
		//獲取當前日期
		LocalDate today = LocalDate.now();
		
		//LocalDate有一個int get(TemporalField field)方法,接受TemporalField介面實現類,TemporalField介面的實現類
		//是enum ChronoField implements TemporalField,是一個列舉,所以可以和私用get方法得到列舉元素的值,
		
		int yearr = date.get(ChronoField.YEAR);
		int monthh = date.get(ChronoField.MONTH_OF_YEAR);
		int dayy = date.get(ChronoField.DAY_OF_MONTH);
		
		//通過解析字串建立LocalDate物件,
		LocalDate datee = LocalDate.parse("2018-10-23");
		
		//訪問時間可以使用LocalTime類
		LocalTime time = LocalTime.of(14, 56,40);
		int hour = time.getHour();
		int minute = time.getMinute();
		int second = time.getSecond();
		
		//通過解析字串建立LocalTime物件
		LocalTime timee = LocalTime.parse("14:59:10");
		
		//既包含日期,又包含時間的符合類
		LocalDateTime dt1 = LocalDateTime.of(2018,10,23,15,1,10);
		LocalDateTime dt2 = LocalDateTime.of(datee, timee);
		LocalDateTime dt3 = date.atTime(15,2,10);
		LocalDateTime dt4 = date.atTime(time);
		LocalDateTime dt5 = time.atDate(date);
		
		//從LocalDateTime 中提取LocalDate, 或 LocalTime
		LocalDate date1 = dt1.toLocalDate();
		LocalTime time1 = dt1.toLocalTime();
		
		//Instant 類對時間的建模是一個大整型數,它是以Unix元年時間開始所經歷的秒數進行計算;
		Instant ins = Instant.ofEpochSecond(3);
		Instant ins2 = Instant.ofEpochSecond(3, 0); 
		Instant ins3 = Instant.ofEpochSecond(5, -100000000); //5秒之前的1億納秒
		
		//Duration , Period 類;
		Duration d1 = Duration.between(time, timee);
		Duration d2 = Duration.between(date, datee);
		Duration d3 = Duration.between(ins, ins2);
		
		//操縱,修改和格式化日期
		//LocalDate withYear(int year)
		//LocalDate withMonth(int month)
		//LocalDate withDayOfMonth(int dayOfMonth)
		//LocalDate withDayOfYear(int dayOfYear)
		
		//withAttribute方法會建立物件的一個副本,並按照需求修改它的屬性,都不會修改原來的物件:
	
		LocalDate datel = LocalDate.of(2018,10,23);  
		LocalDate datel1 = datel.withYear(2019);
		LocalDate datel2 = datel.withMonth(12);
		LocalDate datel3 = datel.withDayOfMonth(30);
		LocalDate datel4 = datel.with(ChronoField.MONTH_OF_YEAR, 6);
		
		LocalDate datel5 = date1.plusMonths(1);
		LocalDate datel6 = datel.minusWeeks(10);
		LocalDate datel7 = datel.plus(6, ChronoUnit.MONTHS);
		
		//TemporalAdjuster
		//進行更復雜的操作,例如下個周幾,最後一天。。。
		//匯入TemporalAdjusters.* 包中的所有方法和變數
		LocalDate datel8 = datel.with(lastDayOfYear());
		LocalDate datel9 = datel.with(firstDayOfYear());
		
		//列印輸出以及解析日期-時間物件
		//java.time.format包中包含解析和列印的方法
		
		String s1 = datel.format(DateTimeFormatter.BASIC_ISO_DATE);
		String s2 = datel.format(DateTimeFormatter.ISO_DATE);
		
		//解析字串重新建立物件
		LocalDate datell = LocalDate.parse("20181023154312", DateTimeFormatter.ISO_DATE_TIME);
		
		//按照某個模式穿點DateTimeFormatter物件
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate datelf = LocalDate.parse(s1, formatter);		
	}
}

內容參考《Java 8 實戰》