1. String and Date (java.util.Date) are transferred to each other
1.1 String -> Date
String dateStr = "// ::"; Date date = new Date(); //Note that the format of format should match the format of date String DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { date = sdf.parse(dateStr); System.out.println(date.toString()); } catch (Exception e) { e.printStackTrace(); } String dateStr = "2010/05/04 12:34:23"; Date date = new Date(); //Note that the format of format should match the format of date String DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { date = sdf.parse(dateStr); System.out.println(date.toString()); } catch (Exception e) { e.printStackTrace(); } 1.2 Date -> String
Convert date to string, you can set any conversion format
String dateStr = ""; Date date = new Date(); //The format of format can be arbitrarily DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try { dateStr = sdf.format(date); System.out.println(dateStr); dateStr = sdf.format(date); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } String dateStr = ""; Date date = new Date(); //The format of format can be arbitrarily DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try { dateStr = sdf.format(date); System.out.println(dateStr); dateStr = sdf2.format(date); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } 2. String and Timestamp are transferred together
2.1 String -> Timestamp
Use Timestamp's valueOf() method
Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = "-- ::"; try { ts = Timestamp.valueOf(tsStr); System.out.println(ts); } catch (Exception e) { e.printStackTrace(); } Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = "2011-05-09 11:49:45"; try { ts = Timestamp.valueOf(tsStr); System.out.println(ts); } catch (Exception e) { e.printStackTrace(); }Note: The type of String must be in the form of: yyyy-mm-dd hh:mm:ss[.f...], with brackets indicating optional, otherwise an error will be reported! ! !
If String is in another format, you can consider re-parsing the string and reorganizing it~~
2.2 Timestamp -> String
Use Timestamp's toString() method or borrow DateFormat
Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = ""; DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { //Method 1 tsStr = sdf.format(ts); System.out.println(tsStr); //Method 2 tsStr = ts.toString(); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); } Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = ""; DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { //Method 1 tsStr = sdf.format(ts); System.out.println(tsStr); //Method 2 tsStr = ts.toString(); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); } It is easy to see that the advantage of Method 1 is that it can flexibly set the form of strings.
3. Date ( java.util.Date ) and Timestamp are transferred together
Statement: Looking at the API, we can see that Date and Timesta are parent-child relationships
3.1 Timestamp -> Date
Timestamp ts = new Timestamp(System.currentTimeMillis()); Date date = new Date(); try { date = ts; System.out.println(date); } catch (Exception e) { e.printStackTrace(); } Timestamp ts = new Timestamp(System.currentTimeMillis()); Date date = new Date(); try { date = ts; System.out.println(date); } catch (Exception e) { e.printStackTrace(); } It's very simple, but at this moment the entity pointed to by the date object is a Timestamp, that is, date has the method of the Date class, but the execution entity of the overwritten method is in the Timestamp.
3.2 Date -> Timestamp
The parent class cannot be directly converted to the child class, you can use the middle String~~~~
java.sql.Date only stores date data but not time data // Time data will be lost preparedStatement.setDate(1, new java.sql.Date(date.getTime()));// PreparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime())); // If you want to get complete data, including date and time, you can java.util.Date d = resultSet.getTimestamp(1);// This is more appropriate to avoid some potential Timestamp problems java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());
If you make up for it yourself, this is the case:
When storing the database, you can receive the java.util.Date type and use the getTime() method to obtain the long value representing the Date object, and then use this long value to construct a Timestamp object to store it in the database.
When fetching from the storage database, you can first get Timestamp and use its getTime() method to get the long value, and then use this long value to construct a java.util.Date object, so that you can operate on this Date object. It's better to say new SimpleTimeFormat("yyyyy-MM-dd HH:mm:ss").format(), etc.