Java.util.Date, Java.sql.Date, Java.sql.Time, java.sql.Timestamp differences and summaries

Source: Internet
Author: User
Tags date1 dateformat local time locale

java.sql. Date,java.sql. Timestamp differences and summaries

Java.lang.Object
....| __java.sql. date/java.sql. Time


"Parent class"java.sql. Date format: month date [date data not stored time data]
"Subclass"java.sql. Timestamp date format: Day of the month and seconds (nanosecond)

Choose a different date type for different databases
· The date type of Oracle, which takes only a month and a day, is chosen to use java.sql. Timestamp type


------------------------------------------

System time is used as standard data within all four types of objects
• System time: The number of milliseconds since January 1, 1970 00:00:00 GMT, Greenwich Mean Time (GMT)
• Local time: Time to print according to time zone [when the zone is gmt+0, the system time is the same as the local time]

We are using local time as the reference standard
------------------------------------------

String date format converted to date format

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Public Class A {
 
  //main function
   public static void Main (string[] args)
 & nbsp {
   //  string str=" 2011-5-31 14:40:50 ";
  try {
    Java.util.Date utildate=f_utildate.parse (str);
   system.out.println (F_utildate.format (utildate));
  } catch (ParseException e) {
   //TODO auto-generated catch block
  & Nbsp;e.printstacktrace ();
  }

// java.sql. Date.valueof ("2010-08-20");
System.out.println (F_sqldate.format (sqldate));

// java.sql. Time.valueof ("13:44:53");
System.out.println (F_sqltime.format (sqltime));

// java.sql. Timestamp.valueof ("2010-08-20 14:06:27.186");
System.out.println (F_timestamp.format (timestamp));
}
}

*******************************************************************************

//java.sql.Date format
try{
SimpleDateFormat dateformate = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
java.util.DateDate1 = Dateformate.parse ("2011-5-31 14:40:50");
java.sql.Date (Date1.gettime ());
System.out.println (Dateformate.format (sqldate));
}catch (Exception ex) {
System.out.println (Ex.getmessage ());
}

//java.util.Date format
java.sql. Date.valueof ("2005-12-12");
java.util.Date (Sqldate1.gettime ());
System.out.println ("java.util.Date Format:" +f.format (utilDate1));

//java.sql. Timestamp
New java.util.Date(). GetTime ());//IDE Error here

//java.sql. Time
New java.util.Date(). GetTime ());

Timestamp Timestamp = new Timestamp (System.currenttimemillis ());

We can use DateFormat to process strings to define the format of time dates
Note: string is first converted to java.util.Dateand then converted to the desired format

Example:
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Locale;

Import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public Class A {
public static void Main (string[] args) throws parseexception{
try{
String datestring = "2010-08-20 12:00:00.125";
DateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd kk:mm:ss. SSS ", locale.english);//Set format
Dateformat.setlenient (FALSE);
java.util.DateUtildate = Dateformat.parse (datestring);//util type
java.sql.Timestamp (Utildate.gettime ());//timestamp type, Timedate.gettime () returns a long type
System.out.println (DateTime);
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}

Java.util.Date, Java.sql.Date, Java.sql.Time, java.sql.Timestamp differences and summaries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.