• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How toconvert a String into date Object

 
ankit kumar
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I want convert Sep 3 2008 09:31:52.000AM into date object. Please anyone help me

thanks..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use DateFormat.parse()
 
ankit kumar
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Lok Manick
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Puneet Nahata
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like your other topic, this doesn't belong in the SCJP forum.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use java.sql.Date - it will probably loose your time information. Use java.sql.Timestamp instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic