The difference between Date Calendar and Timestamp in Java and their mutual conversion and use

  • 2020-04-01 02:18:03
  • OfStack

1 the Java. Util. Date
Contains year, month, day, hour, minute and second information.


//String to Date
String dateStr="2013-8-13 23:23:23";
String pattern="yyyy-MM-dd HH:mm:ss";
DateFormate dateFormat=new SimpleDateFormat(pattern);
Date date=dateFormat.parse(dateStr);
date=dateFormat.format(date);

Java 2. SQL. The Date
Contains year, month, and day information.
Inherited from java.util.Date. Used in database-related operations such as rs.getdate, ps.setdate, etc. Rs is a ResultSet, ps is a PreparedStatement.

//Java.util.date is converted to java.sql.date
new java.sql.Date(utilDate.getTime());//The utilDate is an object of type java.util.date

3 Java. Util. Calendar
Contains year, month, day, hour, minute, second, millisecond information.
JDK1.1 was introduced to replace java.util.Date.

//The Date to the Calendar
Date date=new Date();
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
//The Calendar to Date
Calendar ca=Calendar.getInstance();  
Date d =(Date) ca.getTime();

4 the Java. SQL. Timestamp
Contains year, month, day, hour, minute, second, nano information.
Inherited from java.util.Date. Date contains more information than java.sql.date. Used in database-related operations such as rs.gettimestamp, ps.settimestamp, etc. For example, if a field hireDate in the database is of type Date of Oracle, the information of year, month, day, hour, minute and second can be extracted by using getTimestamp. But when you use getDate, you can only fetch the year, month, and day information. Therefore, getTimestamp is generally recommended.

//Java.util.calendar is converted to java.sql.timestamp
new Timestamp(Calendar.getInstance().getTimeInMillis());
//Java.util.date is converted to java.sql.timestamp
new Timestamp(date.getTime());
//String converted to java.sql.timestamp, String format: yyyy-mm-dd hh:mm:ss[.f...] , the square brackets indicate optional
Timestamp.valueOf("2013-07-06 01:49:30");

5 date and time types provided by Oracle database
The Oracle database provides four types: DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH LOCAL TIME ZONE.

DATE contains century, year, month, day, hour, minute, and second information.
TIMESTAMP is an extension of DATE, containing year, month, day, hour, minute, second, and fractional seconds information. Define the format of TIMESTAMP As follows:


TIMESTAMP [(fractional_seconds_precision)]
//format
TIMESTAMP 'YYYY-MM-DD HH24:MI:SS.FF'
//A case in point
TIMESTAMP '1997-01-31 09:26:50.12'

Where fractional_seconds_precision is optional and is used to specify that the seconds are represented as a floating-point number with several decimal places, ranging from 0 to 9, with a default of 6. In the example above, it is expressed as a two-digit decimal, whose second value is 50.12. Note: 12 is not a millisecond value, nor is it a microsecond value.


Related articles: