CR7
CR7

Reputation: 79

Same date different long time

i've a object Date as for example this date "Fri Aug 01 00:00:00 CDT 2014" that through this validation:

Date fechaInicio = filtros.containsKey("dateFechaInicio") ? Fecha.getMinDate(Fecha.getFecha(filtros.get("dateFechaInicio").toString())) : Fecha.getMinDate(null);

Where map filtros contains the key "dateFechaFinal" it will to method Fecha.getMaxDate(Date date) that does

    public static Date getMinDate(Date fecha) {
    Calendar fechaMin = Calendar.getInstance();
    fechaMin.setTime(fecha != null ? fecha : new Date());
    fechaMin.set(fechaMin.get(Calendar.YEAR),
                                fechaMin.get(Calendar.MONTH),
                                fecha != null ? fechaMin.get(Calendar.DAY_OF_MONTH) : fechaMin.getActualMinimum(Calendar.DAY_OF_MONTH),
                                fechaMin.getActualMinimum(Calendar.HOUR_OF_DAY),
                                fechaMin.getActualMinimum(Calendar.MINUTE),
                                fechaMin.getActualMinimum(Calendar.SECOND));
    return fechaMin.getTime();
}

And return a new Date, if this method receives a date return this Long time 1406869200806 , but if receives a date equal to null the method takes the first day from month for example "Fri Aug 01 00:00:00 CDT 2014" but now return this Long time 1406869200000..

"filtros obtained a String that method getFecha() convert to new Date"

Why happen it?

Upvotes: 1

Views: 129

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501926

You're only setting values down to the second, so the millisecond part is left at whatever it was from new Date(). Your description isn't entirely clear, but I suspect you just want:

fechaMin.set(Calendar.MILLISECOND, 0);

Alternatively, use a better API, such as Joda Time or the java.time API in Java 8.

Upvotes: 6

Related Questions