• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

String to date conversion

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic