• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How toconvert a String into date Object

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I want convert Sep 3 2008 09:31:52.000AM into date object. Please anyone help me

thanks..
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar ankit:

I want convert Sep 3 2008 09:31:52.000AM



-------------------------------------
Date d=new Date(2008,8,3,9,31,52);//new Date(year,month,date,hr,MM,ss)
----------------------------------

just try System.out.println(d)

you will understand

Hope This Helps
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To convert Strings to Date -- and vice versa -- you can try...

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

Henry
[ September 05, 2008: Message edited by: Henry Wong ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use DateFormat.parse()
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading this from a text file Sep 3 2008 09:31:52.000AM and I want to insert the same Date into SQL database. how i read and parse this date.

please post the code.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kumar hope this helps

public static void main(String[] args) throws ParseException {

String str = "Sep 3 2008 09:31:52 AM";

Date d;

SimpleDateFormat sdfOutput = new SimpleDateFormat ( "MMM dd yyyy hh:mm:ss a" );
Date date = sdfOutput.parse( str );
System.out.println("Date "+ date);

}
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Lok

but i think you have taken Date as java.util.Date but in sql you have to take java.sql.Date.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar ankit:
thanks Lok

but i think you have taken Date as java.util.Date but in sql you have to take java.sql.Date.



You should always use java.util.Date throughout your application. However, when making call to database, use this method for converting java.util.Date to java.sql.Date.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like your other topic, this doesn't belong in the SCJP forum.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use java.sql.Date - it will probably loose your time information. Use java.sql.Timestamp instead.
 
reply
    Bookmark Topic Watch Topic
  • New Topic