JDK8中,新增了三個類,用以處理時間。
LocalDate專門處理日期,LocalTime專門處理時間,LocalDateTime包含了日期和時間,而且對于很多復雜的問題,都提供了現成的方法,比如:獲取2017年12月的第一個周一等。
package test;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;/*** Created by lightClouds917* Date 2017/11/6* Description:Java8中處理時間和日期的類*SQL -> Java--------------------------date -> LocalDatetime -> LocalTimetimestamp -> LocalDateTime*/
public class DateTest2 {public static void main(String[] args){test1();test2();Calendar calendar = Calendar.getInstance();System.out.println(calendar.getTime());}/*** 處理日期 LocalDate*/public static void test1(){//獲取當前日期 2017-11-06LocalDate today = LocalDate.now();System.out.println(today);//構造日期 2017-10-08LocalDate today2 = LocalDate.of(2017,10,8);System.out.println(today2);//構造日期 2017-02-22 字符串嚴格按照yyyy-MM-ddLocalDate today3 = LocalDate.parse("2017-02-22");System.out.println(today3);//本月第一天 2017-11-01LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());System.out.println(firstDayOfMonth);//本月第二天 2017-11-02 第n天LocalDate secondDayOfMonth = today.withDayOfMonth(2);System.out.println(secondDayOfMonth);//本月最后一天 2017-02-28 方便解決任何年份的二月份多少天LocalDate lastDayOfMonth = today3.with(TemporalAdjusters.lastDayOfMonth());System.out.println(lastDayOfMonth);//獲取2017年12月的第一個周一 2017-12-04LocalDate firstDayOf201712 = LocalDate.parse("2017-12-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));System.out.println(firstDayOf201712);}/*** 處理時間 LocalTime*/public static void test2(){//獲取當前時間 含有毫秒值 17:18:41.571LocalTime now = LocalTime.now();System.out.println(now);//獲取當前時間 去掉毫秒值 17:45:41LocalTime now1 = LocalTime.now().withNano(0);System.out.println(now1);//00:46:46.651 提供了把時分秒都設為0的方法LocalTime now2 = LocalTime.now().withHour(0);System.out.println(now2);//構造時間 00:20:55LocalTime time1 = LocalTime.of(0,20,55);System.out.println(time1);//構造時間 05:43:22LocalTime time2 = LocalTime.parse("05:43:22");System.out.println(time2);//標準時間 2017-11-06T17:53:15.930LocalDateTime lt = LocalDateTime.now();System.out.println(lt);}
}
及:
public class TimeTest {@Testpublic void testTime() {LocalDateTime time = LocalDateTime.now();System.out.println(time.toString()); //字符串表示System.out.println(time.toLocalTime()); //獲取時間(LocalTime)System.out.println(time.toLocalDate()); //獲取日期(LocalDate)System.out.println(time.getDayOfMonth()); //獲取當前時間月份的第幾天System.out.println(time.getDayOfWeek()); //獲取當前周的第幾天System.out.println(time.getDayOfYear()); //獲取當前時間在該年屬于第幾天System.out.println(time.getHour());System.out.println(time.getMinute());System.out.println(time.getMonthValue());System.out.println(time.getMonth());System.out.println("-----------------------------------");//格式化輸出DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");System.out.println(time.format(formatter));//構造時間LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);//比較時間System.out.println(time.isAfter(startTime));System.out.println(time.isBefore(endTime));//時間運算,相加相減System.out.println(time.plusYears(2)); //加2年System.out.println(time.plusDays(2)); //加兩天System.out.println(time.minusYears(2)); //減兩年System.out.println(time.minusDays(2)); //減兩天//獲取毫秒數(使用Instant)System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//獲取秒數(使用Instant)System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());}}
?