Converting String to Date (java.sql.date)
Hi,
Kindly let me know how I can convert a string like
"20061102" (YYYYMMDD) to a Date type (java.sql.date) in web Dynpro.
Requirement is to set a DateType context Attribute with
the value contained in the String ("20061102" ).
Awaiting your kind responses..
Regards,
Mahesh
SimpleDateFormat yyyyMMdd_parser = new SimpleDateFormat(
"yyyyMMdd");
java.util.Date date=yyyyMMdd_parser.parse("20061102");
java.sql.Date slqDate=new java.sql.Date(date.getTime());
Similar Messages
-
Convert string into java.sql.Date
Hi,
I want to convert a date in string form into java.sql.Date to persist into the database column with Date as its datatype in Oracle. I'm trying as follows:
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class DateTest
public static void main(String[] args)
String strDate = "2002-07-16 14:45:01";
System.out.println("strDate = "+strDate);
Date aDate = null;
try
if(strDate != null && !strDate.trim().equals(""))
SimpleDateFormat aSDF = new SimpleDateFormat();
aSDF.applyPattern("yyyy-MM-dd hh:mm:ss");
java.util.Date utilDate = aSDF.parse(strDate);
System.out.println("utildate = "+utilDate);
aDate = new Date(utilDate.getTime());
aDate.setTime(utilDate.getTime());
System.out.println("aDate = "+aDate);
catch (ParseException e)
System.out.println("Unable to parse the date - "+strDate);
catch (Exception ex)
ex.printStackTrace();
System.out.println("Caught Exception :"+ex.getMessage());
It gives the output as :
strDate = 2002-07-16 14:45:01
utildate = Tue Jul 16 14:45:01 IST 2002
aDate = 2002-07-16
If I put this value into the database table, I can see only the date. Time part is missing. Is this the problem with java.sql.Date or Oracle datatype Date? Please help me asap.
Thanks in advance.
Regards,
Rajapriya.R.If I put this value into the database table, I can
see only the date. Time part is missing. Is this the
problem with java.sql.Date or Oracle datatype Date?This is not a problem, this is the defined behaviour of the Date type. RTFAPI and have a look at Timestamp, while you're at it. -
Needed help regarding converting string to java.sql.Date format
I have a a function which returns a calendar object. The date must be inserted to Oracle DB using java.sql.Date format.
So i have converted the Calendar object to java.sql.Date format using the following code
java.sql.Date publicationDate = new java.sql.Date(book.getPublicationDate().getTime().getTime());But while getting inserted into the DB it was in mm/dd/yyyy format whereas i wanted dd/mm/yyyy format
Can any body please help out how to store the date in dd/mm/yyyy format ?Can u please explain this a bit
This is my code
public int addBook(List<Book> BookList) throws SQLException, ParseException{
System.out.println("Hi there");
Book book = new Book();
BookDB bookDb = new BookDB();
//listLength = BookList.length;
String bookId = null;
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@10.123.79.195:1521:findb01","e115314", "e115314");
addBook = con.prepareStatement("insert into ABC_Book values(?,?,?,?,?,?,?)");
Iterator<Book> iterator = BookList.iterator();
while(iterator.hasNext()){
book = (Book)iterator.next();
System.out.println(book.getBookId());
addBook.setString(1,book.getBookId());
addBook.setString(2,book.getTitle());
addBook.setString(3,book.getAuthor());
addBook.setString(4,book.getPublisher());
System.out.println(book.getPublicationDate());
System.out.println("Before Date");
System.out.println("book.getPublicationDate().getTime()"+book.getPublicationDate().getTime());
java.sql.Date publicationDate = new java.sql.Date(book.getPublicationDate().getTime().getTime());
SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
dateString = formatter.format(publicationDate);
System.out.println("Today is"+dateString);
java.sql.Date date = (java.sql.Date)formatter.parse(dateString);
System.out.println("date"+date);
//java.sql.Date publicationDate = (Date)book.getPublicationDate().getTime();
//System.out.println("Value of date is"+publicationDate);
System.out.println("After Date");
addBook.setDate(5,publicationDate);
addBook.setString(6,book.getCountry());
addBook.setString(7,book.getLanguage());
rs = addBook.executeQuery();
//con.commit();
rowCount = rowCount + rs.getRow();
return rowCount;
catch(SQLException se){
se.printStackTrace();
finally{
con.close();
System.out.println("After adding ");
return 0;
} -
Convert java.sql.Date to java.util.Date
How do i convert a java.sql.Date to java.util.Date ?
SimpleDateFormat formatter = new SimpleDateFormat ("dd/MM/yy HH:mm");
Date received=myresultset.getDate(1);
String utildate = formatter.format(received);
<%=utildate%>
Result : doesn't keep the HH:mm format
14/03/04 00:00
Ideas ?There is simple way to do this job...
java.util.Date utilDate = new java.util.Date();
java.sql.Data sqlDate = new java.sql.Date(utilDate.getTime())
Enjoy Coding ! ! ! ! ! -
Help converting between Java Dates and SQL Dates
Hey all,
I'm working on a JSP to allow a user to enter information about news articles into a mySQL database. I'm using text fields for most of the data, and most of it is transferred correctly when the user hits "Submit." However, I am having a problem with the date field of the mySQL database. Basically I'm trying to retrieve the current date from the JSP page, and then insert this into the date field of the appropriate table when the user hits submit. The problem is that I can't typecast a java.util.Date or even a java.util.Calendar to a java.sql.Date object. I've tried lots of approaches, including creating a new instance of java.util.Calendar, getting all its fields and concatenating them onto the date variable, but this doesn't work.
Anyone know how I could easily convert between java.util.Date or java.util.Calendar and java.sql.Date?
Thanks!Thanks for the help!
I can correctly display the current date on the page in java.sql.Date format now, but it's still not being inserted into the database correctly. The specific code is as follows:
java.util.Date dt = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(dt.getTime());
(As you wrote)
Then (after connecting to the database etc.):
PreparedStatement pstmt = con.prepareStatement("INSERT INTO NEWS(NEWSDATE,DAYOFWEEK,AUTHOR,HEADLINE,CLIP,PUBLICATION,LINK,NEWSLOCATION,DATECREATED,DATEMODIFIED,CATEGORY,KEYWORDS,PHOTOURL,PHOTOGRAPHER,AUDIOURL) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
pstmt.clearParameters();
pstmt.setString(1,date);
pstmt.setString(2,dayofweek);
pstmt.setString(3,author);
pstmt.setString(4,headline);
pstmt.setString(5,clip);
pstmt.setString(6,publication);
pstmt.setString(7,link);
pstmt.setString(8,newslocation);
pstmt.setDate(9,sqlDate);
pstmt.setString(10,datemodified);
pstmt.setString(11,category);
pstmt.setString(12,keywords);
pstmt.setString(13,photoURL);
pstmt.setString(14,photographer);
pstmt.setString(15,audioURL);
int i = pstmt.executeUpdate();
pstmt.close();
All the other fields are retrieved with request.getParameter from text fields. Any idea why the DATECREATED field is being updated to all 0's but the others work fine?
Thanks again. -
Java.util.Date and java.sql.Date
when i am trying to displaying date in my jsp page errors occurs saying that java.util.date and java.sql.date are matching..
i have some sql statements which i need to execute in the page.so i cant delete that import sql statement.
so what is the solution for this.
i want to display the date as 19th april 2003....in the jsp page.
thank uYou should use full names.
java.util.Date date;
java.sql.Date sdate;
..And it may be a good idea to delete one of the two "import" declaration of packages. -
(Again) java.util.Date vs java.sql.Date
Hi there,
(Again) Im trying to understand the EXACT difference between
java.util.Date vs java.sql.Date.
Googling, I can see that this is a very "popular" subject, but I still
cannot figure out it exactly.
Many writers claim that java.sql.Date only stores the DATE part (yyyy-
mm-dd) but not the TIME part (hh:MM:ss) of a Date/Time value, but that
I can easily disprove:
java.util.Date ud = new java.util.Date(); java.sql.Date sd = new java.sql.Date(ud.getTime()); System.out.println(DateFormatUtils. format(ud, "yyyy-MM-dd HH:mm:ss.SSS")); System.out.println(DateFormatUtils. format(sd, "yyyy-MM-dd HH:mm:ss.SSS"));
Output:
2009-09-18 15:17:36.635 2009-09-18 15:17:36.635
So, apparently, java.sql.Date and java.util.Date have THE SAME
precision (at least down to the millisecs...).
And the official API documentation, really looks more confusing than
helpful to me::
*"java.sql.Date:*
*A thin wrapper around a millisecond value that allows JDBC to identify*
*this as an SQL DATE value. A milliseconds value represents the*
*number of milliseconds that have passed since January 1, 1970*
*00:00:00.000 GMT.*
*To conform with the definition of SQL DATE, the millisecond values*
*wrapped by a java.sql.Date instance must be 'normalized' by setting*
*the hours, minutes, seconds, and milliseconds to zero in the*
*particular time zone with which the instance is associated. "*
Exactly what means "an SQL DATE value" ? How EXACTLY does it differ
from a java.util.Date value?
Most importantly: WHY does JDBC need to distinguish between them?
And, here again: *"a java.sql.Date instance must be 'normalized' by*
*setting the hours, minutes, seconds, and milliseconds to zero in the*
*particular time zone..."*
What does that mean exactly? Apparently, the constructor doesnt
enforce this restriction, per the example above. So what's the REAL
point with this type, java.sql.Date?
Very greatful, if you can help me clarify this, once and for all.
TIA,And the official API documentation, really looks more confusing than helpful to me:The problem is that you need to understand SQL as well as Java for this to make sense. It's not the Java API's job to tell you how your SQL database works - there's a myriad of subtle differences even when the DB is compliant with the SQL spec.
Most compliant databases support DATE, TIME, and TIMESTAMP values. DATE represents only a date. TIME represents only a time. TIMESTAMP represents both. There are further complicating factors, but that's roughly how it stands.
In Java the normal type for representing time is (or was when the API was created) the java.util.Date but this is a close approximation only to the TIMESTAMP value. In order to bring the two together the java.sql.Date, java.sql.Time and java.sql.Timestamp classes were created. Making them derive from java.util.Date was probably not a good idea.
java.util.Date suffers from a number of deficiencies. java.util.Calendar was supposed to address them but didn't really succeed. The JodaTime library is rather better, but it's all a lot more complicated than you might expect - partly because time management really is a much harder problem than it appears at first glance - there are timezones, leap years, leap seconds, the difference between astronomical and atomic time, and so on and so forth. -
PreparedStatement.setDate(1,java.sql.Date x) problem
I am using Sun JDBC-ODBC bridge to access MS SQL Server 2000. I have following statement:
java.util.Date date = new java.util.Date();
java.sql.Date expire_date = new java.sql.Date(date.getTime());
PreparedStatement pstat = con.prepareStatement("update account set expire_date=? where userid=?");
pstat.setDate(1,expire_date);
pstat.setString(2,userid);
When I ran the program, I got a SQLException error as
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Optional features not implemented.
I have traced the problem happened in the statement pstat.setDate(1,expire_date). I use jdbc-odbc bridge from j2se 1.3.1.
I appreciate any help.
Thanks, BrianMay I refer to a recent topic where I explained a lot about date conversion between JDBC and SQLServer?
http://forum.java.sun.com/thread.jsp?forum=48&thread=241049
Try how far this helps you, then ask more. -
Trying to convert String to java.sql.Date
I need to convert a String (in the format "yyyy-mm-dd") to java.sql.Date
It was suggested I use the following,
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");
Date result = formater.parse(dbirth.getText());
However, It seem to produce a java.util.Date
Error: found java.util.Date
Required : java.sql.Date
Can anyone help?
Thanks, MarikaI need to convert a String (in the format
"yyyy-mm-dd") to java.sql.Date
It was suggested I use the following,
SimpleDateFormat formater = new
SimpleDateFormat("yyyy-mm-dd");
Date result = formater.parse(dbirth.getText());
However, It seem to produce a java.util.Date
Error: found java.util.Date
Required : java.sql.Date
Can anyone help?
Thanks, Marika SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date parsedDate = formater.parse(dbirth.getText());
java.sql.Date result = new java.sql.Date(parsedDate.getTime()); -
How to convert String date to java sql date
I have an html form for entering the Date on retreving that value in a java servlet i get it as a string i want to convert that date to SQL date format so that i could use setDate() method of the java.sql.Date class to update the date value in the database.
i used the following way but i need the code without using the deprecated ones.
String delDate = (String)p_Request.getParameter("deleteDate");
[b]java.util.Date utilDate = new java.util.Date(delDate);
java.sql.Date deleteDate = new java.sql.Date(utilDate.getTime());
custCode = (String)p_Request.getValue("custCode");
thanx in advance.Check out the later posts in http://forum.java.sun.com/thread.jspa?threadID=647681&messageID=3814745#3814745
-
Convert a String to java.sql.Date Format
Hi,
I am having a String of containing date in the format 'dd/mm/yyyy' OR 'dd-MMM-YYYY' OR 'mm-dd-yyyy' format. I need to convert the string to java.sql.Date object so that I can perform a query the database for the date field. Can any one suggest me with the code please.
Regards,
Smithaimport java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class TestDateFormat
public static void main(String args[])
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.isLenient());
try
Date d1 = sdf.parse("07-11-2001");
System.out.println(d1);
Date d2 = sdf.parse("07:11:2001");
System.out.println(d2);
catch(ParseException e)
System.out.println("Error format, " + e);
See class DateFormat and SimpleDateFormat for detail. -
Hi,
I am new to this Technology.
in my Project DataBase I have the date field as String in format yyyy-mm-dd. when I am trying to fetch this date through the front-end I need to see the date in dd/mm/yyyy format of type java.sql.Date Variable.
if you know, Pl help me.
Thanks in Advance,
Kamala.Then use a SimpleDateFormat object to parse it into a Date object, and also to format the Date the way you want to see it, rather than simply relying on the toString() implementation.
-
How to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
i am using informix database which accepts date value in the form of DATE format......
the other part of my application takes date from the field in DD/MM/YYYY format...so i have to convert my java.sql.date in YYYY/MM/DD fromat into DD/MM/YYYY fromat of same type before inserting into db......
but using parse method in SimpleDateFormat class can get the result only in java.util.date format...
and also using format method can result only in string conversion........816399 wrote:
i am using informix database which accepts date value in the form of DATE format......Huh?
Maybe you mean Informix (fronted by JDBC) expects date values as java.sql.Date objects?
the other part of my application takes date from the field in DD/MM/YYYY format...
so i have to convert my java.sql.date in YYYY/MM/DD format into DD/MM/YYYY format of same type before inserting into db......I am not sure why you are talking about formats here.
There is no formatting inherent in a java.util.Date object
nor in a java.sql.Date object.
but using parse method in SimpleDateFormat class can get the result only in java.util.date format...
and also using format method can result only in string conversion........You can easily create a java.sql.Date object from a java.util.Date object.
String s = "31/12/2010";
java.util.Date ud = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(s);
java.sql.Date sd = new java.sql.Date(ud.getTime());
ud = sd; // java.sql.Date extends java.util.Date so no conversion is needed -
Convert java.sql.Date to oracle.jbo.domain.Date in oaf
Hi,
Need to dafault NeedBydate(i get in from query as below) to a messagelovinput and make it read only.
i am facing some issue in converting from java.sql.Date to oracle.jbo.domain.Date.
unable to figure out the problem.
below is the sample code im trying to use.
PreparedStatement ps=pageContext.createPreparedStatement("select sysdate from dual");
ResultSet rs=ps.executeQuery();
if (rs!=null && ps.next()
java.sql.Date sqlDate= rs.getDate(1);
oracle.jbo.domain.Date oracleDate = new oracle.jbo.domain.Date(sqlDate.getTime());
vorow.setAttribute("Datevalue",oracleDate);
this is my assumption : to set to UI i need date in format dd-Mon-yyyy.(am i correct? as to get that format i tried to use simple date format class but no use)
when i tried to use simple date format :unpaseable date exception was raised.
please guide.Hi,
Why are you using this code???
PreparedStatement ps=pageContext.createPreparedStatement("select sysdate from dual");
ResultSet rs=ps.executeQuery();
if (rs!=null && ps.next()
java.sql.Date sqlDate= rs.getDate(1);
oracle.jbo.domain.Date oracleDate = new oracle.jbo.domain.Date(sqlDate.getTime());
you can get sysdate using this and then set it
oracle.jbo.domain.Date currentDate = am.getOADBTransaction().getCurrentUserDate();
java.text.SimpleDateFormat displayDateFormat = new java.text.SimpleDateFormat ("yyyy-MM-dd");
String DateForm = displayDateFormat.format(currentDate.dateValue());
oracle.jbo.domain.Date dateset = new oracle.jbo.domain.Date(DateForm);
and then set the attribute
vorow.setAttribute("Datevalue",dateset);
Thanks,
Gaurav -
Cannot convert from java.util.Date to java.sql.Date
In the below code am trying to get the current date and 60 days prior date:
Date todayDate;
Date Sixtydaysprior;
String DATE_FORMAT = "MM/dd/yy";
DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Calendar cal = Calendar.getInstance();
todayDate = sdf.parse(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, -60);
Sixtydaysprior = sdf.parse(sdf.format(cal.getTime()));I have imported following files:
<%@page
import="java.util.Calendar,
java.text.SimpleDateFormat,
java.text.ParseException,
java.util.*"
%>Shows up following error msg:
Type mismatch: cannot convert from java.util.Date to java.sql.Date
Thanks.
Edited by: MiltonDetroja on May 22, 2009 11:03 AMShows up following error msg:
Type mismatch: cannot convert from java.util.Date to java.sql.Date
I don't think this exception is thrown from the portion of code you have shown. As clearly specified in exception message, you cannot cast an instance of java.util.Date to java.sql.Date. you will need to do something like this
java.util.Date today = new java.util.Date();
long t = today.getTime();
java.sql.Date dt = new java.sql.Date(t);
Maybe you are looking for
-
Error deploying page from 4.0 application to 4.1
Hi: I have successfully deployed a page from a 4.0 apex application to the same application in apex 4.1. The page is imported successfully. However, when I run the page, I get the following error: Can't display page item because the HTML form was not
-
The Regency International Hotel has two categories of rooms - Economy rooms go for Rs. 3500/- a day - Ocean view rooms go for Rs 5000/- a day. In addition, it has a bar in which it sells Water, Soda, Beer and Wine. Tax is calculated as follow
-
What are best practices for rolling out cluster upgrade?
Hello, I am looking for your input on the approaches to implement Production RAC upgrade without having a spare instance of RAC servers. We have a 2-node database RAC 11.1 on Production. We are planning to upgrade to 11.2 but only have a single datab
-
The information in the question window is pretty complete. I did go in and check my latest version of Java and it is Java 6 Update 21, installed in July of this year.
-
Hi Experts, I am executing a procedure in the package and printing values but i am getting an error below execution ======= variable o_status number; variable o_ename1 varchar2; DECLARE i_internal_id NUMBER (4) := 7839; o_ename