0

Example, from 1 min ago i use api and time response result from Api service as:

{
  "date_time":"2016-03-10 03:20:30"
}

Please discuss step by step, And if available how can programticly display it in Arabic format منذ 15 دقيقة

My Code for date in list view adapter

 TextView date_time = (TextView) convertView.findViewById(R.id.date_time_list_home);
 date_time.setText(m.dateTime());

1 Answers1

3

First, for any date/time manipulation on Android, I highly recommend using the ThreeTenABP library. This is a back port of the Java 8 java.time.* package, circumventing the notoriously disappointing java.util.Date and java.util.Calendar classes.

To parse your "date_time" using this library, you can use the following code:

// I set the ZoneId to systemDefault, but you should really use the ZoneId of the server
DateTimeFormatter formatter = DateTimeFormatter
                                  .ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                                  .withZone(ZoneId.systemDefault()); 

Instant instant = formatter.parse(m.dateTime(), Instant.FROM);

Android provides the DateUtils class to display date and time information. This class takes into account system settings such as Locale and 12/24-hour format. Therefore, if the Locale of the device is set to any of the Arabic locales (ar_), the date/time will be displayed in a format suited for it.

String display = DateUtils.getRelativeTimeSpanString(
                        instant.toEpochMilli(),
                        Instant.now().toEpochMilli(),
                        DateUtils.MINUTE_IN_MILLIS);

date_time.setText(display);

The last parameter in getRelativeTimeSpanString is the minimum resolution, so setting DateUtils.MINUTE_IN_MILLIS will not display the difference in seconds.


If you insist on using the Java 7 classes, here is the same code using them:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(m.dateTime());

String display = DateUtils.getRelativeTimeSpanString(
                        date.getTime(),
                        new Date(),
                        DateUtils.MINUTE_IN_MILLIS);

date_time.setText(display);

If you need a "transition resolution" greater than a single day (i.e. you want to display a date/time that is further than one day in the past as "… days ago") you can use the DateUtils.getRelativeDateTimeString() method instead:

String display = DateUtils.getRelativeDateTimeString(
                        mContext,
                        instant.toEpochMilli(),
                        DateUtils.MINUTE_IN_MILLIS,
                        DateUtils.WEEK_IN_MILLIS,
                        DateUtils.FORMAT_ABBREV_ALL);

Any date that is further back than the transitionResolution (in this case, one week) will be displayed in an appropriate date format, instead of the relative format. The minResolution and transitionResolution can be any long value, DateUtils contains other convenient constants such as MONTH_IN_MILLIS and YEAR_IN_MILLIS.

The last parameter takes an integer for formatting flags. These flags override the default formatting that DateUtils uses for each Locale; view the documentation for more information.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Thanks for nice code, I use ThreeTenABP Lbrary but it format date to Sep 19, 2016 and i want it to format as time ago same ( 19 min ago ) – Mohamed Abozaid Oct 04 '16 at 17:06
  • I am having difficulty understanding your statement. It sounds like you are using the `ThreeTenABP` library to format the text, and not `DateUtils`? The method I use above (`formatter.parse()`) is to translate your date/time string (`m.dateTime()`) into an `Instant`, or a specific point in time. This is *not* meant to be used to display the date/time on screen. Then I use `DateUtils.getRelativeTimeSpanString()` to get a `String` representation of that time as some time ago (i.e. "19 mins ago"). – Bryan Oct 04 '16 at 18:37
  • I use this code: `String display =(String) DateUtils.getRelativeTimeSpanString( date.getTime(), new Date(), DateUtils.MINUTE_IN_MILLIS); date_time.setText(display);` and result in screen : `Sep 19, 2016` , Not: 19 min ago, Understand me! – Mohamed Abozaid Oct 04 '16 at 19:24
  • @MohamedAbozaid Oh, okay. I forgot that `DateUtils.getRelativeTimeSpanString()` will display the relative `String` in that format if both times are not within the same day. Try a date/time within the current day. If you need it to display something like "15 days ago" instead of "Sep 19, 2016", use `getRelativeDateTimeString()`. I will update with information regarding that method. – Bryan Oct 04 '16 at 20:06
  • Give me error `Error:(107, 35) error: method getRelativeDateTimeString in class DateUtils cannot be applied to given types; required: Context,long,long,long,int found: long,long,long reason: actual and formal argument lists differ in length` I think miss context paramter and int paramter – Mohamed Abozaid Oct 04 '16 at 22:50
  • @MohamedAbozaid Yes, it looks like you are missing the two other parameters. `DateUtils.getRelativeDateTimeString()` takes a `Context`, a `long` value for the time in question, a `long` value for the minimum resolution, a `long` value for the transition resolution, and an `int` value for the format flags. – Bryan Oct 05 '16 at 12:43