Convierta la fecha en hora. Obtengo un valor incorrecto en Android.

I am converting date to time, and I have a problem with minimum date and maximum date.
The minimum date time value is higher than the maximum date time value. I don't know how it is happening. Please help me understand why it is happened.

here i pass the values coming from the date picker when i select the march month 31st date 2014 year.

Date date, minPdate, maxPdate;
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
try {
    date = sdf.parse(someDate);
    minPdate = sdf.parse("31.02.2014");
    maxPdate = sdf.parse("01.03.2014");
    lmin = minPdate.getTime();
    lmax = maxPdate.getTime();
    Log.v("tag","min date "+lmin);
    Log.v("tag","max date "+lmax);
} catch (ParseException e) {
    e.printStackTrace();
}

Minimum date is March-31-2014 and Maximum date is April-01-2014.

And my result is

min date 1393804800000
max date 1393632000000

preguntado el 12 de febrero de 14 a las 07:02

your input should be in longtime millissecound dude -

there is only 28 days in February this year, there's never 31 days in Feb -

How did you printed these values????? @Zharf you are right, and not only for this year, Feb have never 31 days :) -

OP have added either wrong output or not added the real code. So it is not possible to find answer. -

Yes I know feb have 28 days only, i am getting the date from date picker. here i put the values coming from date picker when i select march month. -

2 Respuestas

You have a wrong date in minDate:

31.02.2014 is invalid date.

try with 28.02.2014.

It seems the long value for 31st feb is treated as 3rd march.

So the long value of 1st march is less than 3rd march.

Respondido 12 Feb 14, 07:02

But he has added output "Minimum date is March-31-2014 and Maximum date is April-01-2014." what about that? Any Reason? - Pankaj Kumar

Thank you vipul mittal, when i get the date from date picker month index is starts from 0, so for march i am getting "02". i add +1 to the month index coming from the date picker. it resolves my issue. - Sravanya

Java calendar/date APIs are generally considered (by me at least) horrible. As said, 31st feb is being treated as 3rd of march.

However, since you're using Android's DatePicker API, and you seem to want the milliseconds since epoch, you should probably use datePicker.getCalendarView().getDate(). However it might be possible that this is not available and you should just accept the fact that sometimes in java date related APIs month indexing starts with a 0 and sometimes in other classes it starts with 1.

Apparently DatePicker is modeled after Java's Calendario API, which uses 0-based indexing for months. So really you should be using a Calendar object to handle the data.

Calendar cal = new Calendar();
cal.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
Log.d("tag", "time in millis " + cal.getTimeInMillis());

si quieres un Date puedes usar cal.getTime()

Respondido 12 Feb 14, 08:02

Yeah, there's been a new and improved date and time API coming for a long time, maybe they'll finally get it done soon: jcp.org/en/jsr/detail?id=310 - Zharf

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.