aspose file tools*
The moose likes Beginning Java and the fly likes How toconvert a String into date Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How toconvert a String into date Object" Watch "How toconvert a String into date Object" New topic
Author

How toconvert a String into date Object

ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
hi
I want convert Sep 3 2008 09:31:52.000AM into date object. Please anyone help me

thanks..


Cleared SCJP 1.5 | Cleared SCWCD 5.0
SCDJWS 6 in progress.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

use DateFormat.parse()


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
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.
Lok Manick
Ranch Hand

Joined: Jan 24, 2005
Posts: 71
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);

}


with regards,
Loks (SCJP, SCWCD)
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
thanks Lok

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

Joined: Aug 30, 2008
Posts: 9
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.



do not anger the GODS.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

Like your other topic, this doesn't belong in the SCJP forum.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Don't use java.sql.Date - it will probably loose your time information. Use java.sql.Timestamp instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
jQuery in Action, 2nd edition
 
subject: How toconvert a String into date Object