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

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)

[Java]
  1. <span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.hudong.util.orther;
  2. Importjava.sql.Timestamp;
  3. Importjava.text.ParseException;
  4. Importjava.text.SimpleDateFormat;
  5. Import Java.util.Date;
  6. public class Transformdate {
  7. /**
  8. * Use the current time only as a condition for the MySQL timestamp field by date (time 0)
  9. * Final return Time type Java.sql.Date
  10. */
  11. Public Voidtransformcurdate () {
  12. SimpleDateFormat format = new SimpleDateFormat ("yyyy-mm");
  13. Java.sql.Date Timepara = null;
  14. try {
  15. Timepara = new Java.sql.Date (new Date (). GetTime ());
  16. System.out.println (Timepara);
  17. } catch (Exception e) {
  18. E.printstacktrace ();
  19. }
  20. }
  21. /**
  22. * Convert the current time of Java to the specified format (yyyy-mm-0100:00:00 ") as a condition of the MySQL timestamp field
  23. * Final return Time type Java.sql.Date
  24. */
  25. Public Voidtransformcuryearmon () {
  26. SimpleDateFormat format = new SimpleDateFormat ("yyyy-mm");
  27. String time = Format.format (new Date ()). Concat (" -0100:00:00");
  28. Java.sql.Date Timepara = null;
  29. try {
  30. Timepara = Newjava.sql.Date (Format.parse (Time). GetTime ());
  31. System.out.println (Timepara);
  32. } catch (ParseException e) {
  33. E.printstacktrace ();
  34. }
  35. }
  36. /**
  37. * Convert the current time of Java to timestamp as a condition of the MySQL timestamp field
  38. * Final return Time type Java.sql.Timestamp
  39. */
  40. public static void TestData () {
  41. try {
  42. SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-ddhh:mm:ss");
  43. Timestamp date = java.sql.Timestamp.valueOf ("2012-12-1201:12:11");
  44. SYSTEM.OUT.PRINTLN (date);
  45. } catch (Exception e) {
  46. E.printstacktrace ();
  47. }
  48. }
  49. /**
  50. * Processing current time only by date (time is 0)
  51. * Final return Time type Java.util.Date
  52. */
  53. public static void Datatest () {
  54. try {
  55. SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
  56. String time = Format.format (new Date ());
  57. Date date = Format.parse (Time.concat ("00:00:00"));
  58. SYSTEM.OUT.PRINTLN (date);
  59. } catch (Exception e) {
  60. E.printstacktrace ();
  61. }
  62. }
  63. public static void Main (String[]args) {
  64. TestData ();
  65. }
  66. }
  67. </span>

(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.