2

I have a date in string format YYYY/MM/DD HH:MM:SS which I need to validate using preconditions google guava class. I am using checkArgument method in lot of other places. How can I use checkArgument method to validate startDate to make sure it is in this format only YYYY/MM/DD HH:MM:SS and if they are not, then throw IllegalArgumentException with some message.

public Builder startDate(String startDate) {
    // validate startDate here using checkArgument if it is not valid then throw IllegalArgumentException.
    this.sDate = startDate;
    return this;
}

How can I use checkArgument method here?

0

3 Answers 3

2

Don't. Write

 try {
   new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(startDate);
 } catch (ParseException e) {
   throw new IllegalArgumentException(e);
 }

..which will also let you store the Date parsed and use it later. (Though, to be fair, java.util.Date is a terrible API best avoided -- but you implied you were using it in a previous question you appear to have deleted.)

If you end up using Joda Time, http://joda-time.sourceforge.net/userguide.html#Input_and_Output explains how to adjust this answer for those needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that by default, SimpleDateFormat instances are lenient w.r.t. parsing input strings. So, unless you want to allow date strings such as 2015/13/32 25:60:60 to parse correctly, you should call setLenient(false) on the SimpleDateFormat instance before parsing.
1

The Answer by Louis Wasserman is correct. He mentions avoiding the java.util.Date.

java.time

That java.util.Date class, and its partner java.util.Calendar, have been supplanted in Java 8 and later by the java.time framework. See the Oracle Tutorial.

We use DateTimeFormatter and DateTimeParseException classes instead. Contained in the java.time.format package.

Here is the equivalent of his Answer using java.time classes.

try {
    DateTimeFormatter.ofPattern ( "yyyy/MM/dd HH:mm:ss" ).parse ( input );
} catch ( e ) {
    throw new DateTimeParseException ( e );
}

Joda-Time

For those unable to move to Java 8 or later, add the Joda-Time library to your project. Joda-Time provided the inspiration for java.time, and the two frameworks share the same concepts.

Wasserman’s same logic applies to our solution: Make a parse attempt, and catch an exception. We use the DateTimeFormatter class from Joda-Time which throws a standard Java IllegalArgumentException when encountering bad input.

try {
    DateTimeFormatter formatter = DateTimeFormat.forPattern ( "yyyy/MM/dd HH:mm:ss" );
    DateTime dt = formatter.parseDateTime ( input );
} catch ( e ) {
    throw new IllegalArgumentException ( e );
}

7 Comments

I am on Java 7 as of now so can't use Java 8. :(
@user1950349 I would strongly advise using third-party libraries like Joda Time over java.util.Date if you can possibly manage it.
@LouisWasserman I can manage that but then will it change anything in the parsing logic you have?
@user1950349, sure, you'll have to use Joda's APIs for that then. joda-time.sourceforge.net/userguide.html#Input_and_Output explains all the details there.
As Wasserman says, the old date-time classes really are so bad that they should be avoided. Add and use the Joda-Time library instead. In using Joda-Time, follow the same logic seen in Wasserman’s Answer: call on a date-time formatter instance to attempt a parse, and catch the parsing exception.
|
0

You can test the whole string against a regular expression:

Preconditions.checkArguments(str.matches(VALID_DATE_REGEX));

To speed things up, you can compile the Pattern once and invert the call

Preconditions.checkArgument(VALID_DATE_REGEX.matcher(str).matches());

However this is a smell. You should not carry this string of text back and forth in your program: if you need that format it's just because at some point you'll convert it to a date. So you'd better decide if your program can accept invalid input and reporting it to the user, or if it should terminate immediately.

The latter is easier to handle: parse the text immediately and throw an exception for invalid input. Otherwise you need to introduce a validation step in your program, and then program a way to stop the flow and showing the user any blocking and non-blocking errors in the input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.