Convert miliseconds to date and time and format by locale

Marian Klühspies

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???

Basil Bourque

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 م

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Inconsistent date time format for German locale

分類Dev

Convert GMT date format to only time format

分類Dev

convert text string to date/time format

分類Dev

PHP - Convert this string to another date/time format

分類Dev

how to convert python time.time() to date format for sql insertion

分類Dev

How to format the given time string and convert to date/time object

分類Dev

NSDateFormatter time format depending on Locale

分類Dev

How to display date format according to locale?

分類Dev

Using momentjs to format a date in a specific locale

分類Dev

Date Format Convert

分類Dev

Date Format Convert

分類Dev

convert date format in log

分類Dev

String date format convert to date

分類Dev

Convert time format in PHP

分類Dev

What is this date and time format?

分類Dev

How can I convert date time format string used by C# to the format used by moment.js?

分類Dev

Convert Date String in Format "Mon Day, Year Time am/pm" to POSIXlt format in R?

分類Dev

Convert to XTS by date AND time

分類Dev

Convert date format to string in Pandas

分類Dev

How to convert date into given format?

分類Dev

Excel text to convert date format

分類Dev

How to convert date format in JavaScript?

分類Dev

What is this date time format? How to convert it into a human readable output? Why it returns this output?

分類Dev

How do I convert API supplied date/time string into readable format?

分類Dev

Parse Date and Time in specific format

分類Dev

Convert date to format Day, date month year

分類Dev

Convert utc time into local time of specifc format

分類Dev

How to convert this STRING into time + date

分類Dev

Convert string to date with date and time together in Hive

Related 関連記事

ホットタグ

アーカイブ