-5

Is there any source code for turning the timeInMills to a 24hour/Date like from the messenger app. When the timeInMills is below 24 hour it will return like this 16:15 but when it is over 24hour it will return like this THU at 16:15. I am currently creating a chat app and I want to add this to my app.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    This is from a difference in the current time and the particular posting? I am not aware of a specific library call, but in general set the particular format for a date (e.g., "HH:mm", or "ddd HH:mm") based upon the time difference. You may want to include months or years if the time gap is sufficient (kind of how the Linux `ls` provides information). See [How do you format date and time](https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – KevinO May 05 '18 at 03:02
  • Do you mean if it’s over 24 hours *ago*? – Ole V.V. May 05 '18 at 05:57

2 Answers2

1

Edit

Beware this line: long last24hTimestamp = current - MILLISECONDS_PER_DAY;

I'm calculating on behalf of UTC time.

To get the local time, you should take into account the timezone.


So basically you have to calculate if the timestamp timeInMillis is within the last 24h then use one format otherwise use another format.

This will help you:

public static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;// In real app you should pre-calculate this value
public static final String RECENT_DATE_FORMAT = "HH:mm";
public static final String OLD_DATE_FORMAT = "E' at 'HH:mm";

public static String displayTime(long timestamp) {
  long current = System.getCurrentTimeMillis();
  long last24hTimestamp = current - MILLISECONDS_PER_DAY;

  if (timestamp > last24hTimestamp) {

   // Received message within a day, use first format
   SimpleDateFormat sdf = new SimpleDateFormat(RECENT_DATE_FORMAT);
   return sdf.format(new Date(timestamp));
  } else {
    // Message is older than 1 day. Use second format

  }

}

Something you should take care of:

  1. Consider parsing with timezone/localization if your app run in multiple places

  2. If you're using java 8, try to use DateTimeFormatter. It's threadsafe and you can use a static instance per date format, no need to initialize SimpleDateFormat everytime you want to format a date

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
1

java.time

For work with dates or times in Java I recommend java.time, the modern Java date and time API.

static DateTimeFormatter lessThan24HoursAgoFormatter 
        = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);
static DateTimeFormatter moreThan24HoursAgoFormatter 
        = DateTimeFormatter.ofPattern("EEE 'at' HH:mm", Locale.ENGLISH);
static ZoneId zone = ZoneId.of("America/Yakutat");

public static String getDisplayString(long timeInMills) {
    ZonedDateTime dateTime = Instant.ofEpochMilli(timeInMills)
            .atZone(zone)
            .truncatedTo(ChronoUnit.MINUTES);
    ZonedDateTime currentTimeYesterday = ZonedDateTime.now(zone).minusDays(1);
    if (dateTime.isAfter(currentTimeYesterday)) {
        return dateTime.format(lessThan24HoursAgoFormatter);
    } else {
        return dateTime.format(moreThan24HoursAgoFormatter);
    }
}

Running just now

  • getDisplayString(1_525_402_083_258L) returned Thu at 18:48.
  • getDisplayString(1_525_490_972_172L) returned just 19:29.

Please put your desired time zone where I put America/Yakutat. I recommend you insert checks that the millis denote a time that is not more than a week ago and not in the future, since the returned string would be confusing in these cases.

It’s also a possibility that a library exists out there that will format a string akin to what you desire. Use your search engine.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161