0

I'm having huge difficulties with simple date format. First of all, I know not all tutorials on all sites are actually good, but the fact that all non trivial formats (not dd/MM/yyyy) gave a parse exception (plus my own tests don't work as expected) is rather frustrating to me.

This is the site in question: http://www.mkyong.com/java/how-to-convert-string-to-date-java/

And I don't understand why something as simple as:

private static void unexpectedFailure() {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String dateInString = "7-Jun-2013";

    try {

        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Throws a parse exception.

Also besides that, I'm trying to parse my own dates. This code gives strange results (unexpected I would say):

public static void doSomething(List<String> list) { 
    Iterator<String> iter = list.iterator();
    String[] line = iter.next().split(" ");
    System.out.println(Arrays.toString(line));
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    format.setLenient(true);


    try {
        System.out.println(line[0]+" "+line[1]);
        System.out.println(format.parse(line[0]+" "+line[1]));
    } catch (ParseException e) {
        System.out.println("In theory this should not get caught.");
    }
}

Prints out this:

[06/08/2015, 13:51:29:849, DEBUG, etc...]
06/08/2015 13:51:29:849
Thu Aug 06 13:51:29 EEST 2015

Thu Aug 06 13:51:29 EEST 2015 WHAT? WHY?

EDIT I'll try and explain. In my last code snippet I'm just trying to determine if the string is a date, and it passes "the test". However when I'm printing it out the format is simply bizzare. I'm starting to think that is because I'm printing a date. How can I even print a DateFormat? What I was expecting was dd/MM/yyyy hh:mm:ss not ddd MMM 06? hh:mm:ss G YYYY

Kalec
  • 2,681
  • 9
  • 30
  • 49
  • 3
    Please be more explicit about the _difficulties_ in your title. What output do you expect in your second snippet? Why? Remember that a `Date` does not have a format. – Sotirios Delimanolis Aug 13 '15 at 15:35
  • What culture are you in? You're not specifying a culture anywhere, so if "Jun" isn't a valid month abbreviation in your system default culture, that would explain the first problem. For the second format, you almost certainly want `HH` instead of `hh`. – Jon Skeet Aug 13 '15 at 15:37
  • you're asking why your Jun 7 date got converted to Aug 6? – Marc B Aug 13 '15 at 15:37
  • Or why the result of `Date.toString()` is the documented result? – Jon Skeet Aug 13 '15 at 15:37
  • @JonSkeet Culture? Hmm, I have no idea what that is. More reading up on `date` I guess ? @MarcB There are two code snippets: Why did the first one fail, for the second one look at edit. – Kalec Aug 13 '15 at 15:41
  • 2
    _I'm starting to think that is because I'm printing a date._ Yes. You're parsing a date string into a `Date` object, but then trying to print the `Date`. This uses its `toString()` which may or may not match the format it got parsed from. If you already have the formatted string (which you just parsed), there's no reason to parse it to a `Date` just to re-print it. Otherwise, just use `DateFormat#format(..)`. – Sotirios Delimanolis Aug 13 '15 at 15:41
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jul 18 '21 at 16:04

1 Answers1

3

And I don't understand why something as simple as:
(code snipped)
Throws a parse exception.

My guess is that it's tripping up over Jun which may not be a valid month abbreviation in your system default locale. I suggest you specify the locale in your SimpleDateFormat constructor:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

Then you're dealing with a locale which definitely has Jun as a month abbreviation.

That said, where possible I'd suggest using numeric formats where possibly, ideally ones following ISO-8601, e.g.

yyyy-MM-dd'T'HH:mm:ss

However when I'm printing it out the format is simply bizzare.

No it's not. You're effectively using

Date date = format.parse(line[0]+" "+line[1]);
System.out.println(date);

So that's calling Date.toString(), documented as:

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

So that's working as expected. However, you want to format the date using your SimpleDateFormat - so you need to call format:

System.out.println(format.format(date));

Of course that just checks that it can round-trip, basically.

As a side note, I suspect you want HH (24-hour clock) instead of hh (12-hour clock) in your format string.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yup, that pretty much sums up everything. Thank you a bunch. For a beginner this is a bit too much data, miss something like `.parse()` returns a date and you're stuck. Again, thanks! – Kalec Aug 13 '15 at 15:49