Java中的日期相关API

java.lang.System类中的currentTimeMillis()

适用于计算时间差

long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
//称为时间戳
System.out.println(time);//1680588258985

java.util.Date类

java.util.Date类
|—java.sql.Date类
1.两个构造器的使用
>构造器一:Date():创建一个对应当前时间的Date对象
>构造器二:创建指定毫秒数的Date对象
2.两个方法的使用
>toString():显示当前的年、月、日、时、分、秒
>getTime():获取当前Date对象对应的毫秒数。(时间戳)
3. java.sql.Date对应着数据库中的日期类型的变量
>如何实例化
>如何将java.util.Date对象转换为java.sql.Date对象

//构造器一:Date():创建一个对应当前时间的Date对象
 Date date1 = new Date();
 System.out.println(date1);//Tue Apr 04 14:07:57 CST 2023
 System.out.println(date1.getTime());//1680588477659

 //构造器二:创建指定毫秒数的Date对象
 Date date2 = new Date(1680588477659L);
 System.out.println(date2);//Tue Apr 04 14:07:57 CST 2023

 //创建java.sql.Date对象
 java.sql.Date date3 = new java.sql.Date(1680588455555L);
 System.out.println(date3);//2023-04-04

 //如何将java.util.Date对象转换为java.sql.Date对象
 //情况一:
 Date date4 = new java.sql.Date(2343243242323L);
 java.sql.Date date5 = (java.sql.Date) date4;
 System.out.println("date5 = " + date5);//date5 = 2044-04-03
 //情况二:
 Date date6 = new Date();
 java.sql.Date date7 = new java.sql.Date(date6.getTime());
 System.out.println("date7 = " + date7);//date7 = 2023-04-04

java.text.SimpleDateFormat

SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析

1.两个操作:
1.1 格式化:日期 --->字符串
1.2 解析:格式化的逆过程,字符串 ---> 日期
2.SimpleDateFormat的实例化
//实例化SimpleDateFormat:使用默认的构造器
 SimpleDateFormat sdf = new SimpleDateFormat();

 //格式化:日期 --->字符串
 Date date = new Date();
 System.out.println(date);//Tue Apr 04 14:15:36 CST 2023
 String format = sdf.format(date);
 System.out.println(format);//23-4-4 下午2:15

 //解析:格式化的逆过程,字符串 ---> 日期
 String str = "23-4-4 上午11:43";
 Date date1 = sdf.parse(str);
 System.out.println(date1);//Tue Apr 04 11:43:00 CST 2023

 //*************按照指定的方式格式化和解析:调用带参的构造器*****************
 //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);//2023-04-04 02:15:36
 //解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),
 //否则,抛异常
 Date date2 = sdf1.parse("2023-04-04 11:48:27");
 System.out.println(date2);//Tue Apr 04 11:48:27 CST 2023

java.util.Calendar

//1.实例化
//方式一:创建其子类(GregorianCalendar)的对象
//方式二:调用其静态方法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);//4[本月第几天]
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//94[本年第几天]

//set()--calendar可变性
calendar.set(Calendar.DAY_OF_MONTH,22);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//22

//add()
calendar.add(Calendar.DAY_OF_MONTH,-3);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//19

//getTime():日历类---> Date
Date date = calendar.getTime();
System.out.println(date);//Wed Apr 19 14:19:26 CST 2023

//setTime():Date ---> 日历类
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//4

LocalDate、LocalTime、LocalDateTime 的使用

说明:
1.LocalDateTime相较于LocalDate、LocalTime,使用频率要高
2.类似于Calendar

//now():获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2023-04-04
System.out.println(localTime);//14:25:07.670
System.out.println(localDateTime);//2023-04-04T14:25:07.670

//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2023, 4, 5, 13, 23, 43);
System.out.println(localDateTime1);//2023-04-05T13:23:43

//getXxx():获取相关的属性
System.out.println(localDateTime.getDayOfMonth());//4
System.out.println(localDateTime.getDayOfWeek());//TUESDAY
System.out.println(localDateTime.getMonth());//APRIL
System.out.println(localDateTime.getMonthValue());//4
System.out.println(localDateTime.getMinute());//25

//以下测试体现不可变性
//withXxx():设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2023-04-04
System.out.println(localDate1);//2023-04-22

LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);//2023-04-04T14:25:07.670
System.out.println(localDateTime2);//2023-04-04T04:25:07.670

LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);//2023-04-04T14:25:07.670
System.out.println(localDateTime3);//2023-07-04T14:25:07.670

LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);//2023-04-04T14:25:07.670
System.out.println(localDateTime4);//2023-03-29T14:25:07.670

java.time.Instant

类似于 java.util.Date类

//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2023-04-04T06:29:27.698Z

//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2023-04-04T14:29:27.698+08:00

//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数  ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1680589767698

//ofEpochMilli():通过给定的毫秒数,获取Instant实例  -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1680589752125L);
System.out.println(instant1);//2023-04-04T06:29:12.125Z

java.time.format.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);//2023-04-04T14:36:22.831
System.out.println(str1);//2023-04-04T14:36:22.831

//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2023-04-04T15:42:18.797");
System.out.println(parse);//{},ISO resolved to 2023-04-04T15:42:18.797

//方式二:
//本地化相关的格式。如:ofLocalizedDateTime()
//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2023年4月4日 下午02时36分22秒

//本地化相关的格式。如: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);//2023-4-4

//重点: 方式三:自定义的格式。如: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);//2023-04-04 14:36:22

//解析
TemporalAccessor accessor = formatter3.parse("2023-04-04 13:52:09");
System.out.println(accessor);//{},ISO resolved to 2023-04-04T13:52:09

转换

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值