在日历安卓中设置日期

时间:2013-11-16 21:22:38

标签: java android calendar

我不是常见的android程序员,也不太明白为什么会这样:

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();

这是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

想到一个可能的解决方案是:

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

但那会有用吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

问题是HOUR用于定义12小时格式的小时。

更改您的代码:

calendar.set(Calendar.HOUR, hour);

为:

calendar.set(Calendar.HOUR_OF_DAY, hour);