简体   繁体   中英

Set day of month in calendar android

I'm not a frequent programmer for android and do not really understand why this happens:

Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, anio);
    c.set(Calendar.MONTH, mes);
    c.set(Calendar.DAY_OF_MONTH, dia);
    c.set(Calendar.HOUR, hora);
    c.set(Calendar.MINUTE, minuto);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0); 
    Log.i("ET", "Day: " +  dia );
    Log.i("ET", "CalDAy: " +  c.get(Calendar.DAY_OF_MONTH));
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
    Log.i("ETCalendar", "Tactual "+ formatter.format(c.getTimeInMillis()));
    return c.getTimeInMillis();

This is the LogCat:

I/ET(27819): Day: 16 11-16 19:13:38.656: 
I/ET(27819): CalDAy: 17 11-16 19:13:38.656: 
I/ETCalendar(27819): 4Tactual 17/11/2013 07:13

A possible solution that comes to mind is:

c.set(Calendar.DAY_OF_MONTH, dia - 1);

but would that work?

Thanks!

The problem is that HOUR is used to define a hour in 12-hour format.

Change your code:

calendar.set(Calendar.HOUR, hour);

to:

calendar.set(Calendar.HOUR_OF_DAY, hour);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM