java - 安卓时间问题

标签 java android

 private void getSelectedTime(final String json){

        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH); // Note: zero based!
        int day = now.get(Calendar.DAY_OF_MONTH);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        int millis = now.get(Calendar.MILLISECOND);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");


        JSONArray list;
        JSONObject jsonObject;

        try {
            jsonObject = new JSONObject(json);

            list = jsonObject.getJSONArray("3");
            StringTokenizer tokenizer = new StringTokenizer(list.get(0).toString(),"-");
            String startTime = tokenizer.nextToken();
            String endTime = tokenizer.nextToken();
            String temp1 = year+"/"+month+"/"+day+" "+startTime;
            String temp2 = year+"/"+month+"/"+day+" "+endTime;
            System.out.println("temp1="+temp1);
            System.out.println("temp2="+temp2);

            Date date1 = dateFormat.parse(temp1); // temp1=2012/5/25 03:00
            Date date2 = dateFormat.parse(temp2); //temp2=2012/5/25 03:06

            System.out.println("Year1="+date1.getYear());
            System.out.println("Month1="+date1.getMonth());
            System.out.println("Day1="+date1.getDay());
            System.out.println("Hour1="+date1.getHours());
            System.out.println("Minutes1="+date1.getMinutes());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

在我的应用程序中,我正在处理时间问题,但这里遇到了一些问题。看看下面我得到的结果。

list.get(0) = 03:00-03:06
temp1=2012/5/25 03:00
temp2=2012/5/25 03:06

但是当我尝试这样做时

System.out.println("Year="+date1.getYear());
System.out.println("Month="+date1.getMonth());
System.out.println("Day="+date1.getDay());
System.out.println("Hour="+date1.getHours());
System.out.println("Minutes="+date1.getMinutes());

我得到了这个结果

Year=112
Month=4
Day=5
Hour=3
Minutes=0

谁能告诉我为什么结果是错误的?

最佳答案

Could anyone tell me why I the result is wrong?

当然 - 您正在使用已弃用的方法(您应该收到警告 - 不要忽略它们!),并且您还没有阅读它们的文档。例如,来自 Date.getYear():

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

如果您想坚持使用 JDK,则应该使用 java.util.Calendar (通过 setTimeDate 填充它) )在适当的时区。请注意,Calendar 中的月份仍然是从 0 开始的,尽管年份至少更合理。

但是,通常使用 Joda Time 会更好如果可以的话。这是一个经过深思熟虑的更好的 API。不过,它可能太大,您无法在 Android 上使用它 - 您可能希望查看是否有可用的精简版本。

关于java - 安卓时间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188406/

相关文章:

java - java中如何获取特定的日期类型

android - 没有声音的Android通知

android - 创建 adb pull bash 命令

java - android 复选框在 setchecked 复选框为 false 时获取 java.lang.nullpointerException

java - 输入流在文件结束之前返回-1

java - 无法解析类型。它间接引用所需的 .class 文件

java - 基于组件的架构的粒度

android - 有没有办法禁用特定包的使用?

android - 15 岁的最佳 Android 开发者付款方式?

java - 这里的接口(interface)是如何实现的?