[英]android.text.format.Time replacement need as it is deprecated
我是android的新手,我有使用Time對象需要使用的代碼。 有人可以在不使用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);
使用具有HH:mm:ss格式的SimpleDateFormat函數來實現此功能。
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);
正如@Vamisi所說,我們可以使用GregorianCalendar
,但他的代碼似乎有問題。
如果我們每次在循環中調用以下::
gc.add(GregorialCalendar.Date,i);
GregorialCalendar
實例是GC。 每次如果我們用i
添加日期,首先它將是1 + 1 next,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
我猜這是關於開發Android應用程序的Udacity課程的一部分,論壇中也沒有關於時間類棄用的更正。
它的替代品是格里高利歷年級 。 您可以參考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設置為創建時的當前時間[ Calendar gc = new GregorianCalendar();
你可以簡單地運行gc.get(Calendar.DAY_OF_WEEK)
來獲得一個對應於星期幾的整數。 例如:7對應於星期六,1對應星期日,2對應星期一,依此類推。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.