日期時間類
阿新 • • 發佈:2022-05-06
java.util.Date類、java.sql.Date類
1.兩個構造器的使用
構造器一: Date():建立一個對應當前時間的Date物件
構造器二:建立指定毫秒數的Date物件
2.兩個方法的使用
tostring():顯示當前的年、月、日、時、分、秒
getTime():獲取當前Date物件對應的毫秒數。(時間戳)
- java.sql.Date對應著資料庫中的日期型別的變數
如何例項化
如何將java.util.Date物件轉換為java.sql.Date物件
public void test2(){ //構造器一: Date():建立一個對應當前時間的Date物件 Date date1 = new Date(); system.out.println(date1.toString());//Sat Feb 16 16:35:31 GMT+08:00 2019 system.out.print1n(date1.getTime());//1550306204104 //構造器二:建立指定毫秒數的Date物件 Date date2 = newDate(155030620410L); system.out.println(date2.toString()); //建立java.sql.Date物件 java.sql.Date date3 = new java.sql.Date(35235325345L); System.out.println(date3);//1971-02-13 //如何將java.util.Date物件轉換為java.sqL.Date物件 //情況一: //Date date4 = new java.sqL.Date( 2343243242323L); //java.sqL.Date date5 = (java.sqL.Date) date4; //情況二: Date date6 = new Date(); java.sql.Date date7 = new java.sql.date(date6.getTime()); }
SimpleDateFormat類
SimpleDateFormat的使用: SimpleDateFormat對日期Date類的格式化和解析
1.兩個操作:
1.1格式化:日期--->字串
1.2解析:格式化的逆過程,字串--->日期
2.SimpleDateFormat的例項化
@Test public void testSimpleDateFormat() throws ParseException { //例項化SimpLeDateFormat SimpleDateFormat sdf = new simpleDateFormat(); //格式化:日期--->字串 Date date = new Date(); System.out.println(date); String format = sdf.format(date); System.out.println(format); //解析:格式化的逆過程,字串--->日期 String str = "19-12-18 上午11:43"; Date date1 = sdf.parse(str); System.out.println(date1); //************按照指定的方式格式化和解析:呼叫帶參的構造器***************** //SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMM.dd GGG hh:mm aaa") SimpleDateFormat sdf1 = new SimpleDateFormat(pattern:"yyyy-MM-dd hh:mm:ss"); //格式化 String format1 = sdf1.format(date); System.out.println(format1);//2019-02-18 11:48:27 //解析:要求字串必須是符合SimpleDateFormat識別的格式(通過構造器引數體現),否則,拋異常 Date date2 = sdf1.parse(source: "2020-02-18 11:48:27"); System.out.print1n(date2); }
Calendar日曆類
Calendar日曆類(抽象類)的使用
@Test public void testcalendar(){ //1.例項化 //方式一:建立其子類(GregorianCalendar)的物件 //方式二:呼叫其靜態方法getInstance() Calendar calendar = Calendar.getInstance(); // System.out.println(calendar.getclass()); //2.常用方法 // get() int days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //set() calendar.set(Calendar.DAY_OF_MONTH,22); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); //add() calendar.add(Calendar.DAY_OF_MONTH,amount:-3); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(days); //getTime():日曆類--->Date Date date = calendar.getTime(); System.out.println(date); //setTime( ):Date--->日曆類 Date date1 = new Date(); calendar.setTime(date1); days = calendar.get(Calendar.DAY_OF_MONTH); System.out.print1n(days); }
LocaLDate、LocalTime、LocalDateTime類
LocaLDate、LocalTime、LocalDateTime的使用
說明:
1.LocaLDateTime相較於LocalDate、LocalTime,使用頻率要高
@Test
public void test1(){
//now():獲取當前的日期、時間、日期+時間
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.print1n(localDate);
System.out.print1n(localTime) ;
System.out.println(localDateTime) ;
//of():設定指定的年、月、日、時、分、秒。沒有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(year:2020,month:10,dayOfMonth,6,hour,13,minute:23,second:43);
System.out.print1n( localDateTime1);
// getxxx():獲取相關的屬性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
//體現不可變性
//withXxx()∶設定相關的屬性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.print1n(localDateTime);
System.out.println( localDateTime2);
//不可變性
LocalDateTime localDateTime3 = localDateTime.plusMonths(3 );
System.out.println(localDateTime);
System.out.print1n(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println( localDateTime);
System.out.println( localDateTime4);
}
Instant
Instant的使用
類似於java.util.Date類
@Test
public void test2(){
//now():獲取本初子午線對應的標準時間
Instant instant = Instant.now();
System.out.print1n(instant);//2019-02-18T07:29:41.719Z
//新增時間的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00
//toEpochMilli():獲取自1970年1月1日0時0分秒(UTC)開始的毫秒數 --->Date類的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli():通過給定的毫秒數,獲取Instant例項-->Date(Long millis)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1);
}
DateTimeFormatter
DateTimeFormatter:格式化或解析日期、時間類似於SimpleDateFormat
方式一:預定義的標準格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字串
LocalDateTime localDateTime = LocalDateTime.now( );
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);//2019-02-18T15:42:18.797
//解析:字串-->日期
TemporalAccessor parse = formatter.parse(text:"2019-02-18T15:42:18.797");
System.out.print1n(parse);
方式二:
//本地化相關的格式.如:ofLocalizedDateTime()
//FormatStyle.LONG/FormatStyLe.MEDIUM/FormatStyle.SHORT:適用於LcoalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2019年2月18日下午03時47分16秒
//本地化相關的格式。如:ofLocalizedDate()
//FormatStyle.FULL/FormatStyLe.LONG/FormatStyLe.MEDIUM/FormatStyle.SHORT:適用於LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUN);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2019-2-18
重點:方式三:自定義的格式。如: ofPattern("yyyy-MM-dd hh: mm: ss")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2019-02-18 03:52:09
//解析
TemporalAccessor accessor = formatter3.parse(text: "2019-02-18 03:52:09");
System.out.println(accessor);