• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Date

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a below method.
The input to this method comes from file. What happens is if the input is say 20083030, then it is conveting to 06/30/2010. Which is not right. Is there any alternative to fix this issue.

public static String getFormattedDate(String s) {
String retDate = null;
if(s == null || s.equalsIgnoreCase(""))
return retDate;
if(s.startsWith("*"))
return s;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
try {

Date dt = format1.parse(s);

retDate = format.format(dt);
} catch (ParseException e) {
e.printStackTrace();
}

Thanks

return retDate;
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this not right? 2008/30/30 is the 30th day of the 30th month of 2008. Since there's only 12 months in a year, the 30st month of 2008 is interpreted as 30 months since the start of 2008; or June, 2010.
 
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
To make SimpleDateFormat throw an exception instead of parsing the string to some strange date, call setLenient(false) on the SimpleDateFormat object before calling parse().
[ March 18, 2008: Message edited by: Jesper Young ]
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic