0

I googled about this issue but I haven't found solution to this particular problem: can I convert UTC time (which is from server) to local date and time format, without need to specify locale nor formatting? Every example I found has specified formatting style, which can be different for certain countries. For example, in US is MM/dd/yyyy and AM/PM for time, but in Europe is usually dd.MM.yyyy and 24 hour format).

I added an answer to my own question bellow.

Nikola Zarić
  • 865
  • 1
  • 9
  • 28
  • are you getting the time in milliseconds from the server? You can parse that into date class – Mariano Zorrilla Nov 13 '15 at 15:10
  • Will the formatting be right automatically, or do I have to specify some additional parameters (like locale...)? – Nikola Zarić Nov 13 '15 at 15:13
  • a simple way to perform that : store always UTC dates in a defined format exemple 2015-12-12T12:12:12.500Z , and present them as local dates with javascript moment.js. So that you don't bother with formatting style that are for presentation purpose only. – jeorfevre Nov 13 '15 at 15:39
  • **For future visitors of this page**: The legacy date-time API (`java.util` date-time types and their formatting type, `SimpleDateFormat`) is outdated and error-prone. It is recommended to stop using it completely and switch to `java.time`, the [modern date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). You can check [this answer](https://stackoverflow.com/a/67449876/10819573) that uses the `java.time` API. – Arvind Kumar Avinash May 09 '21 at 17:57

1 Answers1

0

I actually found solution for Android:

android.text.format.DateFormat.getTimeFormat(context).format(new java.util.Date(utcDateTime))

So, line above will return String value of formated time, based on user's preference of 24 hour format.

android.text.format.DateFormat.getMediumDateFormat(context).format(new java.util.Date(utcDateTime));

This line will also return String containig formatted date based on user's language preference.

Nikola Zarić
  • 865
  • 1
  • 9
  • 28