aspose file tools
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 Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to date conversion" Watch "String to date conversion" New topic

String to date conversion

sanjeev kumar mandal

Joined: Nov 26, 2007
Posts: 23

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)


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.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14598

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 8 API documentation
I agree. Here's the link:
subject: String to date conversion