Java gets the current system time for the timestamp type

Source: Internet
Author: User

Java gets the current system time to get the timestamp type
Java gets the current system time format for getting the timestamp type: 2010-11-04 16:19:42

Method 1:

New

Method 2:

New Date ();        New Timestamp (Date.gettime ());

-----------------------------
Integration of relevant content
Date and timestamp time-related content in Java Blog Category: Java programming
JavaSQLDAO.java.util.date java.sql.date Java.sql.timestamp

Finishing One:

Here is a piece of article, I personally think the explanation is very detailed, there is the use of java.sql.Date and confused please see.

Java.sql.Date only store date data does not store time data

//time data is lostPreparedstatement.setdate (1,Newjava.sql.Date (Date.gettime ())); //you can handle this.Preparedstatement.settimestamp (1,NewJava.sql.Timestamp (Newjava.util.Date (). GetTime ()); //to get the complete data, including the date and time, you canjava.util.Date D= Resultset.gettimestamp (1); //This is a more appropriate process to avoid some potential timestamp problemsjava.util.Date D=NewJava.util.Date (Resultset.gettimestamp (1). GetTime ());

If you fill it out

In this case:

You can receive the Java.util.Date type when you store the database and then use the GetTime () method to get a long value representing that date object, and then construct a timestamp object into the database with the Long value.

From the storage database, you can get timestamp with his gettime () method to get a long value, and then the long value to construct a Java.util.Date object, so that the date object can be manipulated. Better say new Simpletimeformat ("Yyyyy-mm-dd HH:mm:ss"). Format (), etc.



Finishing two:

It is convenient to use timestamp to record datetime, but sometimes it is not necessary to display the milliseconds after the decimal point, so you need to redefine the format when converting to string.

Timestamp converted to String:simpledateformat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//define format, do not display milliseconds Timestamp now = new Ti         Mestamp (System.currenttimemillis ());//Gets the system current time String str = Df.format (now); The string is converted to Timestamp:simpledateformat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String time = Df.format (new Date ());     Timestamp ts = timestamp.valueof (time); Finishing three: In resultset we often use the setdate or getdate data type is java.sql.Date, and in peacetime Java program we generally used to use java.util.Date. Therefore in the DAO layer we often encounter these two kinds of data types of mutual conversion, after an afternoon of tossing, I am the two of the conversion method made a small summary, I hope everyone feel free.
The relationship between the two
Java.lang.Object
|
+---Java.util.Date
|
+----Java.sql.Date
From this figure we can see that java.sql.Date is inherited from Java.util.Date.

Convert each other
1. Using the gettime () function
These two classes all provide the gettime () function, which returns the corresponding number of milliseconds (long type). This function can be used to implement transformations:

    new Java.util.Date (Sqldate.gettime ());   // SQL-Util    new Java.sql.Date (Utildate.gettime ());   // util-SQL

2. Implementing conversions using the SimpleDateFormat class
SimpleDateFormat is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-and-text), parsing (text, date), and normalization.

New Simpledateformate ("Yyyy-mm-dd HH:mm:ss"= Dateformat.parse (sqldate.tostring ());

3. Direct conversion
Since Java.sql.Date is inherited from the java.util.Date, it can be used directly:

Utildate = sqldate;

4. Alternative method of acquiring dates:

SimpleDateFormat sy=New SimpleDateFormat ("yyyy"); SimpleDateFormat sm=new simpledateformat ("MM"); SimpleDateFormat SD=new SimpleDateFormat ("DD"); String syear=Sy.format (date); String Smon=Sm.format (date); String sday=sd.format (date);

Ps:java.util.Date class getyear () to add 1900 to get the actual value, GetMonth () to add 1

Java gets the current system time for the timestamp type

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.