Time Processing of Jsp+mysql

Source: Internet
Author: User

Long time no use of MySQL, and found that a small number of times problems also encountered a lot of trouble, hehe

So to sum up:

First type:

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

The second type:

Java with PreparedStatement to Setdate, with the form of a question mark to the date question mark assignment

Pstmt.settimestamp (8, New Timestamp (System.currenttimemillis ()));

Pstmt.setdate (1, New Java.sql.Date (Date1.gettime ()));
Pstmt.setdate (2, New Java.sql.Date (Date2.gettime ()));


Third:

In fact, inserting a time field into the MySQL database is also very easy, as long as the java.util.Date type is set to the Hibernate Pojo class object, Pojo.set (new Java.util.Date ());

Relevant information found on the Internet in the following appendix:

Mysql and Java Time type

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


the following ways to achieve it:


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


String nowtime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date);//Converts the time format to a format that conforms to the timestamp requirement.


Timestamp goodsc_date = timestamp.valueof (nowtime);//convert Time to

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

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.