Oracle database contact and conversion of date type to date in Java

Source: Internet
Author: User
Tags string format

Here is a description of the difference between the date object in Java and the date in Oracle in order to help you.
New Date (): Allocates a Date object and initializes the object to indicate the time it was allocated (in milliseconds), which is the current system.
New Date: Allocates a Date object and initializes the object to represent the standard time since the
The specified number of milliseconds (called "epoch", or January 1, 1970 00:00:00 GMT).
Long GetTime (): Returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT.
String toString (): Converts this Date object to the following form
String:dow mon dd hh:mm:ss zzz yyyy
Where: Dow is a day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).

*****************************************
Static Calendar getinstance (): Gets a calendar using the default time zone and locale.
Date GetTime () returns a Date object that represents this calendar time value, from the epoch to the current millisecond offset.
Long Gettimeinmillis (): Returns the time value of this Calendar, in milliseconds.
void SetTime (date date): Sets the time of this Calendar with the given date.
void Settimeinmillis (Long Millis): Sets the current time value for this Calendar with a given long value.


**** *************************************
New Simpledateformate ("Yyyy-mm-dd hh:mm:ss"). Format (new Date ()); Return String Class object


Summarize:
1. Get the current time of the system
Get long type: System.currenttimemillis ()
Get data type: New data ();
Get the Calendar type:
Calendar C = calendar.getinstance ();
C.settime (System.currenttimemillis ();
Usually after the calendar, you can work on each field;
2, the conversion between the various types of time:
Calendar to Date:c.gettime () returns a Date object
Date to Calendar:c.settime (): Returns a Calendar object
3. Storage objects for time in the database
(1) The data type that represents the date and time, the value stored in the data field contains the "component" corresponding to the century, year, month, day, hour, minute, and second, which is displayed and manipulated in different ways, effectively independent of any tower string format
(2) available from Oracle 9i the timestamp type is a valid extension of the date type, providing greater time accuracy, support for nine-bit children, and the ability to store time zone information
(3) Starting with the Oracle 9i interval type, which supports the storage time difference and can be used with date or timestamp addition operations to generate a new date or timestamp

Use Sysdate to select the current system time, return a value of type date, the current date and time of the operating system where the database resides
Internal representation of the date type: Executes select DUMP (sysdate) from dual;
Typ=13 the comma-separated values in the len=8:218,7,3,23,11,27,11,0 results correspond to the bytes used by Oracle to store each part of the date and time, (from century to seconds)
4. Conversion between a Date object and a string
To_dat () and To_char (): Both functions have three parameters, one is the value to convert, one is an optional format mask, and an optional string for the specified language
To
Select To_char (sysdate, ' Yyyy-mm-dd HH24:MI:SS ') from dual;
Select To_date (' 20100323 ', ' YYYYMMDD ') from dual;
Select Ename, To_char (hiredate, ' ddthmon,yyyy ') from EMP
where HireDate between
To_date (' 1980-01-01 ', ' yyyy-mm-dd ')
and
To_date (' 1985-01-01 ', ' yyyy-mm-dd ')
5. Common operation
To store the program's data in a database:
When the program is a string type, the SQL text should use the
To_date (HireDate, ' Yyyy-mm-dd hh24:mi:ss ') is placed in the database as a date type
When you have a date type in your program, you must first convert to a string type, using the
New Simpledateformate ("Yyyy-mm-dd hh:mm:ss"). Format (new Date ()) conversion
When the data in the database is taken out
To use To_char (HireDate, ' yyyy-mm-dd HH24:MI:SS ') into a string type,
You can also continue to use JAVA.SQL.DATE.VALUEOF (String s)
Converts a string of the JDBC date escape form to a date value.
6. Get Oracle database time in Java
Obtained from database date and converted to Java.util.Date type
Method One: Select To_char (sysdate, ' Yyyy-mm-dd HH24:MI:SS ') as Current_time from dual get string
DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Format.parse (Map.get ("Current_time")); the type of date obtained after parsing: Sat Jul 13:32:46 CST 2010
Format.format (datestring), and then format the desired format string
Format.format (datestring); Convert the date type to the desired format. But the type is string. Use the parse () method when you want to change back.
Method One: Select (Sysdate-8/24-to_date (' 1970-01-01 ', ' yyyy-mm-dd ')) * 86400000 Current_milli from dual get JAVA.MATH.BIGDEC iMAL type
BigDecimal B = (BigDecimal) datemap.get ("Current_milli");
Long Datelong = B.longvalue ();
New Date (Datelong); Gets a date object that can handle

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.