0

I am having a difficult time formatting the date/time to include "-07:00" as the time zone. An ideal example is "2018-06-19T14:45:10-07:00".

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        Calendar cal= Calendar.getInstance();
        String dateTime = formatter.format(cal.getTime());

The result of this is something like

"2018-06-19T14:45:10"

. I want to have -07:00 at the end of it. I have also tried to change the formatter to include the timezone

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());

The result of this is something like

"2018-06-19T14:45:10Z"

What am I doing wrong or missing?

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • Possible duplicate of [Formatting ISO 8601 in Java 7](https://stackoverflow.com/questions/46300515/formatting-iso-8601-in-java-7) – Ole V.V. Jun 20 '18 at 05:22
  • Consider throwing the classes `Calendar`, `TimeZone` and `SimpleDateFormat` away. They are long outmoded and poorly designed and `SimpleDateFormat` in particular notoriously troublesome. Instead you may add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jun 20 '18 at 05:25
  • @OleV.V. ThreeTenABP works better in your opinion? How is it for formatting though? – portfoliobuilder Jun 20 '18 at 16:47

1 Answers1

2

You do not need the quotes around Z

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());// 2018-06-19T22:38:28+0000
Mostafa Gazar
  • 2,487
  • 18
  • 23