File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Date" Watch "Java Date" New topic

Java Date

Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
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;
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) {


return retDate;
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

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.

[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14741

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Java Date