wood burning stoves 2.0*
The moose likes Java in General and the fly likes String to date conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to date conversion" Watch "String to date conversion" New topic
Author

String to date conversion

sanjeev kumar mandal
Greenhorn

Joined: Nov 26, 2007
Posts: 19
Hi,

I am trying to convert the date string to date format but it showing exception.I am copying my code below which i have written for conversion of a string to date format.

=====code starts here====
String myDate="Thu Dec 27 11:08:58 IST 2007";
java.text.SimpleDateFormat targetDateFormat = new java.text.SimpleDateFormat("(D) (M) (DD) (hh:mm:ss) (yyyy)");

java.util.Date d2 = now1.parse(myDate);
System.out.println("==============" +d2);
====code ends here=======

=====exception is ====
java.text.ParseException: Unparseable date: "Thu Dec 27 11:08:58 IST 2007"
at java.text.DateFormat.parse(Unknown Source)
(TestDate.java:113)

=======

I need the same format for the date ie only data type should be changed i.e output date will be Thu Dec 27 11:08:58 IST 2007.
Please help me to solve this problem.

Regards
Sanjeev
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The input string "Thu Dec 27 11:08:58 IST 2007" doesn't match the format string "(D) (M) (DD) (hh:mm:ss) (yyyy)".

For example, there is a time zone "IST" in the input string that isn't in the format string. And why do you have all those () braces in the format string?

Carefully read the API documentation of class SimpleDateFormat, it explains exactly how you should write your format string.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String to date conversion
 
Similar Threads
Time format in Java
Deprecated method and parsing rfc2822 date.
Using java.util.Date for comparison
Convert Calendar object to Date object
Date Format Issue