• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

date problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. i need help in converting string to date.. i just need someone to check whether my code is correct or not..

here are my codes:


Date today = new Date();
DateFormat formatLong= DateFormat.getDateInstance(DateFormat.SHORT);
java.sql.Date curDate = java.sql.Date.valueOf(formatLong.format(today));

//and below is the storing part..

String query1 = null;
query1 = "Insert into ITS.dbo.orderDetails values(?)";

PreparedStatement stmnt = conn.prepareStatement(query1);


stmnt.setDate(1, curDate);

stmnt.executeUpdate();





anyone ..pls reply .. i need it urgently...
[ March 11, 2005: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the SimpleDateFormat class:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

You can then format the date string much easier
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java in General (intermediate)
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this snippet...

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your code looks fine. The main difference between using DateFormat.getInstance() and creating your own SimpleDateFormat is that the former provides a default format based on your systems settings (usually called the "locale"). The later, on the other hand, allows you to create custom date formats as illustrated.

Layne
 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yevgeniy Treyvus:
Have a look at this snippet...





i'm sorry guys.. i'm a slow learner but keen to know abt this.. i don't understand wat this means.. i'm using JSP.. so where do i put this codes??

i have this solution:
java.util.Date today = new java.util.Date();//creating date
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");//format definition
java.sql.Date curDate = java.sql.Date.valueOf(sdf.format(today));//creating SQL Date.

tried this solution but still get an error...
Syntax error converting datetime from character string.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)

pls help ..
 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody there pls help .. need this to solve it by this week..
 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody there pls help .. need this to solve it by this week..
 
Yevgeniy Treyvus
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not quiet sure on what you're trying to do....Are you trying to get a java.sql.Date from a String, or are you trying to get a java.sql.Date from java.util.Date?? The following snippet should answer both of those questions.

 
catch it before it slithers away! Oh wait, it's a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic