Java insert time to MySQL, Time date format

Source: Internet
Author: User
Tags time 0

Java inserts the current time to MySQL several ways and the Java time date format several ways: (Data reference network resources)1There are several ways in which Java inserts the current time into MySQL, the first of these: Convert the time of the java.util.Date type to the Java.sql.Date type recognized by the MySQL database time Note: Java.util.Date is the parent of java.sql.Date: I have defined a subclass cat, It inherits the animal class, then the latter is the parent class. Through Cat c=NewCat (); Instantiate a Cat object, but when I define this: Animal a =NewCat (); it means I have defined a reference to the animal type, pointing to the new Cat type object. Because Cat is inherited from its parent class animal, references to animal types can point to objects of the cat type. Date Time=Newjava.sql.Date (Newjava.util.Date (). GetTime ()); the second: Java uses PreparedStatement to setdate, assigning a date question mark in the form of a question mark
Pstmt.settimestamp (1,NewTimestamp (System.currenttimemillis ()));p Stmt.setdate (1,Newjava.sql.Date (Newdate (). GetTime ())); Third: How to use Hibernate to provide database operations
As long as the java.util.Date type is set, take Hibernate's Pojo class object as an example, Pojo.set (Newjava.util.Date ()); it is available. MySQL and Java time type the time type of MySQL has the corresponding time type in Ava date java.sql.Date Datetime java.sql.Ti Mestamp Timestamp Java.sql.Timestamp time java.sql.Time year java.sql.Date so is implemented in the following way: date Date=NewDate ();//get the system time.SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"); String Nowtime= Sdf.format (date);//convert the time format to a format that meets the timestamp requirements.Timestamp dates =timestamp.valueof (nowtime);//Convert the time2. Java time Date format several methods 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 (); } } }

Java insert time to MySQL, Time date format

Related Article

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.