participate


Java Programming - How to convert String to Date
<<   Back to Forum  |   Give us Feedback
This topic has 39 replies on 3 pages.    1 | 2 | 3 | Next »
sachindare
Posts:41
Registered: 8/12/03
How to convert String to Date   
Sep 21, 2004 8:20 AM

 
Hello World,
I have date in String "mm/dd/yy" format I want to conver it into String format "yyyy-mm-dd".
I am doing this like

String MY_DATE_FORMAT = "yyyy-MM-dd";
Date d = new Date("05/18/05");
String expDate = new SimpleDateFormat(MY_DATE_FORMAT).format(d);
System.out.println(expDate);

Is there other good way to do this. Because the constructor of Date is depricated now.
I have tried parse() method of DateFormat but it is giving me exception.
I have tried something like this

try {
DateFormat df = new SimpleDateFormat();
Date d = df.parse("05/18/05");
String MY_DATE_FORMAT = "yyyy-MM-dd";
String expDate = new SimpleDateFormat(MY_DATE_FORMAT).format(d);
System.out.println(expDate);
} catch(Exception e) {
e.printStackTrace();
}

This code gives me exception :

java.text.ParseException: Unparseable date: "05/18/05"
at java.text.DateFormat.parse(DateFormat.java:345)
at StringTest.main(StringTest.java:19)

Experts, please help me in this as early as possible.


Regards,
Sachin Dare.
 
Peter-Lawrey
Posts:6,487
Registered: 5/5/04
Re: How to convert String to Date   
Sep 21, 2004 8:31 AM (reply 1 of 39)  (In reply to original post )

 
Have you tried..
Date date = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
 
travis311279
Posts:243
Registered: 3/23/02
Re: How to convert String to Date   
Sep 21, 2004 10:06 AM (reply 2 of 39)  (In reply to original post )

 
Hello World,
I have date in String "mm/dd/yy" format I want to
conver it into String format "yyyy-mm-dd".

if this is the input :- MM/dd/yy
and this is the desired output :- yyyy-MM-dd

String strTmp = "09/21/04";
Date dtTmp = new SimpleDateFormat("MM/dd/yy").parse(strTmp);
String strOutDt = new SimpleDateFormat("yyyy-MM-dd").format(dtTmp);
 
yawmark
Posts:30,134
Registered: 2/3/03
Re: How to convert String to Date   
Sep 21, 2004 10:09 AM (reply 3 of 39)  (In reply to #2 )

 
[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]
 
yawmark
Posts:30,134
Registered: 2/3/03
Re: How to convert String to Date   
Sep 21, 2004 10:09 AM (reply 4 of 39)  (In reply to #3 )

 
[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]
 
yawmark
Posts:30,134
Registered: 2/3/03
Re: How to convert String to Date   
Sep 21, 2004 10:09 AM (reply 5 of 39)  (In reply to #4 )

 
[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]
 
TranManhHung
Posts:1
Registered: 10/25/05
Re: How to convert String to Date   
Oct 25, 2005 8:01 PM (reply 6 of 39)  (In reply to #2 )

 
ParsePosition pos = new ParsePosition(0);
Date dtTmp = new SimpleDateFormat("MM/dd/yy").parse(strTmp, pos);
String strOutDt = new SimpleDateFormat("mm/dd/yyyy").format(dtTmp);
 
RichardTheLionHearted
Posts:248
Registered: 1/19/05
Re: How to convert String to Date   
Oct 25, 2005 8:03 PM (reply 7 of 39)  (In reply to #6 )

 
Boo!
 
ragavendra
Posts:1
Registered: 4/28/06
Re: How to convert String to Date   
Apr 28, 2006 3:46 AM (reply 8 of 39)  (In reply to original post )

 
I want urgent help plz
 
macrules2
Posts:2,023
Registered: 2/7/02
Re: How to convert String to Date   
Apr 28, 2006 3:48 AM (reply 9 of 39)  (In reply to #8 )

 
WTF?!?
 
quitte
Posts:3,217
Registered: 2/7/00
Re: How to convert String to Date   
Apr 28, 2006 3:49 AM (reply 10 of 39)  (In reply to #8 )

 
Is this a demon thread?
 
andrew_malcolm
Posts:2,120
Registered: 11/12/01
Re: How to convert String to Date   
Apr 28, 2006 3:51 AM (reply 11 of 39)  (In reply to #9 )

 
Well digging up an ancient post, and not asking a specific question isn't going to get you far
 
Mashimaro
Posts:2
Registered: 1/18/07
Re: How to convert String to Date   
Jan 18, 2007 7:25 AM (reply 12 of 39)  (In reply to original post )

 
I am glad to see the infomation about date format here. How about convert a string Jan 3,2007 23:59 to a string mm/dd/yyy. I tried to use

String strTmp = "Jan 3, 2007 23:59";
Date dtTmp = new SimpleDateFormat("MMM d, yyyy H:mm").parse(strTmp);
String strOutDt = new SimpleDateFormat("mm/dd/yyyy").format(dtTmp);
System.out.println("CustomReports_EscrowStatement_view.jsp strOutDt" + strOutDt);

The strOutDt is 59/03/2007. It was wrong? Can I get help?
 
yawmark
Posts:30,134
Registered: 2/3/03
Re: How to convert String to Date   
Jan 18, 2007 7:28 AM (reply 13 of 39)  (In reply to #12 )

 
> String strOutDt = new SimpleDateFormat("mm/dd/yyyy").format(dtTmp);

The "mm" is for minutes. Presumably, you want "MM" (you had it right when you parsed the date).

~
 
Mashimaro
Posts:2
Registered: 1/18/07
Re: How to convert String to Date   
Jan 18, 2007 8:19 AM (reply 14 of 39)  (In reply to #13 )

 
Thank you very much for your quick respons and great help. I got it right with your help. :-)

Jean
 
This topic has 39 replies on 3 pages.    1 | 2 | 3 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 129

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums