user1772643
user1772643

Reputation: 645

dateTimeNoMillis() formatter in ISODateTimeFormat

I need to validate a string whether it is in the format ISODateTimeFormat.dateTimeNoMillis().

final DateTimeFormatter dateHourMinuteSecondFormatter =ISODateTimeFormat.dateTimeNoMillis();
String s = "2012-W12-12";
try {
        dateHourMinuteSecondFormatter.parseDateTime(s);
    } catch (IllegalArgumentException e) {
        e.setMessage("exception thrown);
    }

It is supposed to throw the exception as the date is in the wrong format but it is not. Is there anything else that I need to add?

Upvotes: 0

Views: 1076

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500675

No, there's nothing more you need to do.

I suspect that actually it is throwing the example, but your message diagnostics are incorrect. This certainly fails for me, using Joda Time 2.1:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {

    public static void main(String[] args) throws Exception {
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
        String s = "2012-W12-12";
        try {
            DateTime dt = formatter.parseDateTime(s);
            System.out.println(dt);
        } catch (IllegalArgumentException e) {
            System.out.println(e);
        }
    }
}

Output:

java.lang.IllegalArgumentException: Invalid format: "2012-W12-12" is malformed
at "W12-12"

Upvotes: 2

Related Questions