Java inserts the current time into the Oracle database

Source: Internet
Author: User
Tags date now dateformat time and date

public class test{
public static void Main (String args []) {
Java.util.Date a = new java.util.Date ();
System.out.println (a);
Java.sql.Date B = New Java.sql.Date (A.gettime ());
System.out.println (b);
Java.sql.Time C = new Java.sql.Time (A.gettime ());
System.out.println (c);
Java.sql.Timestamp d=new Java.sql.Timestamp (A.gettime ());
System.out.println (d);
}
}

Mon APR 18:00:34 CST 2006
2006-04-03
18:00:34
2006-04-03 18:00:34.388

1. Oracle default system time is the Sysdate function, the stored data shape such as 25-3-200510:55:33
2. The time-fetching object in Java is java.util.Date.
3. The corresponding time objects in Oracle are Java.util.date,java.sql.time,java.sql.timestamp, and they are all java.util.Date subclasses.
4. The most significant relationship between Oracle and date operations is two conversion functions: To_date (), To_char (). To_date () is typically used to write dates to a database using a function. To_char () is typically used to read from a database into the day

The function that is used during the period.

DATE, Time, and TIMESTAMP:
SQL defines three time-related data types: date is made up of day, month, and year. Time is made up of hours, minutes, and seconds. TIMESTAMP combines DATE with time and adds the nanosecond domain.
The standard Java class Java.util.Date can provide date and time information. However, because the class contains DATE and time information and does not have the nanosecond required for TIMESTAMP, it does not exactly match the above three types of SQL.
So we have defined the three seed classes of java.util.Date. They are:
1. Java.sql.Date for SQL DATE information
2. Java.sql.Time for SQL time information
3. Java.sql.Timestamp about SQL TIMESTAMP information
For the Java.sql.time,java.util.time base class, the hour, minute, second, and millisecond fields are set to zero. For the Java.sql.date,java.util.date base class, the year, month, and day fields are set to 1970 years, respectively, 1

1st of the month. This is the "0" date in the Java era. The dates in java.sql.date can be compared to fields with dates in standard SQL statements. Java.sql.Timestamp class expands by adding a nanosecond domain

Java.util.Date.

Two conversion functions in Oracle:
1. The to_date () function converts a character type to a date type in a certain format:
Specific usage: to_date (' 2004-11-27 ', ' yyyy-mm-dd '), the former is a string, the latter is a conversion date format, note that before and after the two to a corresponding. such as; To_date (' 2004-11-27 13:34:43 ', ' yyyy-mm-dd

Hh24:mi:ss ") will be given a specific time.
2. To_char (): convert date to a certain format for character type:
Specific usage: to_char (sysdate, "Yyyy-mm-dd hh24:mi:ss")

To_date () with 24-hour notation and MM-minute display:
When using Oracle's To_date function for date conversion, many Java programmers may directly use the format "Yyyy-mm-dd HH:mm:ss" as a format for conversion, but will cause an error in Oracle: "ORA 01810

The format code appears two times.
such as: Select To_date (' 2005-01-01 13:14:20 ', ' yyyy-mm-dd HH24:mm:ss ') from dual;
The reason is that SQL is case-insensitive, mm and mm are considered the same format code, so Oracle's SQL uses MI instead of minutes. Oracle's default system time is the Sysdate function, and the stored data is shaped like 2005-3-2510:55:33

, the time-fetching object in Java is java.util.Date.
Select To_date (' 2005-01-01 13:14:20 ', ' yyyy-mm-dd HH24:mi:ss ') from dual

Examples of operations on a date field in Java Operations with Oracle:
Table book has name VARCHAR2 (20)//books name, Buydate date//Purchase date two fields.
A database connection has been created connection conn;

Method one, using java.sql.Date to achieve a relatively simple YYYY-MM-DD format date. Java.sql.Date does not support the time format. Remember not to use the new java.sql.Date (int year,int month,int Date) because you also want to place

Time lag problem.
PreparedStatement pstmt = conn.preparestatement ("INSERT into book (name,buydate) VALUES (?,?)");
Java.sql.Date buydate=java.sql.date.valueof ("2005-06-08");
Pstmt.setstring (1, "Java Programming ideas");
Pstmt.setdate (2,buydate);
Pstmt.execute ();
Method Two, use Java.sql.Timestamp, ditto do not use new Timestamp (...)
PreparedStatement pstmt = conn.preparestatement ("INSERT into book (name,buydate) VALUES (?,?)");
Java.sql.Timestamp buydate=java.sql.timestamp.valueof ("2004-06-08 05:33:99");
Pstmt.setstring (1, "Java Programming ideas");
Pstmt.settimestamp (2,buydate);
Pstmt.execute ();
Method three, using Oracle's to_date built-in functions
PreparedStatement pstmt = conn.preparestatement ("INSERT into book (name,buydate) VALUES (?, To_date (?, ' Yyyy-mm-dd hh24: Mi:ss ') ");
String buydate= "2004-06-08 05:33:99";
Pstmt.setstring (1, "Java Programming ideas");
Pstmt.setstring (2,buydate);
Pstmt.execute ();
Attached: Oracle date format parameter meaning description
D: The week of the week
Day: The name of the days, padded with spaces to 9 characters
DD: The day ordinal of the month
DDD: The day ordinal of the year
DY: Abbreviated name of the day
Iw:iso the first week of the year in the standard
Iyyy:iso standard four-bit year
YYYY: four-bit year
Yyy,yy,y: Last three digits of the year, two digits, one
HH: Hours, by 12 hours
Hh24: Hours, by 24 hours
MI: Min
SS: Seconds
MM: Month
Mon: Shorthand for the month
Month: Full name of the month
W: The first few weeks of the month
WW: The first few weeks of the year


=====================
sql111= SELECT * from logincount where logtime = to_date (' 2009-11-30 ', ' yyyy-mm-dd ')
st = Con.preparestatement ("select * from logincount where logtime = To_date ('" + New Java.sql.Date (Date.gettime ()) + "', ' yyy Y-mm-dd ') ");

method to get the current time:

Ways to get the current time in Java
There are two ways of doing this:

Method one: With the Java.util.Date class to achieve, and combined with the Java.text.DateFormat class to achieve the format of time, see the following code:

Import java.util.*;
Import java.text.*;
The following default time and date display methods are in Chinese language mode
General language is the default Chinese can be, the format of the time date is medium style, for example: 2008-6-16 20:54:53
The dates and times shown below are based on the date class and can also be implemented using the Calendar class to see the class Testdate2.java
public class TestDate {
public static void Main (string[] args) {
Date now = new Date ();
Calendar cal = Calendar.getinstance ();

DateFormat D1 = dateformat.getdateinstance (); Default language (medium style, for example: 2008-6-16 20:54:53)
String str1 = D1.format (now);
DateFormat D2 = Dateformat.getdatetimeinstance ();
String str2 = D2.format (now);
DateFormat d3 = Dateformat.gettimeinstance ();
String STR3 = D3.format (now);
DateFormat D4 = Dateformat.getinstance (); Display date and time using short style
String STR4 = D4.format (now);

DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full,dateformat.full); Display date, week, time (accurate to seconds)
String STR5 = D5.format (now);
DateFormat d6 = dateformat.getdatetimeinstance (Dateformat.long,dateformat.long); Displays the date. Time (accurate to seconds)
String STR6 = D6.format (now);
DateFormat D7 = dateformat.getdatetimeinstance (Dateformat.short,dateformat.short); Display date, time (accurate to minute)
String STR7 = D7.format (now);
DateFormat D8 = Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium); Display date, time (accurate to minute)
String str8 = D8.format (now);//compared with short style, this method is the most useful


System.out.println ("Show Time in Date:" + now);//The result shown by this method is the same as Calendar.getinstance (). GetTime ().


SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdateinstance () after the time:" + str1);





2 ways to get the current time in Java
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdatetimeinstance () after the time:" + str2);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.gettimeinstance () after the time:" + STR3);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getinstance () after the time:" + STR4);

SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (dateformat.full,dateformat.full):" + STR5);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long):" + STR6);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short):" + STR7);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium):" + str8);
}

}

Operation Result:

Display time in Date: Mon June 20:54:53 CST 2008
Format time with Dateformat.getdateinstance (): 2008-6-16
Format time with Dateformat.getdatetimeinstance (): 2008-6-16 20:54:53
After formatting the time with Dateformat.gettimeinstance (): 20:54:53
After formatting the time with Dateformat.getinstance (): 08-6-16 8:54
After the time is formatted with dateformat.getdatetimeinstance (Dateformat.full,dateformat.full)
: June 16, 2008 Monday 08:54 P.M. 53 sec CST
After the time is formatted with dateformat.getdatetimeinstance (Dateformat.long,dateformat.long)
: June 16, 2008 08:54 P.M. 53 seconds
After you format the time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short)
is: 08-6-16 8:54
Format time with dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium)
After: 2008-6-16-20:54:53


Method Two: Use the Java.util.Calendar class to achieve, see below:

Import java.util.*;
Import java.text.*;
The following is the use of the Calendar class to implement date time, and the date class is relatively simple

public class TestDate2 {
public static void Main (string[] args) {

Calendar CA = Calendar.getinstance ();
int year = Ca.get (calendar.year);//Get Years
int Month=ca.get (calendar.month);//Get month
int Day=ca.get (calendar.date);//Acquisition Day
int Minute=ca.get (calendar.minute);//min
int Hour=ca.get (calendar.hour);//hour
int Second=ca.get (calendar.second);//sec
int weekofyear = Ca.get (Calendar.day_of_week);


System.out.println ("with Calendar.getinstance (). GetTime () mode display time:" + ca.gettime ());
System.out.println ("Get Date with calendar:" + year + "years" + month + "Month" + Day + "Days");

SYSTEM.OUT.PRINTLN ("Use calendar to get time is:" + hour + "when" + Minute + "min" + second + "seconds");
System.out.println (weekofyear);//show today is the day of the week (I do this example is exactly Tuesday, so the results show 2, if you run again 6 weeks, then show 6)

}

}
The operating result is:
Display time by Calendar.getinstance (). GetTime (): Mon June 21:54:21 CST 2008
Date obtained with calendar is: May 16, 2008
Use calendar to get the time: 9:54 21 seconds


Original author unknown, from the network

Java inserts the current time into the Oracle database

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.