0

I noticed that java.util.Date class on android gives a different date format for different devices which is giving me lots of problems when running on different devices. for example Huawei devices date format is Mon Jan 01 00:00:00 GMT+02:00 2018 and where as Samsung format is Mon Jan 01 00:00:00 SAST 2018. But I don't have other devices to test and get the different formats so I was wondering is there a list of different formats we would get from different devices that I don't have. Or is there another way to format these dates like these

convert java.util.Date to 19 Apr 2018

convert java.util.Date to 2018-04-19

convert java.util.Date to 2018-05-10 08:52:21

convert 2018-05-10 08:52:21 to 08:52 10 May 2018

Richard K Maleho
  • 436
  • 6
  • 16
  • Try using the incredible *Jodatime* library – Eenvincible Sep 11 '18 at 09:40
  • 1
    Datetime format depends on the culture of the device you are using. It is considered a bad habit to force the format of a date, and give the exact same output for every device. Why would you want to have those exact formats for every device ? (even arabic or hebrew, who don't have the same system at all) ? – Arthur Attout Sep 11 '18 at 09:42
  • @Eenvincible I will check that lib out – Richard K Maleho Sep 11 '18 at 09:43
  • 1
    You can use java's SimpleDateFormat to format the date to a pattern you set. In the other cases what you get depends on the current Locale the device is set to. You can check this tutorial for examples http://tutorials.jenkov.com/java-date-time/parsing-formatting-dates.html – Veselin Todorov Sep 11 '18 at 09:45
  • @ArthurAttout the app I'm developing won't be global and will only be in English, those formats I need will be displayed to the user and will also be saved in the mysql database that's why I need it to be precise – Richard K Maleho Sep 11 '18 at 09:46
  • @VeselinTodorov thank You I will look in to it – Richard K Maleho Sep 11 '18 at 09:47
  • Should be able to specify the LOCALE – Eenvincible Sep 11 '18 at 09:47
  • Just convert date to the format, which you need. Use `DateFormat`/`SimpleDateFormat` – grabarz121 Sep 11 '18 at 09:58
  • By the way, if you save a time in your database, use the datetime formats that are supported by your database, or in seconds from the UNIX epoch, not with a formatted string – Arthur Attout Sep 11 '18 at 10:19
  • Joda-Time is a nice improvement, however, if you consider accepting an external library for your date and time work, my warm recommendation is [java.time](https://docs.oracle.com/javase/tutorial/datetime/) through [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP). java.time an be considered the successor of Joda-Time. Joda-Time is now in maintenance mode. Also @Eenvincible – Ole V.V. Sep 11 '18 at 10:31
  • The part that differs, `GMT+02:00` vs. `SAST`, is the time zone setting of the JVM and probably of the device. This means that the user has asked to have the date presented in this time zone. Shouldn’t you respect that? If you know better in which time zone you want to present the time, you should, of course, and it’s easy when you know how. – Ole V.V. Sep 11 '18 at 10:33
  • @RichardKMaleho I recommend you keep the format used for saving into the database apart from the format used for presentation to the user. In MySQL use its `date` and `datetime` datatypes, for example, and don’t worry about which format is uses internally. – Ole V.V. Sep 11 '18 at 10:38
  • Possible duplicate of [Printing out datetime in a specific format in Java?](https://stackoverflow.com/questions/40715424/printing-out-datetime-in-a-specific-format-in-java). If this doesn’t satisfy your needs, use your search engine and find more and even better answers. – Ole V.V. Sep 11 '18 at 13:12
  • @OleV.V. I am using `date` data type in my database that's why I can't save it as the format as `java.util.Date` gives. I have to first convert it to database `Date` format – Richard K Maleho Sep 11 '18 at 13:22
  • 1
    @OleV.V. my app is'nt being used to display current time. I use the dates because the user will be able to select any future date so time zones don't matter in this case – Richard K Maleho Sep 11 '18 at 13:24
  • No you don’t have to. If using API level 26 or higher, use a `LocalDate` for your date and like `yourPreparedStatement.setObject(5, yourLocalDate)` to save the date. Similar for date and time. On level 25 and under you will need an old-fashioned `java.sql.Date` and `yourPreparedStatement.setDate(5, yourJavaSqlDate)`, but it’s still better than saving a string to the database column of type `date`. – Ole V.V. Sep 11 '18 at 13:25
  • For formatting for presentation [my answer here](https://stackoverflow.com/a/46112643/5772882) may get you started, but as I said, there are many others. – Ole V.V. Sep 11 '18 at 13:29

0 Answers0