File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String to date conversion in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to date conversion in java" Watch "String to date conversion in java" New topic
Author

String to date conversion in java

Pallavi Kulkarni
Greenhorn

Joined: Feb 18, 2008
Posts: 4
I have my date in string format and I want to convert it into date format. I also tried toDate(String) function. But it is not giving result. And I also tried following code:

SimpleDateFormat formatter=new SimpleDateFormat(text3);
java.sql.Date effect_from=new java.sql.Date( formatter.parse(text3).getTime());
java.sql.Date end_on = new java.sql.Date(formatter.parse(text3).getTime());


In above code when I try to print 'effect_from'it prints any random date.
So please tell me solution.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Welcome to JavaRanch.

The SimpleDateFormat constructor doesn't take the text to be parsed as argument, it takes the format description as argument. See the javadocs of the SimpleDateFormat class for a lengthy explanation of the available formats.


Ping & DNS - updated with new look and Ping home screen widget
Amit Y Desai
Ranch Hand

Joined: Dec 06, 2007
Posts: 35
SimpleDateFomat line is wrong...
instead of passing actuall string to SimpleDateFormat,it should pass the pattern..

eg :-

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Please take the time to choose the correct forum for your posts. This forum is for questions on HTML.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Amit Y Desai
Ranch Hand

Joined: Dec 06, 2007
Posts: 35
hi Bear Bibeault ,

i thing this post belongs to correct topic..as string to date conversion comes under "java in general"...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Originally posted by Amit Y Desai:
SimpleDateFomat line is wrong...
instead of passing actuall string to SimpleDateFormat,it should pass the pattern..

eg :-


Note that "mm/dd/yyyy" means most likely not what you think it means. "mm" is minutes, not months. Use "MM" for months. See the API documentation of class SimpleDateFormat for documentation about the format string.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Originally posted by Amit Y Desai:
i thing this post belongs to correct topic..as string to date conversion comes under "java in general"...

It was originally posted in the HTML forum, here, that's why that post is there.

You will see many such messages as unfortunately it's a common phenomenon that people post topics into unrelated forums.
vikram veera
Ranch Hand

Joined: Dec 26, 2000
Posts: 43
Please find the code in the below URL
http://developerparadise.com/devworld/Faqs/2007/Java/3Collection/JavaCollectionFaqs_3_Integerate.php#q0

[ February 22, 2008: Message edited by: vikram veera ]
[ February 22, 2008: Message edited by: vikram veera ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to date conversion in java
 
Similar Threads
Between Syntax
how to update one column of my table?
Passing java.sql.date to constructors
how do i get todays dat without the time
how to get the date from the string representation of date