Java four ways to insert the current time into MySQL and several ways to format Java time date (case description)

Source: Internet
Author: User
Tags time 0 time and date

Tag: Mil MEP assigns a resource using the DateTime date () method Hiberna

Java four ways to insert the current time into MySQL and several ways to format Java time date (case description); Part of the Data Reference network resources

1. Four ways Java inserts the current time into MySQL

First: Convert the time of the java.util.Date type to the Java.sql.Date type time recognized by the MySQL database

Note: Java.util.Date is the parent class of Java.sql.Date

Date time= New Java.sql.Date (Newjava.util.Date (). GetTime ());

The second kind: Java uses the PreparedStatement to Setdate, assigns the date question mark in the form question mark the value

Pstmt.settimestamp (8, Newtimestamp (System.currenttimemillis ()));

Pstmt.setdate (1, New Java.sql.Date (Newdate (). GetTime ()));

Third: How to use Hibernate to provide database operations

In fact, it is easy to insert a time field into the MySQL database, as long as the java.util.Date type is set to the Pojo class object in Hibernate, Pojo.set (New Java.util.Date ()) is available.

IV: Using the valueof method of timestamp

The following appendices are found on the Internet:

Mysql vs. Java time types

The time type of MySQL has the corresponding time type in Java

Date Java.sql.Date

Datetime Java.sql.Timestamp

Timestamp Java.sql.Timestamp

Time Java.sql.Time

Year Java.sql.Date

This is done in the following way:

Date date = new Date ();//Gain system time.

String nowtime = new SimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"). Format (date),//Convert the time format to a format that meets the timestamp requirements.

Timestamp goodsc_date =timestamp.valueof (nowtime);//Convert time

2. Several ways to format Java time and date (case description)

Importjava.sql.timestamp;importjava.text.parseexception;importjava.text.simpledateformat;Importjava.util.Date; Public classTransformdate {/*** Direct the current time only by date (time 0) as the condition of the MySQL timestamp field * Final return time type Java.sql.Date*/     Publicvoidtransformcurdate () {SimpleDateFormat format=NewSimpleDateFormat ("yyyy-mm"); Java.sql.Date Timepara=NULL; Try{Timepara=NewJava.sql.Date (NewDate (). GetTime ());       System.out.println (Timepara); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Convert the current time of Java to the specified format (yyyy-mm-0100:00:00 ") as a condition of the MySQL timestamp field * Final return time type Java.sql.Date*/     PublicVoidtransformcuryearmon () {SimpleDateFormat format=NewSimpleDateFormat ("yyyy-mm"); String Time= Format.format (NewDate ()). Concat (" -0100:00:00"); Java.sql.Date Timepara=NULL; Try{Timepara=Newjava.sql.Date (Format.parse (Time). GetTime ());        System.out.println (Timepara); } Catch(ParseException e) {e.printstacktrace (); }    }    /*** Turn the current time of Java into Timestamp as the condition for the MySQL timestamp field * Final return time type Java.sql.Timestamp*/     Public Static voidTestData () {Try{SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"); Timestamp Date= Java.sql.Timestamp.valueOf ("2012-12-1201:12:11");       SYSTEM.OUT.PRINTLN (date); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Processing current time only by date (time 0) * Final return time type Java.util.Date*/     Public Static voiddatatest () {Try{SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd"); String Time= Format.format (NewDate ()); Date Date= Format.parse (Time.concat ("00:00:00"));       SYSTEM.OUT.PRINTLN (date); } Catch(Exception e) {e.printstacktrace (); }    }     Public Static voidMain (String[]args) {testData (); }}

(RPM) Java Four ways to insert the current time into MySQL and several ways to format Java time date (case description)

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.