• Post Reply Bookmark Topic Watch Topic
  • New Topic

How toconvert a String into date Object  RSS feed

 
Ranch Hand
Posts: 108
  • 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..
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 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
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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 ]
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use DateFormat.parse()
 
ankit kumar
Ranch Hand
Posts: 108
  • 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.
 
Ranch Hand
Posts: 73
  • 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
  • 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.
 
Greenhorn
Posts: 9
  • 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.

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like your other topic, this doesn't belong in the SCJP forum.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!