🔥作者主页:小林同学的学习笔录
🔥mysql专栏:小林同学的专栏
目录
1.Date类
1.1 概述
Java的Date
类是用来表示特定时间点的类。它位于java.util
包中,是Java早期日期和时间处理的一部分。Date
类的实例表示自1970年1月1日00:00:00 GMT以来的毫秒数。
在Java 8及更高版本中,推荐使用java.time
包中的新日期和时间API,如LocalDate
,LocalTime
和LocalDateTime
,因为它们提供了更丰富和更方便的功能。
缺点:
Date
对象是可变的,这意味着一旦创建,它的值可以被改变。这可能导致并发问题,线程不安全Date
类使用的时区是GMT,这可能导致在处理不同时区的应用程序时出现问题
1.2 构造方法
Date()
: 创建表示当前时间的Date
对象,从1970年1月1日00:00:00 GMT开始。
Date(long date)
: 创建一个表示从1970年1月1日00:00:00 GMT开始经过指定毫秒数的Date
对象。
1.3 常用方法
Date类中的多数方法已经过时,常用的方法有:
public long getTime()
把日期对象转换成对应的时间毫秒值,得到的是GMT到现在的毫秒数。public void setTime(long time)
参数给定的毫秒值设置给日期对象,传参需要单位毫秒。before(Date when)
: 检查当前日期是否在指定日期之前。after(Date when)
: 检查当前日期是否在指定日期之后。
小结:Date表示特定的时间瞬间,我们可以使用Date对象对时间进行操作。
代码演示:
public class Demo01 {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
System.out.println(time);
long customTime = 1620462871000L; // 表示2021年5月8日12:34:31
//currentDate 对象的时间将被设置为2021年5月8日12:34:31的时间。
date.setTime(customTime);
long time1 = date.getTime();
System.out.println(time1);
}
}
输出结果:
1715160825915
1620462871000
2.SimpleDateFormat类
2.1 概述
java.text.SimpleDateFormat
是日期/时间格式化类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换。
格式化:按照指定的格式,把Date对象转换为String对象。
解析:按照指定的格式,把String对象转换为Date对象。
2.2 构造方法
由于DateFormat为抽象类,不能直接使用,所以需要常用的子类java.text.SimpleDateFormat
。这个类需要一个模式(格式)来指定格式化或解析的标准。构造方法为:
public SimpleDateFormat(String pattern)
:用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat。参数pattern是一个字符串,代表日期时间的自定义格式。public SimpleDateFormat():无参构造器,按默认格式输出时间格式
2.3 格式规则
yyyy
:四位数的年份(例如:2024)yy
:两位数的年份(例如:24,对应2024)MM
:两位数的月份(01表示一月,12表示十二月)M
:一位或两位数的月份(1表示一月,12表示十二月)dd
:两位数的日期(01到31)d
:一位或两位数的日期(1到31)HH
:两位数的小时数,使用24小时制(00到23)H
:一位或两位数的小时数,使用24小时制(0到23)hh
:两位数的小时数,使用12小时制(01到12)h
:一位或两位数的小时数,使用12小时制(1到12)mm
:两位数的分钟数(00到59)m
:一位或两位数的分钟数(0到59)ss
:两位数的秒数(00到59)E
:星期几的简写(例如:Sun、Mon、Tue等)EE
:星期几的长名(例如:Sunday、Monday、Tuesday等)
2.4 常用方法
DateFormat类的常用方法有:
public String format(Date date)
:将Date对象格式化为字符串。public Date parse(String source)
:将字符串解析为Date对象。
拓展:
String formattedDate = sdf.format(new Date());这句代码new Date()得到的是一个毫秒数,底层做了什么变成指定格式?
当调用
new Date()
时,会创建一个Date
对象,代表当前的日期和时间,以毫秒数的形式存储。然后在调用format()
方法时,SimpleDateFormat
类会将这个毫秒数转换为指定格式的日期字符串。在内部,SimpleDateFormat
类会根据指定的日期格式,将毫秒数转换为年、月、日、时、分、秒等不同的时间单位,然后拼接成最终的格式化日期字符串。所以,实际上是SimpleDateFormat
在底层根据指定的格式使用Date
对象中的毫秒数进行计算和格式化,从而得到最终的格式化日期字符串。
代码演示:
public class Demo01 {
public static void main(String[] args) {
// 使用指定的日期格式创建SimpleDateFormat对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化当前日期
String formattedDate = sdf.format(new Date());
System.out.println("Formatted date: " + formattedDate);
// 将字符串解析为日期对象
String strDate = "2022-01-01 12:00:00";
try {
Date parsedDate = sdf.parse(strDate);
System.out.println("Parsed date: " + parsedDate);
} catch (Exception e) {
System.out.println("Error parsing date: " + e.getMessage());
}
}
}
输出结果:
Formatted date: 2024-05-08 20:46:20
Parsed date: Sat Jan 01 12:00:00 CST 2022
3.Calendar类
3.1 概述
Calendar
类是Java中用于操作日期和时间的类,它提供了一种处理日期和时间的方式,使得可以方便地进行日期和时间的计算、比较以及格式化。Calendar
类是一个抽象类,因此不能直接实例化,通常通过getInstance()
方法获取Calendar
对象的实例。
3.2 常用方法
代码演示:
get演示
public class Demo01 {
public static void main(String[] args) {
//1.获取一个GregorianCalendar对象
Calendar instance = Calendar.getInstance();//获取子类对象
//2.打印子类对象
System.out.println(instance);
//3.获取属性
int year = instance.get(Calendar.YEAR);
int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
int day = instance.get(Calendar.DAY_OF_MONTH);
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
int week = instance.get(Calendar.DAY_OF_WEEK);//返回值范围:1--7,分别表示:"星期日","星期一","星期二",...,"星期六"
System.out.println(year + "年" + month + "月" + day + "日" +
hour + ":" + minute + ":" + second);
}
}
set演示
public class Demo02 {
public static void main(String[] args) {
//设置属性——set(int field,int value):
Calendar c1 = Calendar.getInstance();//获取当前日期
//计算班长出生那天是星期几(假如班长出生日期为:1998年3月18日)
c1.set(Calendar.YEAR, 1998);
c1.set(Calendar.MONTH, 3 - 1);//转换为Calendar内部的月份值
c1.set(Calendar.DAY_OF_MONTH, 18);
int w = c1.get(Calendar.DAY_OF_WEEK);
System.out.println(w);
}
}
add演示
public class Demo03 {
public static void main(String[] args) {
//计算200天以后是哪年哪月哪日,星期几?
Calendar c2 = Calendar.getInstance();//获取当前日期
c2.add(Calendar.DAY_OF_MONTH, 200);//日期加200
int y = c2.get(Calendar.YEAR);
int m = c2.get(Calendar.MONTH) + 1;//转换为实际的月份
int d = c2.get(Calendar.DAY_OF_MONTH);
int wk = c2.get(Calendar.DAY_OF_WEEK);
System.out.println("200天后是:" + y + "年" + m + "月" + d + "日" + "星期" + wk);
}
}
4.JDK8时间相关类
一般时间可以用LocalDateTime类来自定义格式
用法:
public class Demo01 {
public static void main(String[] args) {
//1. 使用 now() 返回表示当前日期时间的 对象
LocalDateTime ldt = LocalDateTime.now(); //LocalDate.now();//LocalTime.now() System.out.println(ldt);
//2. 使用 DateTimeFormatter 对象来进行格式化
// 创建 DateTimeFormatter 对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//把LocalDateTime对象转换为字符串
String format = dateTimeFormatter.format(ldt);
System.out.println("格式化的日期= " + format);
}
}