Parsing the MySQL and Java time types _php tutorial

Source: Internet
Author: User
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

to analyze It
Refer to MySQL reference manual
Date:
A date. The supported range is ' 1000-01-01 ' to ' 9999-12-31 '. MySQL displays date values in ' YYYY-MM-DD ' format, and allows you to assign values to DATE columns using either strings or Numbers.
Only date information is recorded, representing the range from 1000-01-01 to 9999-12-31.
MYSQL displays this type of field in a yyyy-mm-dd manner. Add the Class field data, that is, you can use a string type, or you can use a numeric type

Because a field of date type only records date information, the time information is automatically truncated if the added data contains time information.
If you want to save time information, you can consider using the datetime type.
After testing, the following 2 ways to populate a Date Type field are found:
by string:
Insert into time_table (createdate) VALUES (' 2007-04-09 ')
by Number:
Insert into time_table (createdate) VALUES (20070409)
get can be obtained with java.sql.Date type
The code is:
Date dtdate =rsbuffer.getdate ("CreateDate");
The test code is as follows: (where Idbface is itself a simple class based on the JDBC wrapper that accepts SQL to manipulate the database)
Copy CodeThe code is as follows:
public void TestDate () throws SQLException
{
Idbface dbface =dbfactory.createmysqlface ();
Dbface.connect ();
Clear table
String strdelete = "Delete from time_table";
Dbface.update (Strdelete);
Add to

String Strinsert = "INSERT into time_table (createdate) VALUES (20070409)";
Dbface.update (Strinsert);


Get
String strselect = "SELECT * from time_table";
ResultSet rsbuffer =dbface.select (strselect);
while (Rsbuffer.next ())
{
Date dtdate =rsbuffer.getdate ("CreateDate");
System.out.println (Dtdate.tostring ());
}
Dbface.close ();
}

Execution Result: 2007-04-09

DateTime
A date and time combination. The supported range is ' 1000-01-01 00:00:00 ' to ' 9999-12-31 23:59:59 '. MySQL displays datetime values in ' yyyy-mm-dd-HH:MM:SS ' format, but allows-to-assign values to datetime columns using either strings or numbers.
The main difference between datetime and date is that DateTime can record date and time information. Date information is only recorded. The range is: 1000-01-01 00:00:00 to 9999-12-31 23:59:59 MySQL formats the data according to YYYY-MM-DD HH:MM:SS, allowing it to be submitted in string and numeric form.

For example, to submit a digital way:
Insert into time_table (createdate) VALUES (20070409132013)
Gets the type of data that can be used: Java.sql.Timestamp type
The code is as follows:
Copy CodeThe code is as follows:
public void Testdatetime () throws SQLException
{
Idbface dbface =dbfactory.createmysqlface ();
Dbface.connect ();
Clear table
String strdelete = "Delete from time_table";
Dbface.update (Strdelete);
Add to

String Strinsert = "INSERT into time_table (createdatetime) VALUES (20070409132013)";
Dbface.update (Strinsert);
Get
String strselect = "SELECT * from time_table";
ResultSet rsbuffer =dbface.select (strselect);
while (Rsbuffer.next ())
{
Timestamp tsbuffer =rsbuffer.gettimestamp ("Createdatetime");
System.out.println (Tsbuffer.tostring ());
}
Dbface.close ();
}

Execution Result: 2007-04-09 13:20:13.0
TimeStamp
A timestamp. The range is ' 1970-01-01 00:00:00 ' to partway through the year 2037. A TIMESTAMP column is useful for recording, the date and time of an INSERT or UPDATE operation. The first TIMESTAMP column in a table was automatically set to the date and time of the most recent operation if you don ' t Assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.
Very similar to datetime types
Range of 1970-01-01–2037 years with an accuracy of 1 seconds/
If you do not assign a value to a column of type timestamp in SQL, the column will be framed to cause the current time.
Submitting a null value also causes the column to be entered at the current time.
If the time is submitted incorrectly, the column will be filled in 0.
Timestamp requires less storage space than a datetime type, requiring only 4 bytes, and a datetime requires 8 bytes.
But there is one thing that needs special attention. Timestamp can only represent a time range of 1970-2037.
Use timestamp it is important to ensure that the time data submitted must not exceed this range.
The code and the DateTime class are, and I don't like to use it, so I dropped it.
Time :
A time. The range is ' -838:59:59 ' to ' 838:59:59 '. MySQL displays time values in ' HH:MM:SS ' format, and allows you to assign values to time columns using either strings or N Umbers.

