Search code examples
javaandroiddatetimemilliseconds

Convert miliseconds to date and time and format by locale


How can I convert milliseconds to a time and date string and format it correctly like the user expects it to be?

I did the following:

((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.DEFAULT,Locale.getDefault())).format(new Date(Long.parseLong(timeInMilliseconds)));

Which seems to work, but I only get the date with this method.

Edit:

To clearify, I need to get the time/date pattern from system somehow to give each user his common format

Now I combined your solutions with mine and it seems to work like I expect.

private String getFormattedDateTimeString(Context context, String timeInMilliseconds) {
    SimpleDateFormat dateInstance = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
    SimpleDateFormat timeInstance = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault());
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.parseLong(timeInMilliseconds));
    String date = dateInstance.format(calendar.getTime());
    String time = timeInstance.format(calendar.getTime());
    return date + " " + time;
}

Why the hell do I get downvotes for this question???


Solution

  • All the other answers are missing the point that the string representation of the date-time needs to be localized.

    Joda-Time

    The Joda-Time 2.3 library makes this work much easier.

    Joda-Time leverages a java.util.Locale to determine proper formatting of a date-time's string representation. The DateTimeFormat class offers an option for "style" pattern as a way of generating a DateTimeFormatter. You specify a two character style pattern. The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be omitted by specifying a style character '-'.

    If you do not specify a Locale or time zone, the JVM's default will be used.

    Locale

    To create a java.util.Locale, you need:

    Example Code

    // Simulate input.
    long millisecondsSinceEpoch = DateTime.now().getMillis();
    
    // Proceed with a 'long' value in hand.
    DateTime dateTimeUtc = new DateTime( millisecondsSinceEpoch, DateTimeZone.UTC );
    
    DateTimeZone timeZone = DateTimeZone.forID( "Asia/Riyadh" );
    DateTime dateTimeRiyadh = dateTimeUtc.withZone( timeZone );
    
    // 'ar' = Arabic, 'SA' = Saudi Arabia.
    java.util.Locale locale = new Locale( "ar", "SA" ); // ( language code, country code );
    DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale ).withZone( timeZone );
    String output = formatter.print( dateTimeUtc );
    

    Dump to console…

    System.out.println( "millisecondsSinceEpoch: " + millisecondsSinceEpoch );
    System.out.println( "dateTimeUtc: " + dateTimeUtc );
    System.out.println( "dateTimeRiyadh: " + dateTimeRiyadh );
    System.out.println( "output: " + output );
    

    When run…

    millisecondsSinceEpoch: 1392583624765
    dateTimeUtc: 2014-02-16T20:47:04.765Z
    dateTimeRiyadh: 2014-02-16T23:47:04.765+03:00
    output: 16 فبراير, 2014 AST 11:47:04 م