- Blog
- Community
- Homepage
- New essay
- Contact
- Management
- Subscription
Post-42-0 comments-72 differences between java. util. Date and java. SQL. Date and the application of java. util. Date are used in addition to SQL statements.
Java. SQL. Date is used for SQL statements. It contains only the Date but not the time part.
It has the getTime method that returns the number of milliseconds, and can be directly constructed.
Java. util. Date d = new java. util. Date (sqlDate. getTime ());
...
--------------------------------------------------------------------------------
Java. util. Date is the parent class of java. SQL. Date (note spelling)
The former is a commonly used time class. We usually use it to format or obtain the current time.
The latter is used when reading and writing the database, because the 2nd parameter of setDate () of PreparedStament and the 2nd parameter of getDate () method of ResultSet are both java. SQL. Date
Conversion is
Java. SQL. Date date = new Java. SQL. Date ();
Java. util. Date d = new java. util. Date (date. getTime ());
The opposite is true.
--------------------------------------------------------------------------------
Inheritance relationship: java. lang. Object -- java. util. Date -- java. SQL. Date
The specific conversion relationship is java. util. Date d = new java. util. Date (new Java. SQL. Date ());
--------------------------------------------------------------------------------
SQL. date, usually in the database time field, util. date is generally a daily date Field
--------------------------------------------------------------------------------
Java. SQL. Date is mainly used in SQL!
Java. util. Date can be used in general environments!
--------------------------------------------------------------------------------
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Java. util. Date utilDate = new Date ();
Java. SQL. Date sqlDate = new java. SQL. Date (utilDate. getTime ());
Java. SQL. Time sTime = new java. SQL. Time (utilDate. getTime ());
Java. SQL. Timestamp stp = new java. SQL. Timestamp (utilDate. getTime ());
System. out. println (utilDate. getYear ());
All the time and date can be formatted by SimpleDateFormat format ()
F. format (stp); f. format (sTime); f. format (sqlDate); f. format (utilDate)
Java. SQL. Date sqlDate = java. SQL. Date. valueOf ("2005-12-12 ");
UtilDate = new java. util. Date (sqlDate. getTime ());
Bytes --------------------------------------------------------------------------------------------------
The alternative method is to get the year, month, and day:
Import java. text. SimpleDateFormat;
Import java. util .*;
Java. util. Date date = new java. util. Date ();
// If you want to obtain the format of YYYYMMDD
SimpleDateFormat sy1 = new SimpleDateFormat ("yyyyMMDD ");
String dateFormat = sy1.format (date );
// If You Want To separately obtain the year, month, and day
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 );
Java. util. Date is used in addition to SQL statements.
Java. SQL. Date is used for SQL statements. It contains only the Date but not the time part.
It has the getTime method that returns the number of milliseconds, and can be directly constructed.
Java. util. Date d = new java. util. Date (sqlDate. getTime ());
...
--------------------------------------------------------------------------------
Java. util. Date is the parent class of java. SQL. Date (note spelling)
The former is a commonly used time class. We usually use it to format or obtain the current time.
The latter is used when reading and writing the database, because the 2nd parameter of setDate () of PreparedStament and the 2nd parameter of getDate () method of ResultSet are both java. SQL. Date
Conversion is
Java. SQL. Date date = new Java. SQL. Date ();
Java. util. Date d = new java. util. Date (date. getTime ());
The opposite is true.
--------------------------------------------------------------------------------
Inheritance relationship: java. lang. Object -- java. util. Date -- java. SQL. Date
The specific conversion relationship is java. util. Date d = new java. util. Date (new Java. SQL. Date ());
--------------------------------------------------------------------------------
SQL. date, usually in the database time field, util. date is generally a daily date Field
--------------------------------------------------------------------------------
Java. SQL. Date is mainly used in SQL!
Java. util. Date can be used in general environments!
--------------------------------------------------------------------------------
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Java. util. Date utilDate = new Date ();
Java. SQL. Date sqlDate = new java. SQL. Date (utilDate. getTime ());
Java. SQL. Time sTime = new java. SQL. Time (utilDate. getTime ());
Java. SQL. Timestamp stp = new java. SQL. Timestamp (utilDate. getTime ());
System. out. println (utilDate. getYear ());
All the time and date can be formatted by SimpleDateFormat format ()
F. format (stp); f. format (sTime); f. format (sqlDate); f. format (utilDate)
Java. SQL. Date sqlDate = java. SQL. Date. valueOf ("2005-12-12 ");
UtilDate = new java. util. Date (sqlDate. getTime ());
Bytes --------------------------------------------------------------------------------------------------
The alternative method is to get the year, month, and day:
Import java. text. SimpleDateFormat;
Import java. util .*;
Java. util. Date date = new java. util. Date ();
// If you want to obtain the format of YYYYMMDD
SimpleDateFormat sy1 = new SimpleDateFormat ("yyyyMMDD ");
String dateFormat = sy1.format (date );
// If You Want To separately obtain the year, month, and day
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 );