android.text.format.Timereplace需要,因为它已被弃用

我是新来的android和我有我需要使用时间对象的代码。 有人可以帮助我实现相同的function,而不使用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); 

Solutions Collecting From Web of "android.text.format.Timereplace需要,因为它已被弃用"

使用HH:mm:ss格式的SimpleDateFormat函数来实现此function。

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

根据Android文档,时间类在API级别22中已弃用。 改用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); 

我猜测这是开发Android应用程序的Udacity课程的一部分,在论坛上也没有关于Time Class的弃用的更正。

更换它是格里历日历类 。 您可以参考android开发人员博客上更新的文档: http : //developer.android.com/reference/android/text/format/Time.html

至于使用公历的代码中的变化,以便它以相同的方式工作(即使用循环迭代天),你可以做什么:

  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; } 

注意:对象gc在创build时被设置为当前时间[ Calendar gc = new GregorianCalendar(); ],你可以简单地运行gc.get(Calendar.DAY_OF_WEEK)来得到一个对应于星期几的整数。 例如:7对应于星期六,1到星期天,2到星期一等等。

正如@Vamisi所说的,我们可以使用GregorianCalendar ,但是他的代码似乎有错误。

如果我们每次在循环中调用以下内容::

 gc.add(GregorialCalendar.Date,i); 

GregorialCalendar实例是GC。 每次我们加上date,首先是1 + 1,2 + 2,4 + 3 …等等

所以正确的方法是这样的:

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

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