目錄
1.LocalDate、LocalTime、LocalDateTime 的使用
1.JDK1.8之前的日期時間類講解
1.1Date類
package com.atguigu.java;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* jdk 8之前的日期時間的API測試
* 1. System類中currentTimeMillis();
* 2. java.util.Date和子類java.sql.Date
* 3. SimpleDateFormat
* 4. Calendar
*
* @author wudi
* @create 2019 上午 11:35
*/
public class DateTimeTest {
/*
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.MMMMM.dd GGG hh:mm aaa");
SimpleDateFormat sdf1 = new SimpleDateFormat("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("2020-02-18 11:48:27");
System.out.println(date2);
}
/*
練習一:字符串"2020-09-08"轉換爲java.sql.Date
練習二:"三天打漁兩天曬網" 1990-01-01 xxxx-xx-xx 打漁?曬網?
舉例:2020-09-08 ? 總天數
總天數 % 5 == 1,2,3 : 打漁
總天數 % 5 == 4,0 : 曬網
總天數的計算?
方式一:( date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24) + 1
方式二:1990-01-01 --> 2019-12-31 + 2020-01-01 -->2020-09-08
*/
@Test
public void testExer() throws ParseException {
String birth = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(birth);
// System.out.println(date);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
/*
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可變性
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,-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.println(days);
}
}
1.2Calendar類
package com.atguigu.java;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* jdk 8之前的日期時間的API測試
* 1. System類中currentTimeMillis();
* 2. java.util.Date和子類java.sql.Date
* 3. SimpleDateFormat
* 4. Calendar
*
* @author wudi
* @create 2019 上午 11:35
*/
public class DateTimeTest {
/*
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可變性
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,-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.println(days);
}
}
注意:上面兩種方法有很多問題:
- 可變性:像日期和時間這樣的類應該是不可變的。
- 偏移性:Date中的年份是從1900開始的,而月份都從0開始。
- 格式化:格式化只對Date有用,Calendar則不行。
- 此外,它們也不是線程安全的;不能處理閏秒等。
2.JDK1.8中的日期時間
1.LocalDate、LocalTime、LocalDateTime 的使用
package com.atguigu.java;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
/**
* jdk 8中日期時間API的測試
*
* @author wudi
* @create 2019 下午 2:44
*/
public class JDK8DateTimeTest {
@Test
public void testDate(){
//偏移量
Date date1 = new Date(2020 - 1900,9 - 1,8);
System.out.println(date1);//Tue Sep 08 00:00:00 GMT+08:00 2020
}
/*
LocalDate、LocalTime、LocalDateTime 的使用
說明:
1.LocalDateTime相較於LocalDate、LocalTime,使用頻率要高
2.類似於Calendar
*/
@Test
public void test1(){
//now():獲取當前的日期、時間、日期+時間
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//of():設置指定的年、月、日、時、分、秒。沒有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
System.out.println(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.println(localDateTime);
System.out.println(localDateTime2);
//不可變性
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);
System.out.println(localDateTime4);
}
/*
DateTimeFormatter:格式化或解析日期、時間
類似於SimpleDateFormat
*/
@Test
public void test3(){
// 方式一:預定義的標準格式。如: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("2019-02-18T15:42:18.797");
System.out.println(parse);
// 方式二:
// 本地化相關的格式。如:ofLocalizedDateTime()
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :適用於LocalDateTime
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.MEDIUM);
//格式化
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("2019-02-18 03:52:09");
System.out.println(accessor);
}
}
總結:日期時間前後就這麼多了。具體開發中根據自己需求選擇吧。