目录
①情景1:传入String year,String month,要求查当月的数据
(一)Date类
分为 java.util.Date 和 java.sql.Date
其中 sql.Date类继承于util包
①util.Date
早期用来表示时间和日期的类,包含年月日时分秒,非线程安全,我们可以对它的时间进行修改
它的大部分方法已经被淘汰,主要方法获取当前时间:
他的很多操作与1970 年 1 月 1 日 00:00:00 UTC(协调世界时)相关,比如构造函数,像上面我不传参,他就自动获取当前系统时间;如果我想指定一个时间,就必须传从1970年1月1日0时开始到指定时间所经历的毫秒数
util.Date类还有几个其余方法,平时用的不多,在这里不赘述,因为用的不多
多数情况下util.Date都会转成别的类型再操作
说两个重要的:util.Date转String和util.Date转sql.Date
自己可以测试一下
public static void main(String[] args) {
//util.Date格式:Fri Jan 03 10:59:51 CST 2025
Date date1 = new Date();
//转换为年月日时分秒 格式:2025-01-03 10:59:51
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(date1));
//转换为年月日 格式:2025-01-03
java.sql.Date date2 = new java.sql.Date(date1.getTime());
System.out.println(date2);
}
②sql.Date
老版本java用于在SQL数据库中处理日期的类,只包含年月日,时间部分截断为00:00:00(因为这样与sql中的DATE类型符合)
他继承自util.Date所以可用的api也很少,区别就是上面的格式
util.Date是 Fri Jan 03 10:59:51 CST 2025
sql.Date是 2025-01-03
(二)Local家族
java8之后通过java.time包引入的,这三位都是不可变的(即一旦创建就不可修改它对应的时间,当你做修改时,实际上是创建了一个新的时间对象),线程安全
①LocalDate
只包含年月日,具体用法如下:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now(); //获取当前时间
//返回格式:2025-01-03
System.out.println(currentDate);
}
}
LocalDate.of()方法,在mvc中常用
比如这里传的是2025,1,1,实际开发中可能就是year,month,day
//创建
LocalDate specificDate = LocalDate.of(2025, 1, 1);
//返回格式:2025-1-1
System.out.println(specificDate);
其他常用方法:
getYear()获取年,getMonthValue()获取月,getDayOfMonth()获取日
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now(); //获取当前时间2025-1-3
int year = localDate.getYear(); //返回2025
int month = localDate.getMonthValue() //返回1
int day = localDate.getDayOfMonth(); //返回3
}
}
修改时间
LocalDate.withYear();
LocalDate.withMonth();
LocalDate.withDay();
LocalDate.withHour();等等,Localdate和LocalDateTime都能用
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
int newHour = 12;
LocalDateTime modifiedDateTime = localDateTime.withHour(newHour);
System.out.println("原始日期时间: " + localDateTime);
System.out.println("修改小时后的日期时间: " + modifiedDateTime);
}
}
还有isBefore、isAfter、equals、plusDays等方法,不过多赘述,问问gpt就会用
②LocalTime
只包含时分秒,和LocalDate一模一样,只不过把年月日换成时分秒
③LocalDateTime
包含年月日时分秒,和LocalDate、LocalTime类似,只是变量更多罢了
(三)Calendar类
包含年月日时分秒
java.util.Calendar比util.Date更灵活,操作方法如下:
通常使用Calendar.getInstance()静态方法来获取一个Calendar实例,这个方法会根据系统默认的时区和本地化设置来初始化Calendar对象
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
}
}
通过get和set方法,可以操控calendar时间的各种值
//小坑点:月份从0开始,年日不是
calendar.set(Calendar.YEAR, 2025);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 3);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);
Date与Calendar互转
//calendar转date
//calendar也有对应的毫秒数,调用getTime()可以转为date类型
java.util.Date date = calendar.getTime();
System.out.println(date);
//date转calendar
//要先创建一个calendar类型,然后通过setTime()把date转为calendar
java.util.Date anotherDate = new Date();
Calendar anotherCalendar = Calendar.getInstance();
anotherCalendar.setTime(anotherDate);
(四)SimpleDateFormat类
特点:非线程安全、Date类型适用,可直接使用
对于日期和时间进行格式化和解析,主要作用如下:
①将Date对象转换为指定格式的字符串(常用)
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
}
}
首先定义一个simpleDateFormat对象sdf,指定对应的日期格式,yyyy-MM-dd HH:mm:ss是对应的模式字符;然后调用sdf的format方法,把date对象作为参数传入并将其转换为对应格式的字符串
②将字符串解析为Date对象
String dateString = "2025-01-01 12:30:00";
try {
Date parsedDate = sdf.parse(dateString);
System.out.println(parsedDate);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
(五)DateTimeFormat类
特点:线程安全,LocalDate、LocalDateTime等均适用
使用方法:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2025, 1, 1);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = localDate.format(dtf);
System.out.println(formattedDate);
}
}
(六)开发怎么用
实战情景演练:
①情景1:传入String year,String month,要求查当月的数据
和数据库对接最简单的方法就是转成String类型
在xml动态sql中,可以用
#{endDate} >= 'user.create_time'(你在数据库中的日期字段) 以及
'user.create_time' >= #{startDate} 完成
第一种方法,暴力拼接时分秒,传入mapper.xml
第二种方法,用LocalDateTime.of(),拼接时分秒,然后再用DateTimeFormatter转成String类型
①情景2:传入Date,要求根据日期来查
传入一个日期Date 比如2025-01-10,要求查找这天的数据
使用simpleDateFromat转换就能变成String类型,再把String类型传入xml即可
假如simpleDateFromat定义的格式是年月日,都能转;如果定义的格式是年月日时分秒,就有一点不同:
如果是util.Date,时分秒也可以携带
如果是sql.Date,时分秒默认为0时0分0秒
(七)总结
①这些日期的格式是什么样的?
util.Date 年月日时分秒 Fri Jan 03 10:59:51 CST 2025
sql.Date 年月日 2025-01-03
LocalDate 年月日 2025-01-03
LocalTime 时分秒 10:59:51
LocalDateTime 年月日时分秒 2025-01-03 10:59:51
Calendar 年月日时分秒 2025-01-03 10:59:51
②什么时候用什么类型?
util.Date和sql.Date在老版本中常用于实体类上,和数据库中的日期字段对应
Date类型不支持修改直接修改年月日等的方法,需要借助Calendar修改
LocalDate是java8之后推出的,优点是性能安全,提供了丰富的api,可操作性更强
可以调用它自身的api进行时间的修改
Calendar可以方便我们设置和调整日期时间的各个部分,修改年月日时分秒这些属性的值,常用来和Date协作
SimpleDateFormat和DateTimeFormat用来修改日期的格式
其中SimpleDateFormat用来修改Date类型比较多,DateTimeFormat用来修改LocalDate等类型比较多