android.text.format.Time已弃用,需要更换
我是android新手,我有使用Time object所需的代码。有人能帮我实现相同的功能而不使用时间类吗android.text.format.Time已弃用,需要更换,android,Android,我是android新手,我有使用Time object所需的代码。有人能帮我实现相同的功能而不使用时间类吗 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.
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格式的SimpleDataFormat函数来实现此功能
SimpleDateFormat serverFormat = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
serverFormat.format(Calendar.getInstance());
时间类在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);
GregorianCalendar gc=新的GregorianCalendar();
//因为您要求函数在循环中实现
对于(inti=0;i,正如@Vamisi所说,我们可以使用gregorianalendar
,但他的代码中似乎有一个错误
如果我们每次在循环中调用以下命令:
gc.add(GregorialCalendar.Date,i);
gregoriallicalendar
的实例是GC。如果我们每次按i
添加日期,首先是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);
//因为您要求函数在循环中实现
对于(int i=0;i
我猜这是Udacity开发Android应用程序课程的一部分,论坛中也没有关于时间类的反对意见
替代它的是公历类。您可以参考android开发者博客上更新的文档:
对于使用公历对代码进行的更改,以使其以相同的方式工作(即使用循环循环遍历天),您可以做以下几点:
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;
}
JSONObject forecastJson=新的JSONObject(forecastJsonStr);
JSONArray weatherArray=forecastJson.getJSONArray(OWM_列表);
//使用Gregorian Calendar类而不是Time类获取当前日期
Calendar gc=新的Gregorianalendar();
String[]resultStrs=新字符串[numDays];
对于(int i=0;i
注意:对象gc被设置为创建时的当前时间[Calendar gc=new gregoriacalendar();
],您可以简单地运行gc.get(Calendar.DAY/w)
获取一个整数,该整数对应于一周中的某一天。例如:7对应于周六,1对应于周日,2对应于周一等等。感谢您的回复,但我将如何使用该函数实现For循环中的代码我不知道这是如何替换上述代码的任何解释please@Marzouk:我用c代替时间alendar并获取当前日期,格式如您所需