DATE常用API
JDK8之前日期时间API
- System类中currentTimeMillis();
- java.util.Date和子类java.sql.Date
- SimpleDateFormat
- Calendar
System类中currentTimeMillis();
System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
此方法适于计算时间差。
//1.System类中的currentTimeMillis()
@Test
public void test1(){
long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
//称为时间戳
System.out.println(time);
}
java.util.Date和子类java.sql.Date
Date有两种,一种是java.util.Date包下的,一种是java.sql.Date包下的
java.util.Date的两种创建方式
利用Date的两个构造器创建
构造器一:Date():创建一个对应当前时间的Date对象
构造器二:创建指定毫秒数的Date对象Date的两个常用方法:
toString():显示当前的年、月、日、时、分、秒
getTime():获取当前Date对象对应的毫秒数。(时间戳)
@Test
public void test2(){
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());//Sun Jul 25 20:33:09 CST 2021
System.out.println(date1.getTime());//1627216389819
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。通常称为时间戳
//构造器二:创建指定毫秒数的Date对象
Date date2 = new Date(155030620410L);
System.out.println(date2.toString());
}
java.sql.Date的创建方式
import java.sql.Date;//导入java.sql.Date包
public static void main(String[] args) {
//new好Date()后传入lang型的参数,这个参数就是距离与1970年1月1日0时0分0秒之间毫秒值
Date date3 = new Date(1627216389819L);
System.out.println(date3);2021-07-25
//只显示年月日
}
//如何将java.util.Date对象转换为java.sql.Date对象
//情况一:
Date date4 = new java.sql.Date(1627216389819L);
java.sql.Date date5 = (java.sql.Date) date4;
System.out.println(date4);//2021-07-25
//情况二:
Date date6 = new Date();//util包下的
java.sql.Date date7 = new java.sql.Date(date6.getTime());
System.out.println(date7);//2021-07-25
java.text.SimpleDateFormat类
Date类的API不易于国际化,大部分被废弃了,java.text.SimpleDateFormat类是一个不与语言环境有关的方式来格式化和解析日期的具体类。
它允许进行格式化:日期—>文本、解析:文本—>日期
实例化SimpleDateFormat:使用默认的构造器(一般不会使用)
@Test
public void test1() throws ParseException {
// 实例化SimpleDateFormat:使用默认的构造器
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
Date date = new Date();
System.out.println(date);//Sun Jul 25 21:05:08 CST 2021
//将date格式化会得到以下格式的字符串
String format = simpleDateFormat.format(date);
System.out.println(format);//21-7-25 下午9:05
//同样,也必须是以下格式的字符串才可以解析,得到date类型的数据
String str ="21-7-25 下午9:05";
Date parse = simpleDateFormat.parse(str);
System.out.println(parse);// Sun Jul 25 21:05:00 CST 2021
}
实例化SimpleDateFormat:使用带参的构造器
在开发中对于日期的格式会有各种各样的格式,为了方便我们使用,可以自定义(开发中常用)
@Test
public void test1() throws ParseException {
//指定日期格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
System.out.println(date);//Sun Jul 25 21:16:02 CST 2021
//格式化
String format = simpleDateFormat.format(date);
System.out.println(format);//2021-07-25 09:16:02
// 解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则,抛异常
String str ="2021-07-25 09:16:02";
Date parse = simpleDateFormat.parse(str);
System.out.println(parse);// Sun Jul 25 09:16:02 CST 2021
}
java.util.Calendar(日历)类
Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
获取Calendar实例的方法
- 使用Calendar.getInstance()方法
- 调用它的子类GregorianCalendar的构造器。
注意:
- 获取月份时:一月是0,二月是1,以此类推,12月是11
- 获取星期时:周日是1,周二是2 , 以此类推,周六是7
@Test
public void testCalendar(){
//1.实例化
//方式一:创建其子类(GregorianCalendar)的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
//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);
}
JDK8中新日期时间API
Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime)和持续时间(Duration)的类。历史悠久的 Date 类新增了 toInstant() 方法,用于把 Date 转换成新的表示形式。这些新增的本地化时间日期 API 大大简化了日期时间和本地化的管理。
LocalDate、LocalTime、LocalDateTime
LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
LocalDate代表IOS格式(yyyy-MM-dd)的日期,
LocalTime表示一个时间,
LocalDateTime是用来表示日期和时间的,这是一个最常用的类之一。
@Test
public void test3(){
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2021-07-25
System.out.println(localTime);//22:54:26.637
System.out.println(localDateTime);//2021-07-25T22:54:26.637
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 7, 25, 22, 53, 26);
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);
}
Instant
时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳
@Test
public void test4(){
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2021-07-25T15:45:57.570Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-07-25T23:45:57.570+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1627227957570
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1627227957570L);
System.out.println(instant1);//2021-07-25T15:45:57.570Z
}
DateTimeFormatter
格式化与解析日期或时间
java.time.format.DateTimeFormatter 类:该类提供了三种格式化方法:
- 预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
- 本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
- 自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
@Test
public void test5(){
// 方式一:预定义的标准格式。如: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);//2021-07-25T23:22:12.012
System.out.println(str1);//2021-07-25T23:22:12.012
//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2021-07-25T23:22:12.012");
System.out.println(parse);//{},ISO resolved to 2021-07-25T23:22:12.012
// 方式二:
// 本地化相关的格式。如:ofLocalizedDateTime()
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2021年7月25日 下午11时24分10秒
// 本地化相关的格式。如: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);//2021-7-25
// 重点: 方式三:自定义的格式。如: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);//2021-07-25 11:24:10
//解析
TemporalAccessor accessor = formatter3.parse("2021-07-25 11:24:10");
System.out.println(accessor);//{SecondOfMinute=9, HourOfAmPm=3, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=52, MilliOfSecond=0},ISO resolved to 2019-02-18
}