1

I'm developing an app which is able to get the timing info of a post.

What i want is to display the time in milli into date to make it user friendly but I need to pay attention to the format of date defined in the settings of the android device.

In case it's dd/mm/yyyy or mm/dd/yyyy or yyyy/dd/mm

when I'm able to determine the format, I just need to get the day and month. year is useless for me.

I have done the code below, but I do not like the fact I'm using substring because if 01/02 become 1/2 It's not working

DateFormat.getDateInstance(DateFormat.SHORT).format(new Date(tweetsCreatedTime)).substring(0,5)

tweetsCreatedTime is in millisec and defined as long

Instead of using local, it's better to get the settings and make sure that even if local is showing EN or US, the user do not change the way it should be displayed.

Thanks

Seb
  • 2,929
  • 4
  • 30
  • 73

3 Answers3

1

java.time

You should be using modern date-classes from the java.time package.

For details, see my Answer from a nearly identical Question, How can I format Date with Locale in Android.

Short example code, changing the misnomer tweetsCreatedTime to tweetMillisecondsSinceEpoch for the long number variable of your given input. Note that while your input is in milliseconds, the java.time classes are actually capable of the much finer resolution of nanoseconds.

Instant instant = Instant.ofEpochMilli( tweetMillisecondsSinceEpoch );  // Number of milliseconds since the POSIX epoch of first moment of 1970 in UTC. 
ZoneId zoneId = ZoneId.of( "Pacific/Auckland" );  // Arbitrary choice of time zone. Crucial it determining the date, as date varies with a new day dawning earlier to the east.
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );  // Apply a time zone.
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM );  // Let java.time translate human language and determine cultural norms in formatting.
formatter = formatter.withLocale( Locale.CANADA_FRENCH );  // Arbitrarily choosing Québec as the Locale. Note that Locale has *nothing* to do with time zone. You could use Chinese locale for a time zone in Finland. 
String output = zdt.format( formatter );  // Generate String representation of our date-time value.

2015-05-23

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

I use DateFormat:

java.text.DateFormat dateFormat = DateFormat.getTimeFormat(context);
String dateString = dateFormat.format(date);

Keep in mind it also returns class named java.text.DateFormat but it's a different class.

klimat
  • 24,711
  • 7
  • 63
  • 70
0

Hi use SimpleDateFormater it allow use to use your own format with any date regardless to device settings here is a custom method that i use all time an even you can set wich local you want it to display the date

 /**
 * Get localized date string (Using given locale)
 *
 * @param dateString Date string
 * @param locale     Desired locale
 * @return Formatted localized date string
 */
public static String formatLocalized(String dateString, Locale locale) {
    Date date = formatDate(dateString, locale);
    SimpleDateFormat iso8601Format = new SimpleDateFormat("d MMM yyyy", locale);
    iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return iso8601Format.format(date);

}

define local using Locale.ENGLISH|Locale.FRENCH ...

thunder413
  • 585
  • 3
  • 10
  • thanks but you force the format in the SimpleDateFormat. Is there a way to get this format from the settings of the device – Seb Apr 27 '16 at 01:55
  • I think what you can get from the device is witch local is used and that value can be found using Locale.getDefault(); but the way a date is display is up to you if the locale is french display it using the french format or if the locale is english (most likely ;) display the date using the english format – thunder413 Apr 27 '16 at 01:59