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]

> 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 >

> > 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 >

> 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....
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)
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
}
}