0

I have a date string with this format 2019-01-01T18:46:19 and I want to convert it to this format 06:46 pm AEST 01/01/19 with abbreviated Australian time zone for example. How can I do this in Java?

y.allam
  • 1,446
  • 13
  • 24
  • 1
    Did you search before asking? This may not be a very exact duplicate of an existing question. Still I think you could have gor a lot of help from previous questions and answers, like the last half of [this answer](https://stackoverflow.com/a/45827312/5772882). I downvote for not showing any effort in search, research and trying some code. – Ole V.V. Jun 09 '18 at 07:18
  • Yes, i have searched but didn't find an obvious solution. At least my question will help anyone in the future and save him some time – y.allam Jun 09 '18 at 14:19

1 Answers1

2

You can use the newer java.time API for this task:

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZOneId;
import java.time.format.DateTimeFormatter;

ZoneId zone = ZoneId.of("Australia/Sydney");
LocalDateTime ldt = LocalDateTime.parse("2019-01-01T18:46:19") ;
ZonedDateTime zdt = ZonedDateTime.of(ldt, zone);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm a z dd/MM/yy");
System.out.println(dtf.format(zdt));

Output:

06:46 PM AEDT 01/01/19

Note: your format in your question is ambiguous as to whether you want day/month/year or the American month/day/year but I will assume that Australia follows the UK in preferring day/month/year.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • 1
    FYI, much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jun 09 '18 at 04:56
  • I suggest letting [*java.time* automatically localize](https://docs.oracle.com/javase/10/docs/api/java/time/format/DateTimeFormatter.html#ofLocalizedDateTime(java.time.format.FormatStyle)) rather than hard-coding a particular format. `DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( new Locale( "en" , "AU" ) )` – Basil Bourque Jun 09 '18 at 04:59
  • @BasilBourque Unfortunately `FormatStyle.MEDIUM` doesn't give the same format that OP wants. I agree that normally hardcoding a particular format might not be optimal, but in this case OP has a specific output format in the question. Also, OP hasn't said anything about needing the code to run under outdated versions of the JVM, but it is worth noting that the API has been backported. – David Conrad Jun 09 '18 at 14:42
  • @DavidConrad this is the output i'm getting: `06:46 PM GMT+11:00 01/01/19` – y.allam Jun 09 '18 at 15:39
  • 1
    @y.allam That sounds like you are using GMT+11:00 as “time zone” rather than Australia/Sydney? Please specify time zone in the *region/city* format and I trust that everything will be fine. If you are using the device time zone setting, make sure to set it to a real time zone rather than an offset from GMT, – Ole V.V. Jun 09 '18 at 19:21
  • @DavidConrad My first comment about back-porting was in regards to the `Android` tag on the Question. Android lacks a *java.time* implementation before version 26. My second comment was not a criticism of your answer, but was “FYI” aimed at the reader and at the original posting author. – Basil Bourque Jun 10 '18 at 03:35
  • 1
    @y.allam What JDK are you using? I get `06:46 PM AEST 01/01/19` with Oracle Hotspot JDK versions 1.8.0_172, 10.0.1, and 11-ea+17. – David Conrad Jun 10 '18 at 14:46
  • @DavidConrad mine is `Java HotSpot, 1.8.0_74-b02`, FYI, i'm on Android and using the *ThreeTenABP* library – y.allam Jun 11 '18 at 13:56
  • @y.allam Hmm. You can try changing the `z` to `zz` or `zzz` or `zzzz` and see if that helps. – David Conrad Jun 11 '18 at 22:13