Conversion of time formats in programming languages and time formats in relational data has always been a simple and trade-off problem, this involves the programming of database design and SQL programming and the processing of Time-format data in applications. Both of them can be intertwined.
For the time format in Java and the time format conversion in the database, write two methods here:
/**
*
* @ Param utilDate
* @ Return
*/
Public static java. SQL. Date convertUtilDateToSqlDate (java. util. Date utilDate ){
Java. SQL. Date sqlDate = new java. SQL. Date (utilDate. getTime ());
Return sqlDate;
}
/**
*
* @ Param sqlDate
* @ Return
*/
Public static java. util. Date convertSqlDateToUtilDate (java. SQL. Date sqlDate ){
Java. util. Date utilDate = new java. util. Date (sqlDate. getTime ());
Return utilDate; www.2cto.com
}
The Normal conversion is quite simple, but it takes some effort to get a certain format.
1. Write Data of the field type Date to the database from the application
For example: PreparedStatement. setDate (int, java. SQL. Date) method Description: The java. SQL. Date method can be converted
For example: TO_DATE (new SimpleDateFormat (). format (ud, "yyyy-MM-dd HH: mm: ss"), "YYYY-MM-DD HH24: MI: SS ")
You can use an SQL statement to handle this problem: SQL = "UPDATE TABLENAME SET TIMER = TO_DATE (" "+ TIMEMISS +" ", 'yyyymmddhh24miss ') WHERR... "Note TIMEMISS is a variable, for example, 20080522131223 type
2. Convert the specified format string to the database time type java. SQL. Date
You can achieve this by using the following methods:
Method 1:
Public static java. SQL. Date convertStringToSqlDate (String formatStr, String timeStr ){
SimpleDateFormat bartDateFormat = new SimpleDateFormat (formatStr );
Java. SQL. Date sqlDate = null;
Try {
Java. util. Date date = bartDateFormat. parse (timeStr );
SqlDate = new java. SQL. Date (date. getTime ());
System. out. println (sqlDate. getTime ());
} Catch (Exception ex ){
System. out. println (ex. getMessage ());
}
Return sqlDate;
}
Method 2:
String strDate = "2002-08-09 ";
StringTokenizer st = new StringTokenizer (strDate ,"-");
Java. SQL. Date date = new java. SQL. Date (Integer. parseInt (st. nextToken ()));
This problem can be solved in different ways.
3. About java. SQL. Date and java. SQL. Date
Java. SQL. Date is the data type set to work with SQLDATE.
The normalized java. SQL. Date only contains the year, month, and day information, and the time, minute, and second milliseconds are cleared. The format is similar to YYYY-MM-DD.
When we call the getDate () method of ResultSet to obtain the returned value, the java program uses java. SQL. Date of the "standard" to format the value in the database. Therefore, the information of the nonstandard part in the database will be stolen.
In the ResultSet. java provided by sun, comment on getDate as follows:
Retrieves the of the designated column in the current row of this <code> ResultSet </code> object as a "java. SQL. Date" object in the Java programming language.
If we set a java. SQL. when the Date value is stored in the database through the setDate method of PrepareStatement, the java program will. SQL. date normalization, the nonstandard part will be stolen. However, we use java. SQL. date is generally set by java. util. date conversion, such as java. SQL. date sqlDate = new java. SQL. date (new java. util. date (). getTime ()).
Obviously, the converted java. SQL. date is often not a standard java. SQL. date. to save java. util. the exact value of Date. In this case, java. SQL. timestamp. it is a good choice.
4. one-size-fits-all approach
There have been some small controversy around me about this practice. One teacher told us to set all the time types in the database to the string type, another instructor told us that designing database table fields is the least professional practice during application development. However, we finally found that if the time data is used as java in the application. util. date, and the type of the time data field in the database is Date, so the above conversion methods will be particularly meaningful in time data update or query.
Later I thought about this question, why didn't I convert the classes of the two types of adapters? This is not more convenient.
Now it seems to be clear. If the database updates the time field from the database server, you can safely use the time type in the database, and it is quite easy and free to read data from the database and switch the data in the application; on the contrary, if the update of the time field in the database comes from an external application, you can design the time field in the database as a string because it serves the application for the database, in addition, the time format from the outside will be easier to control and set.
5. once and for all
A rough idea is that there is no conversion similar to the time format because the time format we need to display in actual applications is ever-changing, therefore, you can only analyze specific business needs and write a tool class to deal with these problems. Of course, such work is always done.
Here is a processing column:
// Obtain the current time and declare the time variable
Calendar calendar = Calendar. getInstance ();
// Get the year
Int year = calendar. get (Calendar. YEAR );
// Get the month, but add 1 to the month
Int month = calendar. get (Calendar. MONTH) + 1;
// Obtain the date
Int day = calendar. get (Calendar. DATE );
// Convert the string to the datetime format
String today = "" + year + "-" + month + "-" + day + "";
Well, there is not much time. Once and for all, it is based on a high degree of abstraction and abstraction, and analysis and extraction are based on analysis.