Custom Date Formatting

I'm using the simple code below to custom format an input date and when I compile I keep getting a:

Unhandled exception type ParseException error. Can anyone explain why?

import java.text.*;

publicclass Test{

publicstaticvoid main (String[] parameters)throws ParseException{

SimpleDateFormat inputFormat =new SimpleDateFormat ("yyyyMMdd");

SimpleDateFormat outputFormat =new SimpleDateFormat ("MM/dd/yyyy");

String inputDate ="2004-09-27";

String outputDate = outputFormat.format (inputFormat.parse (inputDate));

System.out.println (outputDate);

}

}

[1115 byte] By [drakestera] at [2007-10-2 20:15:02]
# 1
The input format does not match the format of the string you pass."yyyyMMdd""2004-09-27"~
yawmarka at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 2
That code does not give that error. You're either compiling some other version of the code, or the error is coming from some bit you're not showing here.
jverda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Try putting your code in here.

try{

//your code here

}catch(Exception e){

System.out.println("Error Parsing");

e.printStackTrace();

Norweeda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 4

> The input format does not match the format of the

> string you pass.

>

> "yyyyMMdd"

> "2004-09-27"

>

> ~

Which will of course cause ParseException to be thrown at runtime. But the OP said he's getting a compile-time error. Although, since he can't get that error with that code, maybe he did mean runtime after all. But then, the "unhandled exception" message sounds like compile time.

I'm confused, and I think OP is even more so. :-)

jverda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 5
> The input format does not match the format of the> string you pass.> > "yyyyMMdd"> "2004-09-27"> > ~So would the correct string to pass then be 20040927 ?
drakestera at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 6
As I now see you're throwing that error. Make sure you save your file before compiling.
Norweeda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 7

> > The input format does not match the format of the

> > string you pass.

> >

> > "yyyyMMdd"

> > "2004-09-27"

> >

> > ~

>

> So would the correct string to pass then be 20040927 ?

Yes, or change the format to match the string you're passing.

jverda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> Which will of course cause ParseException to be

> thrown at runtime.

Interestingly enough, it doesn't (I just checked)*. It *will* produce some unexpected results, though... :o)

> But the OP said he's getting a compile-time error.

You're right - I jumped too soon.

* isLenient....

yawmarka at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 9

I'm still getting the ParseExeption. For some reason it doesn't seem to like this line:

String outputDate = sdfOutput.format(sdfInput.parse (date));

Looking at the API docs, it looks like the parse methods takes 2 arguments. Does another argument need to be included?

public Date parse(String text,

ParsePosition pos)

drakestera at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 10

import java.text.*;

public class Foo {

public static void main(String[] args) throws Exception {

SimpleDateFormat inputFormat = new SimpleDateFormat ("yyyyMMdd");

SimpleDateFormat outputFormat = new SimpleDateFormat ("MM/dd/yyyy");

String inputDate = "2004-09-27";

String outputDate = outputFormat.format (inputFormat.parse (inputDate));

System.out.println (outputDate); // 12/09/2003

}

}

yawmarka at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 11
That's what I read, but if that were the case it would be a compile time error not a runtime error.At this point I've got no idea what error you're getting.
Norweeda at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...
# 12
Got it! Thanks everyone for the help : )
drakestera at 2007-7-13 22:57:17 > top of Java-index,Java Essentials,Java Programming...