JDK8之前日期和时间的API测试
- 1.System类中currentTimeMillis();
- 2.java.util.Date和子类java.sql.Date
- 3.SimpleDateFormat
- 4.Calendar
- 注意:
1.一月是0,12月是11
2.周日是1,周六是7
java.util.Date类
- 1.两个构造器的使用
//构造器一:Date():创建了一个对应当前时间的Date对象
//构造器二:创建指定毫秒数的Date对象 - 2.两个方法的使用
>toString():显示当前的年、月、日、时、分、秒
>getTime():获取当前Date对象对应的毫秒数。(时间戳) - 3.java.sql.Date对应着数据库中的日期数据类型
>如何实例化
>如何将java.util.Date对象转化为java.sql.Date对象
@Test
public void test2(){
//构造器一:Date():创建了一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());//Sat Nov 21 20:46:18 CST 2020
System.out.println(date1.getTime());//1605962853374
//构造器二:创建指定毫秒数的Date对象
// new Date(2020, 11, 21);
Date date2 = new Date(1605962853374L);
System.out.println(date2);//Sat Nov 21 20:47:33 CST 2020
//创建 java.sql.Date对象
java.sql.Date date3 = new java.sql.Date(1605962853374L);
System.out.println(date3);//2020-11-21
//如何将java.util.Date对象转化为java.sql.Date对象
//情况一:
// Date date4 = new java.sql.Date(1605962853374L);
// java.sql.Date date5 = (java.sql.Date)date4;
//情况二:
Date date6 = new Date();
// java.sql.Date date7 = (java.sql.Date)date6;//ClassCastException
java.sql.Date date7 = new java.sql.Date(date6.getTime());
System.out.println(date7);//2020-11-21
}
SimpleDateFormat的使用:simpleDateFormat对日期Date类的格式化和解析
- 1.两个操作:
1.1格式化:日期 --> 字符串
1.2解析:格式化的逆过程 - 2.SimpleDateFormat的实例化
@Test
public void test() throws ParseException {
//实例化SimpleDateFormat:使用默认的构造器
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期 -> 字符串
Date date1 = new Date();
System.out.println(date1);//Sun Nov 22 10:07:24 CST 2020
String format = sdf.format(date1);
System.out.println(format);//20-11-22 上午10:08
//解析:格式化的逆过程,字符串 --> 日期
// String str = "2020-11-22";//ParseException,识别不了
String str = "20-11-22 上午10:13";
Date date2= sdf.parse(str);
System.out.println(date2);//Sun Nov 22 10:13:00 CST 2020
//*************按照指定的方式格式化解析:调用带参的构造器****************
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
String format1 = sdf1.format(date1);
System.out.println(format1);//02020.十一月.22 公元 02:05 下午
//格式化
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format2 = sdf2.format(date1);
System.out.println(format2);//2020-11-22 02:10:21
//解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则抛异常
Date date3 = sdf2.parse(format2);
Date date4 = sdf2.parse("2020-11-22 02:10:21");
System.out.println(date3);//Sun Nov 22 02:15:38 CST 2020
System.out.println(date4);//Sun Nov 22 02:10:21 CST 2020
}
练习:字符串“2020-11-22”转换为java.sql.Date
@Test
public void test2() throws ParseException {
String birth = "2020-11-22";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(birth);
System.out.println(date);//Sun Nov 22 00:00:00 CST 2020
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);//2020-11-22
}
练习:“三天打鱼两天晒网” 1990-01-01 XXXX-XX-XX 打鱼?晒网?
2020-09-08?
总天数 % 5 == 1,2,3打鱼/ 4,0晒网
总天数的计算?
@Test
public void test3() throws ParseException {
long time1 = System.currentTimeMillis();
System.out.println(time1);
String endTime = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf1.parse(endTime);
System.out.println(date1);//Tue Sep 08 00:00:00 CST 2020
System.out.println(date1.getTime());//1599494400000
String startTime = "1990-01-01";
Date date2 = sdf1.parse(startTime);
System.out.println(date2);//Mon Jan 01 00:00:00 CST 1990
System.out.println(date2.getTime());//631123200000
long sumDay = 0L;
sumDay = (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
//天数
System.out.println(sumDay);//11208
int temp = (int) (sumDay % 5);
if (temp == 1 || temp ==2 || temp == 3){
System.out.println("今天又是打鱼的一天!");
}else{
System.out.println("该晒网了!");
}
}
Calender日历类(抽象类)的使用
@Test
public void test1() {
//1.实例化
//方式一:创建其子类对象 (GregorianCalender)
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass());//class java.util.GregorianCalendar
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);//获取当前时间是这个月的第几天
System.out.println(days);//22
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//327
//set()
calendar.set(Calendar.DAY_OF_MONTH, 25);
int days1 = calendar.get(Calendar.DAY_OF_MONTH);//获取当前时间是这个月的第几天
System.out.println(days1);//25
//add()
calendar.add(Calendar.DAY_OF_MONTH, 5);
int days2 = calendar.get(Calendar.DAY_OF_MONTH);//获取当前时间是这个月的第几天
System.out.println(days2);//25+5
//getTime() 日历类-->Date
Date date = calendar.getTime();
System.out.println(date);//Mon Nov 30 15:37:19 CST 2020
//setTime() Date --> 日历类
Date date1 = new Date();
calendar.setTime(date1);
System.out.println(date1);//Sun Nov 22 15:38:41 CST 2020
}
JDK 8中的日期时间API测试
@Test
public void test1(){
Date date1 = new Date(2020 ,9,8);
System.out.println(date1);//Fri Oct 08 00:00:00 CST 3920
//偏移量
Date date2 = new Date(2020 - 1900 ,9 - 1,8);
System.out.println(date2);//Tue Sep 08 00:00:00 CST 2020
}
/*
LocalDate、LocalTime、LocalDateTime的使用
说明:
1.LocalDateTime使用较多
*/
@Test
public void test2(){
//now():获取当前日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2020-11-22
System.out.println(localTime);//16:00:48.382
System.out.println(localDateTime);//2020-11-22T16:00:48.382
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localdatatime1 = LocalDateTime.of(2018, 6, 22, 21, 30, 55);
System.out.println(localdatatime1);//2018-06-22T21:30:55
//getXXX()
System.out.println(localDateTime.getDayOfMonth());//22
System.out.println(localDateTime.getDayOfWeek());//SUNDAY
System.out.println(localDateTime.getMonth());//NOVEMBER
System.out.println(localDateTime.getMonthValue());//11
System.out.println(localDateTime.getMinute());//7
//LocalDate。。不可变性
//withXXX():设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(25);
System.out.println(localDate);//2020-11-22
System.out.println(localDate1);//2020-11-25
LocalDateTime localDateTime1 = localDateTime.withHour(4);
System.out.println(localDateTime);//2020-11-22T16:11:40.102
System.out.println(localDateTime1);//2020-11-22T04:11:40.102
//不可变性
LocalDateTime localDateTime2 = localDateTime.plusMonths(3);
System.out.println(localDateTime);//2020-11-22T16:13:52.401
System.out.println(localDateTime2);//2021-02-22T16:13:52.401
}
Instant的使用类似于java.util.Date类
@Test
public void test1(){
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2020-11-22T08:17:38.101Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2020-11-22T16:20:46.036+08:00
//获取自1970对应的毫秒数 --> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1606033374048
//ofEpochMilli:通过给定毫秒数,获取Instent实例 --> Date(Long millis)
Instant instant1 = Instant.ofEpochMilli(1606033374048L);
System.out.println(instant1);//2020-11-22T08:22:54.048Z
}
DateTimeFormatter:格式化或解析日期、时间(类似于SimpleDateFormat)
@Test
public void test2(){
//方式一:预定义的标准格式。如: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);//2020-11-22T16:32:57.305
System.out.println(str1);//2020-11-22T16:32:57.305
//解析:字符串 --> 日期
TemporalAccessor parse = formatter.parse("2020-11-22T16:32:57.305");
System.out.println(parse);//{},ISO resolved to 2020-11-22T16:32:57.305
//方式二:本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//20-11-22 下午4:36
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String str3 = formatter2.format(localDateTime);
System.out.println(str3);//2020年11月22日 下午04时37分35秒
//解析
TemporalAccessor parse1 = formatter1.parse("20-11-22 下午4:36");
System.out.println(parse1);//{},ISO resolved to 2020-11-22T16:36
//本地化相关的格式。如:ofLocalizedDate()
//格式化
DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String str4 = formatter3.format(LocalDate.now());
System.out.println(str4);//2020年11月22日 星期日
DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
String str5 = formatter4.format(LocalDate.now());
System.out.println(str5);//2020-11-22
DateTimeFormatter formatter5 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
String str6 = formatter5.format(LocalDate.now());
System.out.println(str6);//2020年11月22日
//解析。。。
//重点:方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss E")
DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str7 = formatter6.format(LocalDateTime.now());
System.out.println(str7);//2020-11-22 04:46:08
//解析
TemporalAccessor parse2 = formatter6.parse("2020-11-22 04:46:08");
System.out.println(parse2);
//{SecondOfMinute=8, MicroOfSecond=0, HourOfAmPm=4, NanoOfSecond=0, MinuteOfHour=46, MilliOfSecond=0},ISO resolved to 2020-11-22
}