Analysis of various conversion modes of Java date time and MySQL storage time type fields

Source: Internet
Author: User
Tags date1 mysql in mysql version time and date

One: The conversion method between various date

public class Timetest {public static void main (string[] args) {Date date = new Date (); First, get the current system time and date and format the output: SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format string datetime = Df.format (date); Formats a Date into a date/time string.  System.out.println (DateTime); 2017-09-24 23:33:20//Second, date converted to Datetimelong longtime = Date.gettime (); Timestamp Timestamp = new Timestamp (longtime); SYSTEM.OUT.PRINTLN (timestamp); 2017-09-24 23:33:20.655//Three, string converted to datestring Strtime = "2017-09-24 12:00:01"; SimpleDateFormat sdf1 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate date1 = null;try {date1 = Sdf1.parse (strtime);} C Atch (ParseException e) {e.printstacktrace ();} System.out.println (date1); Sun Sep 12:00:01 CST 2017//Four, date converted to stringdate date2 = new Date ();D ateformat format = new SimpleDateFormat ("yyyy-mm- DD "); String str1 = Format.format (date2); System.out.println (STR1); 2017-09-24//Five, string conversion to java.sql.DateString str2 = "2017-12-01"; SimpleDateFormat SDF2 = new SimpleDateFormat ("Yyyy-mm-dd"); Java.sql.Date sdate = null;try {java.util.Date date3 = Sdf2.parse (str2); sdate = new java. Sql. Date (Date3.gettime ());} catch (ParseException e) {e.printstacktrace ();} System.out.println (sdate); 2017-12-01}}

II: Date,datetime,timestamp and time types in MySQL database

DATETIME   type is used when you need a value that contains both date and time information. MySQL retrieves and displays the datetime value in ' yyyy-mm-dd HH:MM:SS ' format, supported by ' 1000-01-01 00:00:00 ' to ' 9999-12-31 23:59:59 '. ("support" means that although earlier values may work, there is no guarantee that they can.) )

The date type is used when you only need a date value, and there is no time section. MySQL retrieves and displays the date value in ' YYYY-MM-DD ' format, supported by ' 1000-01-01 ' to ' 9999-12-31 '.

The TIMESTAMP column type provides a type that you can use to automatically mark an INSERT or update operation with the current date and time.

The time data type represents the times of the day. MySQL retrieves and displays the time value in the "HH:MM:SS" format. The range of support is ' 00:00:00 ' to ' 23:59:59 '.

CREATE TABLE (MYSQL)

 

The difference between datetime and timestamp:
The date range of the 1.datetime is relatively large; If you have 1970 years of data, you still need to use DateTime. But the timestamp occupies a small amount of storage space.
A column of type 2.timestamp has another feature: By default, the timestamp column is automatically populated/updated with the current time (CURRENT_TIMESTAMP) when the INSERT, update data.

The 3.timestamp comparison is affected by time zone timezone and by the MySQL version and SQL mode of the server.

You can specify datetime, date, and timestamp values by using any one of the commonly used format sets:
A string of ' yyyy-mm-dd HH:MM:SS ' or ' yy-mm-dd HH:MM:SS ' format that allows a "loose" syntax: Any punctuation can be used as a delimiter between the date part and the time part. For example, ' 98-12-31 11:30:45 ', ' 98.12.31 11+30+45 ', ' 98/12/31 11*30*45 ' and ' [email protected]@31 11^30^45 ' are equivalent.

Three: MySQL in timestamp type time requires Java to convert time to its database corresponding data format

Date Date=new date ();
SimpleDateFormat temp=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
String Date1=temp.format (date);
Date Date2=temp.parse (date1);

Analysis of various conversion modes of Java date time and MySQL storage time type fields

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.