Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.text.format.Time replacement need as it is deprecated

Tags:

android

I am new to android and I have the code I need to use using Time object. Can someone help me achieve the same functionality without using Time class.

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);
like image 305
Sidd Avatar asked Jun 08 '15 07:06

Sidd


4 Answers

Use SimpleDateFormat function with HH:mm:ss format to achieve this functionality .

SimpleDateFormat serverFormat = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
serverFormat.format(Calendar.getInstance());
like image 117
Anand Savjani Avatar answered Nov 18 '22 01:11

Anand Savjani


Time class is deprecated in API level 22 as per the Android Documentation. Use GregorianCalendar class instead.

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);
like image 30
Vamsi Tallapudi Avatar answered Nov 18 '22 00:11

Vamsi Tallapudi


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.

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

As for the changes in the code using Gregorian Calendar so that it works in the same way (i.e. 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();] and you can simple run gc.get(Calendar.DAY_OF_WEEK) to get an integer with correspond to the day of the week. For example: 7 corresponds to Saturday, 1 to Sunday, 2 to Monday and so on.

like image 22
Kriti Sharan Avatar answered Nov 18 '22 00:11

Kriti Sharan


As @Vamisi says, we can use GregorianCalendar, but there seems to be a fault in his code.

If we call the following each time in the loop::

gc.add(GregorialCalendar.Date,i);

Instance of GregorialCalendar is GC. Each time if we add date by i, first it will be 1+1 next, 2+2 , 4+3 ...etc

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

like image 1
Tharun Avatar answered Nov 18 '22 00:11

Tharun