0

I am trying to learn the ins and outs of DateFormat's parse() and format() methods for the SCJP 6 exam. I recently tried to make some code to format and parse dates and found that the parse method only works if a String is formatted in the way that matches the DateFormat instance's format. Does that mean that if I was using this class in real life and reading a document that had dates formatted in 6 different ways that I would need 6 different DateFormat objects to successfully parse the strings into date objects?

import java.util.*;
import java.text.*;
import java.io.Console;

class PlayWithDates {
    public static void main(String[] args) {
    String formatted = "";
    Date parsed;
    Date d1 = new Date();
    DateFormat defaultFormat = DateFormat.getDateInstance();
    DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
    formatted = defaultFormat.format(d1);
    System.out.println(formatted);
    formatted = shortFormat.format(d1);
    System.out.println(formatted instanceof String);
    System.out.println(formatted);  

    try {
        parsed = shortFormat.parse(formatted);
        System.out.println(parsed); 
    } catch(ParseException pe) {
        System.out.println(pe);
    }
    try {
        parsed = defaultFormat.parse(formatted);
        System.out.println(parsed); 
    } catch(ParseException pe) {
        System.out.println(pe);
    }
    }
}

When I run this program I get:

Dec 11, 2013
true
12/11/13
Wed Dec 11 00:00:00 MST 2013
java.text.ParseException: Unparseable date: "12/11/13"

It seems to me that it would be very difficult to predict when a String would be parseable or not. Should I be somehow doing this with a Calendar instance? Thanks for any explanations. I'm going a bit crazy studying for this exam.

paniclater
  • 903
  • 1
  • 12
  • 18
  • 1
    For each differently formatted String, you will need a DateFormat instance to support it... – MadProgrammer Dec 11 '13 at 20:16
  • Ouch. So if you are parsing a document of Dates you need to foretell what format the dates will be in and then create a DateFormat instance for each possible format and try / catch until you succeed in the parse? That sounds like it is going to be difficult to catch on the exam. – paniclater Dec 11 '13 at 20:36
  • What you can do (and something that Apache Commons has) is create a list of possible date formats and simply iterate over the list until want succeeds...or you run out of formats. You have to remember, given a value of "12" it's impossible to know if that's a day, month or year... – MadProgrammer Dec 11 '13 at 21:55
  • Fair enough. I see that 12/3/12 could be construed as December 3 or March 12. As I should have expected, much more difficult of a problem to solve than I initially anticipated. – paniclater Dec 12 '13 at 00:10

1 Answers1

1

Since SimpleDateFormat accepts format specification in constructor you need separate instance of this class for each format.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • DateFormat has a no-arg constructor that can be used as follows: DateFormat df = DateFormat.getInstance() or the option DateFormat df = DateFormat.getDateInstance() – paniclater Dec 11 '13 at 20:33