Some faded Java date-time functions

Source: Internet
Author: User
Tags current time date1 dateformat

Today, my roommate asked me how the Stmt.setdate () in JDBC was able to get through the current time, passing in the parameter new Java.util.Date (), passing in the parameter new Java.sql.Date (), and trying for a while, or not. At this point, I found that, since the use of HIBERNATE,JPA, I even forget the original things. This is a very sad question, decided to have a small test!

Package test;

Import Java.sql.Timestamp;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

public class Sqldate {

public static void Main (string[] args) {

System.out.println (System.currenttimemillis ()); Results: 1244711626453

Java.util.Date date1 = new Java.util.Date ();
System.out.println (Date1.tostring ()); Result: Thu June 16:27:57 CST 2009

Java.sql.Date extends Java.util.Date
Java.sql.Date date2 = new Java.sql.Date (System.currenttimemillis ());
System.out.println (Date2.tostring ()); Results: 2009-06-11

Java.sql.Date date3 = new Java.sql.Date (Date1.gettime ());
System.out.println (Date3.tostring ()); Results: 2009-06-11

Timestamp extends Java.util.Date
Timestamp stamp1 = new Timestamp (System.currenttimemillis ());
System.out.println (Stamp1.tostring ()); Results: 2009-06-11 16:27:57.75

Timestamp stamp2 = new Timestamp (Date1.gettime ());
System.out.println (Stamp2.tostring ()); Results: 2009-06-11 16:52:56.171

Date time converted to formatted string
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
Format (java.util.Date Date), timestamp is a subclass of Java.util.Date
String timestr = Sdf.format (STAMP1);
System.out.println (TIMESTR); Results: 2009/06/11 16:52:56

SimpleDateFormat extends DateFormat, DateFormat is an abstract class
DateFormat df = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
String timeStr2 = Df.format (date1);
String TIMESTR3 = Df.format (date2);
System.out.println (TIMESTR2); Results: 2009/06/11 16:52:56
System.out.println (TIMESTR3); Results: 2009/06/11 16:52:56

Convert String to date time
try {
The parameters in parse () must match the format defined in SDF, otherwise the exception will be thrown
Java.util.Date Date6 = Sdf.parse ("2009/12/10 5:12:02");
System.out.println (Date6.tostring ()); Results: Thu Dec 05:12:02 CST 2009

Java.sql.Date Date7 = new Java.sql.Date (Date6.gettime ());
System.out.println (Date7.tostring ()); Results: 2009-12-10

Note: the Sdf.parse () return value is java.util.Date type and cannot be converted to Java.sql.Date type
Java.sql.Date Date8 = (java.sql.Date) sdf.parse ("2009/12/10 5:12:02"); Not feasible

Timestamp STAMP9 = new Timestamp (Date7.gettime ());
System.out.println (Stamp9.tostring ()); Results: 2009-12-10 05:12:02.0
catch (ParseException e) {
E.printstacktrace ();
}

}
}

In this way, the use of several common date-time classes and the conversion of strings and date-time classes is straightforward. Posted to the blog, the next time you forget, open to see, also immediately remember!

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.