在日历安卓中设置日期

问题描述 投票:1回答:1

我不是常见的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);

但那会有用吗?

谢谢!

java android calendar
1个回答
4
投票

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

更改您的代码:

calendar.set(Calendar.HOUR, hour);

至:

calendar.set(Calendar.HOUR_OF_DAY, hour);
© www.soinside.com 2019 - 2024. All rights reserved.