time information is only recorded, not date information.
The range is -838:59:59 to 838:59:59, and MYSQL formats the data in HH:MM:SS, allowing input as a string or as a number.
Code:
Copy CodeThe code is as follows:
public void Testtime () throws SQLException
{
Idbface dbface =dbfactory.createmysqlface ();
Dbface.connect ();
Clear table
String strdelete = "Delete from time_table";
Dbface.update (Strdelete);
Add to

String Strinsert = "INSERT into time_table (createtime) VALUES (131211)";
Dbface.update (Strinsert);
Get
String strselect = "SELECT * from time_table";
ResultSet rsbuffer =dbface.select (strselect);
while (Rsbuffer.next ())
{
Time Tmbuffer =rsbuffer.gettime ("Createtime");
System.out.println (Tmbuffer.tostring ());
}
Dbface.close ();
}

Execution Result: 13:12:11
Year
A year in Two-digit or four-digit format. The default is Four-digit format. In four-digit format, the allowable values is 1901 to 2155, and 0000. In two-digit format, the allowable values is representing years from 1970 to 2069. MySQL displays year values in YYYY format, but allows you to assign values to year columns using either strings or numbers . The year type was unavailable prior to MySQL 3.22.

Year can have 2 representations, 4-bit and 2-bit.
The default is 4 bits. Its range is 1901-2155
The 2-bit notation only records the latter 2 digits. Its range is 1970-2069
It is allowed to be inserted as a string or as a number.
Code:
Copy CodeThe code is as follows:
public void Testyear () throws SQLException
{
Idbface dbface =dbfactory.createmysqlface ();
Dbface.connect ();
Clear table
String strdelete = "Delete from time_table";
Dbface.update (Strdelete);
Add to

String Strinsert = "INSERT into time_table (createyear) VALUES (2007)";
Dbface.update (Strinsert);
Get
String strselect = "SELECT * from time_table";
ResultSet rsbuffer =dbface.select (strselect);
while (Rsbuffer.next ())
{
Date dtbuffer =rsbuffer.getdate ("Createyear");
System.out.println (Dtbuffer.getyear () +1900);
}
Dbface.close ();
}

Execution Result: 2007
It should be stated that:
The Date.getyear () method returns how many years have elapsed since 1900. So in order to show the correct time, you must add 1900.
The method has been deprecated.

in addition.
One way to do this is to not use any of these types to record time.
Instead, the time is recorded in a char (or vchar) manner.
This is convenient when inserting data and displaying records without any conversion.
But there are 2 important flaws to bear.
(1) To independently develop the method to verify the legality of time data. For example, the AJIDJIEOA string is not a time information, but can still be inserted normally.
(2) If the system needs to make the time range as a condition for the record retrieval. This will also be a * annoying. Logging time with a string will not be able to use the API provided by MySQL for time. The code retrieved for the time range may be stripped from the database. This will inevitably have an impact on performance. For example, to query from 1 million data for a mere 100 data of 1992-3-12–1992-3-13 days, you might have to isolate 1 million data and then develop new ways to filter it.

In addition, MySQL to 4.1 time accuracy appearance if only to seconds.
To record finer granularity of time. You can consider constructing DateTime.
Record Datetime.trick ().
It is just an idea that there is no additional question yet to prove. /

http://www.bkjia.com/PHPjc/327755.html www.bkjia.com true http://www.bkjia.com/PHPjc/327755.html techarticle The time type of MySQL has a time type corresponding to it in Java date java.sql.Date Datetime java.sql.Timestamp Timestamp java.sql.Timestamp Times Java.sql.Time year java.sql.Date to it into ...

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