SimpleDateFormat getHours()返回不正确的值

时间:2012-12-23 11:13:16

标签: java android simpledateformat

为什么“09:50”的getHours()返回8? 代码:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = sdf.parse("09:50");
int hours = date.getHours(); // return 8 instead of 9 ???

3 个答案:

答案 0 :(得分:0)

            Calendar prueba = Calendar.getInstance();

        prueba.set(2013, 1, 6, 10, 49, 32);

        int day = prueba.get(Calendar.DAY_OF_MONTH);
        int month = prueba.get(Calendar.MONTH);
        int year = prueba.get(Calendar.YEAR);
        int hours = prueba.get(Calendar.HOUR);
        int minutes = prueba.get(Calendar.MINUTE);
        int seconds = prueba.get(Calendar.SECOND);

        Log.v("printf", "Date: " + day + "/"
                                 + month + "/"
                                 + year + "\n");
        Log.v("printf", "Time: " + hours + ":"
                                 + minutes + ":"
                                 + seconds + "\n");

PD:记住字段月份从日历类开始0(0月1日,2月1日,......) PD2:我在filter logcat上有一个名为“printf”的个性化标签。更改您使用的标记。

答案 1 :(得分:0)

似乎您的SimpleDateFormat可能尚未设置为默认时区。 主叫:

sdf.setTimeZone(TimeZone.getDefault());
在解析文字之前

应解决您的问题。 我测试了以下代码:

...
    public static final SimpleDateFormat CDfTimestamp = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
...
    Date da1 = null;
    for (String dl1 : new String[] { "1970-01-01 12:34:56",
            "2016-01-01 12:34:56" }) {
        try {
            CDfTimestamp.setTimeZone(TimeZone.getTimeZone("GMT+05:00"));
            da1 = CDfTimestamp.parse(dl1);
            System.out.printf("parse GMT+05:00 %s -> %d %d %d (os=%d)\n", dl1, da1
                    .getHours(), da1.getMinutes(), da1.getSeconds(), CDfTimestamp
                    .getTimeZone().getOffset(0));
            CDfTimestamp.setTimeZone(TimeZone.getTimeZone("UTC"));
            da1 = CDfTimestamp.parse(dl1);
            System.out.printf("parse UTC       %s -> %d %d %d (os=%d)\n", dl1, da1
                    .getHours(), da1.getMinutes(), da1.getSeconds(), CDfTimestamp
                    .getTimeZone().getOffset(0));
            CDfTimestamp.setTimeZone(TimeZone.getDefault());
            da1 = CDfTimestamp.parse(dl1);
            System.out.printf("parse default   %s -> %d %d %d (os=%d)\n", dl1, da1
                    .getHours(), da1.getMinutes(), da1.getSeconds(), CDfTimestamp
                    .getTimeZone().getOffset(0));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
...

获得以下输出:

parse GMT+05:00 1970-01-01 12:34:56 -> 8 34 56 (os=18000000)
parse UTC       1970-01-01 12:34:56 -> 13 34 56 (os=0)
parse default   1970-01-01 12:34:56 -> 12 34 56 (os=3600000)
parse GMT+05:00 2016-01-01 12:34:56 -> 8 34 56 (os=18000000)
parse UTC       2016-01-01 12:34:56 -> 13 34 56 (os=0)
parse default   2016-01-01 12:34:56 -> 12 34 56 (os=3600000)

答案 2 :(得分:-3)

不推荐使用日期。你应该使用日历。

 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
 Date date = sdf.parse("09:50");
 Calendar cal = new GregorianCalendar();
cal.setTime(date);
int hours = cal.get(Calendar.HOR_OF_DAY);