public static DateTimeValue stringToDatetime(String s)
throws InvalidQueryException {
String[] mainSplit = s.split(" ");
if(mainSplit.length != 2) {
log.error(String.format(dateTimeMessage, s));
throw new InvalidQueryException(String.format(dateTimeMessage, s));
}
String[] dateSplit = mainSplit[0].split("-");
String[] timeSplit = mainSplit[1].split(":");
if((dateSplit.length != 3) || (timeSplit.length != 3)) {
log.error(String.format(dateTimeMessage, s));
throw new InvalidQueryException(String.format(dateTimeMessage, s));
}
try {
int year = Integer.parseInt(dateSplit[0]);
int month = Integer.parseInt(dateSplit[1]);
month--; // normalize 1-12 to 0-11.
int day = Integer.parseInt(dateSplit[2]);
int hour = Integer.parseInt(timeSplit[0]);
int minute = Integer.parseInt(timeSplit[1]);
int second;
int milli = 0;
if(timeSplit[2].contains(".")) {
String[] secondMilliSplit = timeSplit[2].split("\\.");
if(secondMilliSplit.length != 2) {
log.error(String.format(dateTimeMessage, s));
throw new InvalidQueryException(String.format(dateTimeMessage, s));
}
second = Integer.parseInt(secondMilliSplit[0]);
milli = Integer.parseInt(secondMilliSplit[1]);
}
else {
second = Integer.parseInt(timeSplit[2]);
}
return new DateTimeValue(year, month, day, hour, minute, second, milli);
}
catch(NumberFormatException e) {
log.error(String.format(dateTimeMessage, s));
throw new InvalidQueryException(String.format(dateTimeMessage, s));
}
catch(IllegalArgumentException e) {
log.error(String.format(dateTimeMessage, s));
throw new InvalidQueryException(String.format(dateTimeMessage, s));
}
}