I have a date of type "long" and want to validate it. It was converted elsewhere from an xml File.
- xml File date: 2018-01-35T00:00+02:00
- long date: 1517695200000L
This date (2018-01-35 as yyyy-mm-dd) should be an invalid date.
I already tried the solution given in: How to sanity check a date in java
I converted the long to a Date but that already changed the date from 2018-01-35 to 2018-02-04, so there was no exception thrown. This was the complete code:
long longDate = 1517695200000L;
Date date = new Date(longDate);
System.out.println("long: " + longDate);
System.out.println("Date: " + date);
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(date);
try {
cal.getTime();
} catch (Exception e) {
LOG.error("invalid date");
return false;
}
Is there any possibilty to convert a long to a Date without this "self-validation"? Or does anybody have another idea? Thanks in advance.