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"
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"
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.
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.