why Java date formatter ignores seconds, milliseconds and time zone information while parsing date

381 views Asked by At

I am very confused in the below code. Even after passing the correct dateformat for parsing the entire date, time and timezone, why java is ignoring the information beyond minutes.

I have a list of date formatters as my incoming date can come in any formats. I want all dates to be parsed with whatever information was provided.

But I see that in output, all the information for seconds, milliseconds and timezone is ignored.

public class DateValidator {


 final static DateFormat dateFormat_hour = new SimpleDateFormat("yyyy-MM-dd,HH");
    final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final DateFormat DATE_TIMESTAMP_MIN= new SimpleDateFormat("yyyy-MM-dd,HH:mm");
    private static final DateFormat DATE_TIMESTAMP_SEC = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
private static final DateFormat DATE_TIMESTAMP_MILLISEC = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss.SSS");

private static final DateFormat DATE_TIMESTAMP_ZONE_OFFSET = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss.SSSZ");

/**
 * the list formatter which will store all date formats which are possible in incoming date.
 */
private static final List<DateFormat> DATE_FORMATTER_LIST;
static {
    DATE_FORMATTER_LIST = Arrays.asList(DATE_TIMESTAMP_MIN, DATE_TIMESTAMP_SEC, DATE_TIMESTAMP_MILLISEC, DATE_TIMESTAMP_ZONE_OFFSET,dateFormat_hour,dateFormat);
}

/**
 * validating the date type is valid or not.
 *
 * @param dateString the date string
 * @return true or false
 */
public static Date parseDate(String dateString) {

    if (dateString == null || dateString.isEmpty()) {
        return null;
    }

    for (DateFormat pattern : DATE_FORMATTER_LIST) {

        try {
            return pattern.parse(dateString);

        } catch (ParseException pe) {
           //ignore
        }
    }
    System.out.println(("Error while parsing date , date not in valid format"));
    return null;
}

public static void main(String args[]){
 
    System.out.println(new Date(parseDate("2020-11-01,01:01:05.555+0300").getTime()));//****no, no
    System.out.println(new Date(parseDate("2020-11-01,01:01:05.555+0000").getTime()));//****no, no
    System.out.println(new Date(parseDate("2020-11-01,01:01:05.000+0000").getTime()));//****no, no
    System.out.println(new Date(parseDate("2020-11-01,01:01:59.000").getTime()));//****no, no
    System.out.println(new Date(parseDate("2020-11-01,01:01:05").getTime()));//****no, no
    System.out.println(new Date(parseDate("2020-11-01,01:01").getTime()));//*****good
    System.out.println(new Date(parseDate("2020-11-01,01").getTime()));//*****good
    System.out.println(new Date(parseDate("2020-11-01").getTime()));//*****good
}

}

Output is :

Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:01:00 IST 2020
Sun Nov 01 01:00:00 IST 2020
Sun Nov 01 00:00:00 IST 2020
0

There are 0 answers