简体   繁体   English

android.text.format.Time替换需要,因为它已被弃用

[英]android.text.format.Time replacement need as it is deprecated

I am new to android and I have the code I need to use using Time object. 我是android的新手,我有使用Time对象需要使用的代码。 Can someone help me achieve the same functionality without using Time class. 有人可以在不使用Time类的情况下帮助我实现相同的功能。

Time dayTime = new Time();
dayTime.setToNow();

// we start at the day returned by local time. Otherwise this is a mess.
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);

// now we work exclusively in UTC
dayTime = new Time();
long dateTime;

// Cheating to convert this to UTC time, which is what we want anyhow
// this code below is in a for loop
dateTime = dayTime.setJulianDay(julianStartDay + i);
day = getReadableDateString(dateTime);

Use SimpleDateFormat function with HH:mm:ss format to achieve this functionality . 使用具有HH:mm:ss格式的SimpleDateFormat函数来实现此功能。

SimpleDateFormat serverFormat = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
serverFormat.format(Calendar.getInstance());

Time class is deprecated in API level 22 as per the Android Documentation . 根据Android文档 ,API级别22中不推荐使用时间类。 Use GregorianCalendar class instead. 请改用GregorianCalendar类。

GregorianCalendar gc = new GregorianCalendar();

//since you have asked for the function to achieve in loop
for(int i= 0; i<Array.length;i++){
        gc.add(GregorianCalendar.DATE, 1);
}
 //code for formatting the date   
Date time = gc.getTime();
SimpleDateFormat shortDateFormat = new SimpleDateFormat("EEE MMM dd");
day = shortenedDateFormat.format(time);

As @Vamisi says, we can use GregorianCalendar , but there seems to be a fault in his code. 正如@Vamisi所说,我们可以使用GregorianCalendar ,但他的代码似乎有问题。

If we call the following each time in the loop:: 如果我们每次在循环中调用以下::

gc.add(GregorialCalendar.Date,i);

Instance of GregorialCalendar is GC. GregorialCalendar实例是GC。 Each time if we add date by i , first it will be 1+1 next, 2+2 , 4+3 ...etc 每次如果我们用i添加日期,首先它将是1 + 1 next,2 + 2,4 + 3 ......等等

So the correct method would be this: 所以正确的方法是这样的:

//since you have asked for the function to achieve in loop
for(int i= 0; i<Array.length;i++){
        GregorianCalendar gc = new GregorianCalendar();
        gc.add(GregorianCalendar.DATE, i);
}
//code for formatting the date   
Date time = gc.getTime();
SimpleDateFormat shortDateFormat = new SimpleDateFormat("EEE MMM dd");
day = shortDateFormat.format(time);

Reference: http://developer.android.com/reference/java/util/GregorianCalendar.html http://www.tutorialspoint.com/java/util/gregoriancalendar_add.htm 参考: http//developer.android.com/reference/java/util/GregorianCalendar.html http://www.tutorialspoint.com/java/util/gregoriancalendar_add.htm

I am guessing this is a part of the Udacity course on Developing Android Apps and there are no corrections in the forum as well regarding the deprecation of Time Class. 我猜这是关于开发Android应用程序的Udacity课程的一部分,论坛中也没有关于时间类弃用的更正。

The replacement for it is the Gregorian Calendar Class . 它的替代品是格里高利历年级 You can refer to the document updated on the android developer blog: http://developer.android.com/reference/android/text/format/Time.html 您可以参考Android开发人员博客上更新的文档: http//developer.android.com/reference/android/text/format/Time.html

As for the changes in the code using Gregorian Calendar so that it works in the same way (ie using the loop for iterating through days) here's what you can do: 至于使用格里高利历的代码更改,以便它以相同的方式工作(即使用循环迭代几天),这里是你可以做的:

        JSONObject forecastJson = new JSONObject(forecastJsonStr);
        JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);

        //Using the Gregorian Calendar Class instead of Time Class to get current date
        Calendar gc = new GregorianCalendar();

        String[] resultStrs = new String[numDays];

        for(int i = 0; i < weatherArray.length(); i++) {
            // For now, using the format "Day, description, hi/low" for the app display
            String day;
            String description;
            String highAndLow;

            // Get the JSON object representing the day
            JSONObject dayForecast = weatherArray.getJSONObject(i);

            //Converting the integer value returned by Calendar.DAY_OF_WEEK to
            //a human-readable String
            day = gc.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH);

            //iterating to the next day
            gc.add(Calendar.DAY_OF_WEEK, 1);

            // description is in a child array called "weather", which is 1 element long.
            JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
            description = weatherObject.getString(OWM_DESCRIPTION);

            // Temperatures are in a child object called "temp".
            JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
            double high = temperatureObject.getDouble(OWM_MAX);
            double low = temperatureObject.getDouble(OWM_MIN);

            highAndLow = formatHighLows(high, low);
            resultStrs[i] = day + " - " + description + " - " + highAndLow;
        }

Note: The object gc gets set to the current time at the time of its creation [ Calendar gc = new GregorianCalendar(); 注意:对象gc设置为创建时的当前时间[ Calendar gc = new GregorianCalendar(); ] and you can simple run gc.get(Calendar.DAY_OF_WEEK) to get an integer with correspond to the day of the week. 你可以简单地运行gc.get(Calendar.DAY_OF_WEEK)来获得一个对应于星期几的整数。 For example: 7 corresponds to Saturday, 1 to Sunday, 2 to Monday and so on. 例如:7对应于星期六,1对应星期日,2对应星期一,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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