Before Java 8, there were two date classes: java.util.Date
, java.util.Calendar
and after Java 8, a brand new date class was launched java.time.LocalDate
. Next, let’s briefly introduce how to use them.
1. java.util.Date
Before talking about the java.util.Date class, let's talk about the method of another class first: System.currentTimeMillis()
Return the current time and Greenwich Mean Time 1970-01-01 00:00:00 (Beijing time: 1970-01-01 08: 00:00) in milliseconds.This method is suitable for calculating time differences
// 1626624005531 时间戳
Long time = System.currentTimeMillis();
System.currentTimeMillis()
The return type of the method is Long type. Generally used to obtain the execution time difference of a certain method or other. That is: get it once before the start, get it once at the end, and subtract the start time from the end time to get the execution time. like:
public class DateTest {
public void test() {
System.out.println("======test======");
}
public static void main(String[] args) {
DateTest dateTest = new DateTest();
Long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
dateTest.test();
}
Long endTime = System.currentTimeMillis();
Long result = endTime - startTime;
// 55 毫秒
System.out.println(result);
}
}
In the above code, the for loop calls the DateTest#test() method 10,000 times, which takes 55 milliseconds.
Convert timestamps to and from the java.util.Date class:
// Date 类型转换为时间戳
Date date = new Date();
Long time = date.getTime();
// 时间戳转换为 Date 类型
Long long = 1626624005531L;
Date nowTime = new Date(long);
Generally, the java.util.Date class is used together with the java.text.SimpleDateFormat class to format dates.
For example: Convert date to a string in a specific format:
public class DateTest {
public static void main(String[] args) {
String timePattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(timePattern);
String s = format.format(new Date());
System.out.println(s);
}
}
Convert string to date:
public static void main(String[] args) {
String timePattern = "yyyy-MM-dd HH:mm:ss";
String timeOne = "2021-07-18 22:53:22";
SimpleDateFormat format = new SimpleDateFormat(timePattern);
try {
Date nowDate = format.parse(timeOne);
System.out.println(nowDate);
} catch (ParseException e) {
System.out.println("日期格式化错误!!,日期为:" + timeOne);
}
}
2. java.sql.Date
java.sql.Date
It is a subclass of the java.util.Date class; it is used for SQL statements. It only contains the date without the time part. It is generally used when reading and writing the database, and both the PreparedStament#setDate()
parameters of the method and ResultSet#getDate()
the method are used java.sql.Date
.
How to get the current time using java.sql.Date?
public class DateTest {
public static void main(String[] args) {
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
// 2021-07-19
System.out.println(date);
}
}
Convert java.util.Date and java.sql.Date to and from each other:
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
java.sql.Date dat = new java.sql.Date(date.getTime());
}
}
3. java.util.Calendar
java.util.Calendar: Calendar class, an abstract class
Calendar instantiation method:
- Create an object of its subclass GregorianCalendar
- Call its static method getInstance()
In fact, these two methods are the same:
public class CalendarTest {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
// class java.util.GregorianCalendar
System.out.println(calendar.getClass());
}
}
Commonly used methods:
- get(int field): Get a certain time
- set(): set a certain time
- add(): add a certain time at a certain time
- getTime(): Convert Calendar class to Date class
- setTime(): Convert Date class to Calendar class
field values are:
- DAY_OF_MONTH: The day of this month
- DAY_OF_YEAR: The day of the year
- …
Notice:
- When getting the month: January is 0, February is 1,…
- When getting the day of the week: Sunday is 1, Monday is 2,…
get()
method
public static void main(String[] args) {
// 获取到的是当前时间
Calendar calendar = Calendar.getInstance();
int i = calendar.get(Calendar.DAY_OF_MONTH);
// 18,表示是这个月的第 18 天(今天是 2021-07-18)
System.out.println(i);
}
set(int field, int value)
method
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 23);
// 23。将当前时间设置成某个时间,即“2021-07-23”
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
}
add(int field, int value)
method
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
// 给当前时间加上 5 天
calendar.add(Calendar.DAY_OF_MONTH, 5);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
}
So, what if I want to add 2 months to the current time?
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
// 给当前时间加上 5 天
calendar.add(Calendar.MONTH, 2);
System.out.println(calendar.get(Calendar.MONTH));
}
Then, what about subtracting another 3 days?
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
// 给当前时间加上 5 天
calendar.add(Calendar.MONTH, 2);
// 减去 3 天
calendar.add(Calendar.DAY_OF_MONTH, -3);
System.out.println(calendar.get(Calendar.MONTH));
}
getTime()
method
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -3);
Date time = calendar.getTime();
// 获取 3 天前的日期
System.out.println(time);
}
setTime(Date date)
method
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
// 获取 3 天前的日期
calendar.add(Calendar.DAY_OF_MONTH, -3);
Date nowTime = new Date();
// 又获取到当前时间
calendar.setTime(nowTime);
}