0

I have 2 QueryParam's

Date = "YYYY-MM-DD" Country = "3 letters"

I need to return 400 error if the inputs are invalid, I don't have to check if a date is maybe in future or that the country doesn't exist, I have to check that the format is valid. That country is always 3 letters and not some weird symbols and date is (number-number-number-number --- number-number --- number-number).

Tim
  • 41,901
  • 18
  • 127
  • 145
user3630020
  • 15
  • 1
  • 3
  • for the date checking have a look at http://stackoverflow.com/questions/4528047/checking-the-validity-of-a-date – quant May 13 '14 at 06:42
  • @quant that's perfect, now I only need to check the Country abbreviation – user3630020 May 13 '14 at 06:50
  • @Totò Haven't really tried anything yet, because I don't have an idea where to begin. – user3630020 May 13 '14 at 06:51
  • @user3630020 Well, there's a limited number of countries in the world, so why don't you just do a list of all countries and check, if the string is somewhere in the list? You may also use a regular expression, \w\w\w (see http://stackoverflow.com/questions/2062169/regex-w-in-utf-8), or [A-Za-z][A-Za-z][A-Za-z], if you don't want to have numbers in it, should do it. – quant May 13 '14 at 07:03

2 Answers2

0

A very naive solution I can think of right now is as follows. If you are not comfortable using regex, for date you can simply try string.split() or any equivalent java method which splits based on a certain character. After that just try to check if all three splits are valid numbers using Integer.parseInt(). For letters you can check if each character is in the range that you are requiring by using a simple ascii value comparison. However, regex will always be better.

working
  • 873
  • 3
  • 11
  • 21
0

You can use the String.split() function which give you an array of string (3 strings in your case) and then compare the value in this array as you desired by using Integer.parseInt()
For example :

String date = "2010-02-12";
String arr[] = date.split("-");
int day = Integer.parseInt(arr[2]);
if(day < 1 || day > 31)
 System.out.println("Not a good day !");

This code is not tested but it show you how it works.
Be aware that Integer.parseInt() can throws NumberFormatException, so catch it and then you can raise your error 400.
It is not as nice as regex, but it will work.

JumpIfBelow
  • 53
  • 2
  • 11