In the process of development, we have to encounter the situation of displaying dates anyway. The types of dates are complicated and numerous. So the most common attributes that personal habits look at are in the form of YYYY-MM-DD, but we set the attributes in the database. Date properties are often of type Date.
I encountered such a problem during development.In the background, I output a Date variable read from the database, and its display is 2023-12-31, but when I return it to the front desk, it becomes: December 31, 2023. When I encounter this problem, my first thought is to convert it into a String type and then echo it. , then start the operation directly:
List<PageData> userList = auditPlanService.getAuditPlanUser(pd); // 列出用户列表
for (int i = 0; i < userList.size(); i++) {
System.out.println("当前类为:" + userList.get(i).get("PLAN_TIME").getClass().getName());//当前类为:java.sql.Date
}
The displayed class is: java.sql.Date
In other words, the userList.get(i).get("PLAN_TIME") read is the java.sql.Date class. We can also output:
For: 2023-11-22
Then let’s look at the front-end: The front-end echoes this format. I checked it and it’s called the Chinese date format. Let’s talk about the solution directly.
List<PageData> userList = auditPlanService.getAuditPlanUser(pd); // 列出用户列表
for (int i = 0; i < userList.size(); i++) {
userList.get(i).put("PLAN_TIME", userList.get(i).get("PLAN_TIME").toString());
}
Just use toString to convert it into a String class echo. You can see that the display is normal. At this point, the problem is solved., then we can continue Let’s explore: Aboutjava.sql.Date and java.util.Date
Second: 关于java.sql.Date and java.util.Date
1.java.util.Date
:
java.util.Date
The class is in the java.util
package.
java.util.Date
Represents the date and time of an instant, accurate to milliseconds. It holds the number of milliseconds since January 1, 1970 00:00:00 GMT.
2.java.sql.Date
:
java.sql.Date
The class is in the java.sql
package.
java.sql.Date
is designed to work with SQL's DATE
type. It inherits from java.util.Date
, but its time part is specified as 00:00:00 (midnight) and milliseconds are not saved.
java.sql.Date
is widely used in JDBC for date interaction with databases. java.sql.Date
is typically used when you need to store a date in a database without the time part.
In general:
-
java.util.Date
Used for general date and time operations, accurate to milliseconds. -
java.sql.Date
is designed to be compatible with the SQLDATE
type, mainly used in JDBC, and the time part is specified as 00:00:00.
In modern Java programming, it is more recommended to use the new date and time APIs in the java.time
package (for example, LocalDate
, LocalDateTime
), because it provides richer functionality and better design. However, you may still need to use java.sql.Date
when interacting with the database.
So now that we’ve talked about itLocalDate class: Detailed explanation of dates in JAVA_java localdate Gets the current date_Garos’ blog-CSDN bloghttps ://blog.csdn.net/jialuosi/article/details/133770363?spm=1001.2014.3001.5502LocalDate,那就说一下他与Date的区别。(关于LocalDate的详细使用,可以看我之前发过的一篇文章)
Three: AboutLocalDate与Date
1.LocalDate
(Java 8 and later):
(1):LocalDate
is a class in thejava.time
package introduced in Java 8 and belongs to the new date and time API.
(2):
It represents a date without time and time zone information.
(3):LocalDate
Immutable, thread-safe, and therefore more suitable for use in multi-threaded environments.
(4):
It is not affected by time zone and is more suitable for processing date-related logic without considering time zone issues.
2.Date
(old date class, Java 7 and before):
(1):Date
is an early date class in Java and exists in thejava.util
package.
(2):
It contains date and time information, expressed in milliseconds since January 1, 1970.
(3):Date
is mutable and therefore requires additional synchronization measures when used in a multi-threaded environment.
(4):
has been replaced by the new date and time API (java.time
package). It is recommended to use classes such asLocalDate
in new code.