Timestamp basic knowledge and time to compare the size

The main write the date when the connection to the database processing, pstmt.setDate () is the type of java.sql.Date type, this type of compliant and in fact did not exist when the minutes and seconds into the database, so you should use when accessing setTimestamp () or getTimestamp ().

Finishing a:

java.sql.Date only stores the date data is not stored time data

// lose time data

preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

// it can be to deal with

preparedStatement.setTimestamp(1, new Timestamp(new java.util.Date().getTime()));

 

// we want to get complete data, including date and time, it can be

java.util.Date d = resultSet.getTimestamp(1);

// this treatment more appropriate to avoid some potential Timestamp issues

java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());

   When the storage to the database may receive java.util.Date type then getTime () method to obtain representative of the Date object long value, then the long Constructs a Timestamp object is stored into the database.

       When taken from the memory database, you may be obtained either Timestamp with his getTime () method to obtain long value, then the long Constructs a java.util.Date object so that this can Date object is operated. For example, new SimpleDateFormat. ( "Yyyyy-MM -dd HH: mm: ss") format (Date) or the format ( Timestamp ) will do -

 

Finishing two:

With Timestamp to record the date and time is very convenient, but is not required when the display is sometimes behind the decimal milliseconds, so it is necessary to convert the String redefined format.

String into a Timestamp:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// define the format, not displayed ms

Timestamp now = new Timestamp(System.currentTimeMillis());

// get the current time

String str = df.format(now);        

String into a Timestamp:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");

String time = df.format(new Date());

Timestamp ts = Timestamp.valueOf(time);    

 

Finishing three:

In ResultSet we often use setDate or getDate data type is java.sql.Date , and in the usual java program we are generally accustomed to using java.util.Date . So in DAO layer we often encounter each other and maybe convert the data types.

Relationship between the two

java.lang.Object

    |

    + --- java.util.Date

            |

            +----java.sql.Date

Conversion

1. Use getTime () function

Both classes provide getTime () function returns the number of milliseconds for the corresponding ( Long type). Using this function can convert:

    java.util.Date utilDate = new java.util.Date(sqlDate.getTime());   // sql -> util

    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());   // util -> sql

 

2. Use SimpleDateFormat class implements conversion

SimpleDateFormat is a concrete class analysis and data formatting locale-sensitive manner. It allows to format (DATE -> text) , parsing (text -> date) and standardization.

SimpleDateFormat dateFormat = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss");

java.util.Date utilDate = dateFormat.parse(sqlDate.toString());

 

3. direct conversion

Since java.sql.Date from java.util.Date inheritance over, so you can directly use:

utilDate = sqlDate;

 

4. The alternative method of obtaining a date:

SimpleDateFormat sy=new SimpleDateFormat("yyyy");

SimpleDateFormat sm=new SimpleDateFormat("MM");

SimpleDateFormat sd=new SimpleDateFormat("dd");

String syear = sy.format (date);

String smon=sm.format(date);

String sday=sd.format(date);

 

PS :. 1 java.util.Date class the getYear () to add 1900 before actual values obtained, The getMonth () will have to add 1.

        2. String to Date conversion: Date.valueOf (str), remember when the lead pack to lead the Date sql package, not the util package of Date

        3. String to convert Timestamp Timestamp ts = Timestamp.valueOf (time);    

Reproduced in: https: //my.oschina.net/usenrong/blog/197850

Guess you like

Origin blog.csdn.net/weixin_34194379/article/details/92028885