0

Hi I am not able to understand how to convert
"2020-03-11 16:27:31" to "2020-03-11T16:27:31+00:00".

I have no idea about it.

The date is given in the format "2020-03-11 16:27:31"

I want above date in this format "2020-03-11T16:27:31+00:00"

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 5
    If you just talk about parsing, please use the same date, not different ones, this is confusing – azro Mar 19 '20 at 10:47
  • Hello and welcome to StackOverflow! You seem to be under the impression that StackOverflow is a site where you post a problem and get some code in return. This is in fact not the case. Your question will most likely be closed or even deleted shortly. To prevent this from happening in the future, please [take the tour](https://stackoverflow.com/tour) and [take a look at the help center](https://stackoverflow.com/help). In particular, [make yourself famlilar as to what is regarded as on-topic around here](https://stackoverflow.com/help/on-topic) – azro Mar 19 '20 at 10:48
  • 4
    And if there shouldn't be a space before or after T, please remove that from the post. It's not clear why you put it in there. – Jon Skeet Mar 19 '20 at 10:48
  • Hint One is Date Object and another one is LocalDate Object – Silverfang Mar 19 '20 at 10:52
  • What is the last sentence of your question supposed to mean? Is a space before and after the T desired or not? Currently, you are stating there were no spaces but providing a `String` with those spaces... very confusing – deHaar Mar 19 '20 at 11:05

2 Answers2

1

Here the way you can do what you need (if you're using java8):

    String date = "2020-03-11 16:27:31";
    String pattern = "yyyy-MM-dd HH:mm:ss";
    // formatter with spaces before and after 'T'
    DateTimeFormatter f0 = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(DateTimeFormatter.ISO_LOCAL_DATE)
            .appendLiteral(' ')
            .appendLiteral('T')
            .appendLiteral(' ')
            .append(DateTimeFormatter.ISO_LOCAL_TIME)
            .optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
            .toFormatter();

    // formatter without spaces before and after 'T'
    DateTimeFormatter f1 = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
            .optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
            .toFormatter();
    OffsetDateTime offsetDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern))
            .atOffset(ZoneOffset.ofHoursMinutes(0, 0));
    String result = offsetDateTime.format(f1);

But I suggest you to read java.timi manual as @deHaar recomended.

DontPanic
  • 1,327
  • 1
  • 13
  • 20
0

If you are using Java 8 or higher, then use java.time (read about why and how to use it here) to parse the String with a suitable DateTimeFormatter to a LocalDateTime, create an OffsetDateTime of that by adding a ZoneOffset of +00:00 and output it with a different DateTimeFormatter.

You can prepare desired formats by adding desired patterns to the DateTimeFormatter calling its method ofPattern(String pattern).

In case you want to see an example, please show us what you have tried so far.

deHaar
  • 17,687
  • 10
  • 38
  • 51