Convert UTC datetime to local datetime using java.time - java

I have a UTC date-time like this (a String): 2022-11-22T17:15:00
And a ZoneID like this: "America/Tijuana"
Using java.time API, I want to get the actual datetime for that zone, which is: 2022-11-22T09:15:00 (the time is 09:15 instead of 17:15)
ZonedDateTime.toLocalDateTime() returns: 2022-11-22T17:15
ZonedDateTime.toString() returns:
2022-11-22T17:15-08:00[America/Tijuana]
None of the above gives me what I'm looking for.
This is my code:
ZoneId zonaID = ZoneId.of('America/Tijuana');
CharSequence dateUTC = "2022-11-22T17:15:00";
LocalDateTime dateTimeL = LocalDateTime.parse(dateUTC);
ZonedDateTime myZDT = ZonedDateTime.now();
ZonedDateTime myZDTFinal = myZDT.of(dateTimeL, zonaID);
System.out.println("using toLocalDateTime: " + myZDTFinal.toLocalDateTime());
System.out.println("using toString: " + myZDTFinal.toString());
I know that this might be a duplicated question but there's so many questions about date-times and I just haven't been able to figure out this.
Any help will be really appreciated.

You have to convert your date to UTC, then convert the convert this zone to your expected zone using withZoneSameInstant like this:
ZonedDateTime toUTCZone = ZonedDateTime.of(dateTimeL, ZoneOffset.UTC);
ZonedDateTime myZDTFinal = toUTCZone.withZoneSameInstant(zonaID);
Output
2022-11-22T09:15-08:00[America/Tijuana]

There can be many ways to achieve the result. A simple approach would be
Parse the given string into LocalDateTime.
Convert it into an OffsetDateTime at UTC using LocalDateTime#atOffset.
Use OffsetDateTime#atZoneSameInstant to convert the resulting OffsetDateTime into a ZonedDateTime at ZoneId.of("America/Tijuana").
Get LocalDateTime out of the resulting ZonedDateTime by using ZonedDateTime#toLocalDateTime.
If required, format this LocalDateTime into the desired string.
LocalDateTime
.parse("2022-11-22T17:15:00") // Parse the given date-time string into LocalDateTime
.atOffset(ZoneOffset.UTC) // Convert it into a ZonedDateTime at UTC
.atZoneSameInstant(ZoneId.of("America/Tijuana")) // Convert the result into a ZonedDateTime at another time-zome
.toLocalDateTime() // Get the LocalDateTime out of the ZonedDateTime
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", Locale.ENGLISH))); // If required
Demo:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDateTime ldtInTijuana = LocalDateTime.parse("2022-11-22T17:15:00")
.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(ZoneId.of("America/Tijuana"))
.toLocalDateTime();
System.out.println(ldtInTijuana);
// Custom format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
String formatted = ldtInTijuana.format(formatter);
System.out.println(formatted);
}
}
Output:
2022-11-22T09:15
2022-11-22T09:15:00
Note that LocalDateTime#toString removes second and fraction-of-second values if they are zero. Suppose you want to keep them (as you have posted in your question), you can use a DateTimeFormatter as shown above.
An alternate approach:
Alternatively, you can append Z at the end of your ISO 8601 formatted date-time string to enable Instant to parse it and then convert the Instant into a ZonedDateTime corresponding to the ZoneId.of("America/Tijuana") by using Instant#atZone. The symbol, Z refers to UTC in a date-time string.
The rest of the steps will remain the same.
Demo:
public class Main {
public static void main(String[] args) {
String text = "2022-11-22T17:15:00";
text = text + "Z"; // Z refers to UTC
Instant instant = Instant.parse(text);
LocalDateTime ldt = instant.atZone(ZoneId.of("America/Tijuana")).toLocalDateTime();
System.out.println(ldt);
}
}
Output:
2022-11-22T09:15
Learn more about the modern Date-Time API from Trail: Date Time.

Related

convert string ("2022-12-23T07:20:00" ) time into ZonedDateTime

i was trying to convet string time into ZonedDateTime but not comes up with solution . This is the string format of time "2022-12-23T07:20:00"
i have tried this approach
String stringDate = "2022-12-23T07:20:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
ZonedDateTime ztdOfDateOfPurchase = ZonedDateTime.parse(stringDate , dateTimeFormatter);
this error is coming=> java.time.format.DateTimeParseException: Text '2022-12-23T07:20:00' could not be parsed at index 19
Simply parse your date-time string using LocalDateTime#parse and add the applicable ZoneId to get the ZonedDateTime.
Note that java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format (e.g. your date-time string, 2022-12-23T07:20:00).
Demo:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.parse("2022-12-23T07:20:00");
// Replace ZoneId.systemDefault() with the applicable zone ID e.g.
// ZoneId.of("America/New_York")
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
System.out.println(zdt);
// Alternatively,
zdt = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdt);
}
}
Output in my timezone:
2022-12-23T07:20Z[Europe/London]
2022-12-23T07:20Z[Europe/London]
Learn more about the modern Date-Time API from Trail: Date Time.

Parse a String to LocaDateTime in Java

Consider a String "2022-03-23 21:06:29.4933333 +00:00".
How do I parse the above DateTimeOffset String to LocalDateTime in Java?
I tried with the following DateTimeFormatter but the format seems to be incorrect:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss\[.nnnnnnn\] \[+|-\]hh:mm\]");
LocalDateTime dateTime = LocalDateTime.parse(timestamp, formatter)
First, start by having the JavDocs for DateTimeFormatter at hand, this is going to really help determine which specifiers you need
The first thing to do is parse the text into a ZonedDateTime, LocalDateTime won't parse a input value with a time zone (AFAIK), you "might" be able to force it, but what's the point?
String text = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS z");
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);
This prints...
2022-03-23T21:06:29.493333300Z
Now you could use ZonedDateTime#toLocalDateTime, but this won't take into account the current time zone of the user/computer.
If you need to convert the ZonedDateTime to LocalDateTime, it's best to do so in away which will translate the time (and date if required) to best represent the time within the current time zone (okay, I was confused typing it)
For example, converting the input value into my current time zone (+11 hours) would look like this...
ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime currentZDT = zdt.withZoneSameInstant(currentZone);
System.out.println(currentZDT);
LocalDateTime ldt = currentZDT.toLocalDateTime();
System.out.println(ldt);
which will print...
2022-03-24T08:06:29.493333300+11:00[Australia/Melbourne]
2022-03-24T08:06:29.493333300
This means that at 9:06pm on the 23rd March in Grinch (GMT), it was 8:06am on the 24th March where I live.
Now you can use different ZoneIds to convert to a TimeZone which is not the current computers TimeZone, but I'll leave that up to you to experiment with (for example, I used Convert ZonedDateTime to LocalDateTime at time zone to base my example on)
You need create custom DateTimeFormatter:
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public class Main {
public static void main(String args[]){
String dateString = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral(' ')
.appendOffsetId()
.toFormatter();
//In case of OffSet matter, retaining the instant
LocalDateTime localDateTimeSavePointOfTime = OffsetDateTime.parse(dateString, formatter).withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalDateTime();
//In case OffSet does not matter we can skip it
LocalDateTime localDateTimeSkipOffSet = LocalDateTime.parse(dateString, formatter);
}
}

java convert date time string to zulu date time format

I need to convert this date "2021-09-27 16:32:36" into zulu format like this "yyyy-MM-dd'T'HH:mm:ss.SSS'Z".
tl;dr
"2021-09-27 16:32:36"
.replace( " " , "T" )
.concat( ".Z" )
2021-09-27T16:32:36Z
A fractional second of zero can be omitted under ISO 8601.
String manipulation
Usually I would recommend using java.time classes. But in your case the obvious solution is simple string manipulation, as suggested by Andy Turner.
String iso8601 = "2021-09-27 16:32:36".replace( " " , "T" ).concat( ".000Z" ) ;
I would recommend dropping the zero fractional second. The string would still comply with ISO 8601.
String iso8601 = "2021-09-27 16:32:36".replace( " " , "T" ).concat( "Z" ) ;
The resulting string 2021-09-27T16:32:36Z represents a moment as seen with an offset of zero hours-minutes-seconds ahead/behind UTC.
If you need to do further work, parse that as an Instant. Example: Instant.parse( iso8601 )
Time zone is crucial
The Zulu time that you are asking for defines a definite and precise point in time. The string you have got does not. If we don’t know its time zone, it may denote times in a span of more than 24 hours.
For this answer I am assuming that the time is in US Central time (America/Chicago).
The format you are asking for is ISO 8601.
java.time
Like the other answers I am recommending java.time, the modern Java date and time API, for all of your date and time work. It has good support for ISO 8601.
I am using this formatter for parsing your string:
private static final DateTimeFormatter PARSER
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ROOT);
Now the work goes like this:
ZoneId zone = ZoneId.of("America/Chicago");
String dateString = "2021-09-27 16:32:36";
ZonedDateTime dateTime = LocalDateTime.parse(dateString, PARSER).atZone(zone);
String isoZuluString = dateTime.withZoneSameInstant(ZoneOffset.UTC).toString();
System.out.println(isoZuluString);
Output is:
2021-09-27T21:32:36Z
It’s in ISO 8601 format and in Zulu time, so as far as I am concerned, we’re done. The milliseconds you asked for are not there. They were not in the original string either, and according to the ISO 8601 format they are not mandatory, so you should be fine. Only if you encounter a particularly picky service that requires a fraction of second in the string even when it is .000, use a formatter for producing it:
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendPattern("'T'HH:mm:ss.SSSX")
.toFormatter(Locale.ROOT);
The formatter could have been written with a format pattern alone. I took this opportunity for demonstrating that we may reuse built-in formatters in our own to make it easier and safer to get ISO 8601 right. Format like this:
String isoZuluString = dateTime.withZoneSameInstant(ZoneOffset.UTC)
.format(FORMATTER);
2021-09-27T21:32:36.000Z
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
Example with printing on the console :
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ZuluZulu {
public static void zuluFormatter(String localDateTime) {
String pattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
String s = localDateTime;
LocalDateTime dt = LocalDateTime.parse(s, formatter);
System.out.println("dateTime Simple Format without T = " + dt.format(formatter));
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS'Z'");
System.out.println("DateTime Zulu format = " + dt.format(formatter2));
}
public static void main(String[] args) {
zuluFormatter("2021-09-27 16:32:36");
}
}
Output :
dateTime Simple Format without T = 2021-09-27 16:32:36
DateTime Zulu format = 2021-09-27 16:32:36.000Z
this example is exactly what you need without printing on the console :
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ZuluZulu {
public static String zuluFormatter(String localDateTime) {
String pattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(pattern);
String s = localDateTime;
LocalDateTime dt = LocalDateTime.parse(s, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-
MM-dd HH:mm:ss.SSS'Z'");
return dt.format(formatter2);
}
public static void main(String[] args) {
System.out.println(zuluFormatter("2021-09-27 16:32:36"));
}
}

Parsing LocalDate to ZonedDateTime in correct format

Given:
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDate date = LocalDate.parse(dateString, DATE_TIME_FORMATTER);
ZonedDateTime zonedDateTime = date.atStartOfDay((ZoneOffset.UTC));
System.out.println(zonedDateTime);
}
And output:
2018-07-30T00:00Z
...what is the pattern to print seconds? Stupid question no doubt but driving me a little nuts
I need:
2018-07-30T00:00:00Z
I changed java.time.LocalDate to java.time.LocalDateTime, you need it if you want to show also the seconds.
package com.test;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormatter {
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
ZonedDateTime zonedDateTime = date.atZone(ZoneOffset.UTC);
System.out.println(zonedDateTime);
}
}
Output is:
2018-07-30T13:36:17.820Z
LocalDate will keep just date. You need to parse LocalDateTime and convert to ZonedDateTime and you will have seconds as you expect.
var dateString = "2018-07-30T13:36:17.820";
var format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
var localDate = LocalDateTime.parse(dateString, format);
var zone = ZoneId.of( "America/Montreal" );
var zonedDateTime = localDate.atZone(zone);
System.out.println(zonedDateTime);
You will have to go a few steps:
parse the String to a LocalDateTime because it contains date and time of day
extract the date only
create a ZonedDateTime out of that by adding the start of day (LocalTime.MIN = 00:00:00) and a ZoneOffset.UTC
This code may do:
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
// parse a LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(dateString);
// extract the date part
LocalDate localDate = localDateTime.toLocalDate();
// make it a ZonedDateTime by applying a ZoneId
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, LocalTime.MIN, ZoneOffset.UTC);
// print the result
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
Output is
2018-07-30T00:00:00Z
There are several ways to do it, this is just one of them and it just slightly differs from most of the other answers (and comments :-) ).
tl;dr
You have used the wrong things in the wrong places.
You do not need a DateTimeFormatter explicitly in order to parse 2018-07-30T13:36:17.820 because it's already in ISO 8601 format which is also the default format used by LocalDateTime#parse. Moreover, this string has date and time instead of just date; therefore, it makes more sense to parse it into LocalDateTime instead of LocalDate. You can always get LocalDate from LocalDateTime using LocalDateTime#toLocalDate.
The ZonedDateTime#toString uses the LocalDateTime#toString which in turn uses LocalTime#toString for the time part which omits second and fraction-of-second if they are zero. If you need a string with zero second and fraction-of-second, you will need to use a DateTimeFormatter.
Demo:
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
String dateString = "2018-07-30T13:36:17.820";
LocalDateTime localDateTime = LocalDateTime.parse(dateString);// You do not need a DateTimeFormatter here
ZonedDateTime zonedDateTime = localDateTime.toLocalDate().atStartOfDay(ZoneOffset.UTC);
// Print zonedDateTime.toString()
System.out.println(zonedDateTime);
// Custom format
final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
System.out.println(DATE_TIME_FORMATTER.format(zonedDateTime));
}
}
Output:
2018-07-30T00:00Z
2018-07-30T00:00:00.000
Learn more about the modern date-time API from Trail: Date Time.

How to parse UTC timestamp of format yyyy-MM-ddTHH:mm:ss.SSS+ZZZZ

I have timestamp as 2020-12-03T05:35:59.398+0000 in String format being recieved in a streaming batch.
I want only 2020-12-03 05:35:59 as java.sql.Timestamp instance in order to be able to compare it with other Timestamp instance.
Getting following error with Timestamp.valueOf() function:
Exception in thread "main" java.time.format.DateTimeParseException : Text '2020-12-03T05:35:59.398+0000' could not be parsed at index 23
I tried the answer given here , and conversion did happen but the time was changed to 2020-12-03 11:05:59
I have tried changing between the formats given here but still no solution.
Is there even a format for timestamp with wierd + in between 398+0000?
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work.
DateTimeFormatter isoFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx");
DateTimeFormatter sqlTimestampFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
String aTimestampString = "2020-12-03T05:35:59.398+0000";
String anotherTimestampString = "2020-12-04 06:43:58.556385";
Instant anInstant = isoFormatter.parse(aTimestampString, Instant::from);
Instant anotherInstant = LocalDateTime.parse(anotherTimestampString, sqlTimestampFormatter)
.atOffset(ZoneOffset.UTC)
.toInstant();
if (anInstant.isBefore(anotherInstant)) {
System.out.println(aTimestampString + " is earlier");
} else {
System.out.println(anotherTimestampString + " is earlier");
}
Output from this example is:
2020-12-03T05:35:59.398+0000 is earlier
The +0000 in the former string above is an offset from UTC — an offset of 00 hours 00 minutes. Since it is zero, we know that the time is in UTC. I don’t know the time zone or UTC offset of the other string. You need to know, or you will get incorrect results. In the code above I have assumed that the other string is in UTC too.
Don’t use Timestamp
I tried the answer given here, and conversion did happen but the time
was changed to 2020-12-03 11:05:59
This is how confusing the Timestamp class is. You got the correct timestamp value. What happens when you print the Timestamp object, is that you are (implicitly or explicitly) calling its toString method. Timestamp.toString() confusingly uses the default time zone of your JVM for rendering the string. So if your timestamp is equal to 2020-12-03T05:35:59.398 UTC and your time zone is, say, Asia/Kolkata, then time is converted to Asia/Kolkata time zone and the string 2020-12-03 11:05:59 is returned and printed.
You have nothing good to use the old-fashioned java.sql.Timestamp class for. It was originally meant for transferring timestamp values with and without time zone to and from SQL databases. Since JDBC 4.2 we prefer OffsetDateTime, Instant and LocalDateTime for that purpose. So just forget about the Timestamp class.
Link
Oracle tutorial: Date Time explaining how to use java.time.
Is there even a format for timestamp with wierd + in between 398+0000?
The 398 part is fraction-of-second (millisecond) while the +0000 part is the zone offset part.
You can parse 2020-12-03T05:35:59.398+0000 into an OffsetDateTime using the format pattern, uuuu-MM-dd'T'HH:mm:ss.SSSX.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String text = "2020-12-03T05:35:59.398+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
OffsetDateTime odt = OffsetDateTime.parse(text, formatter);
System.out.println(odt);
}
}
Output:
2020-12-03T05:35:59.398Z
Check the DateTimeFormatter documentation page to learn more about the letters used for formatting.
You can use isBefore and isAfter functions of OffsetDateTime to compare its two instances.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String text = "2020-12-03T05:35:59.398+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
OffsetDateTime odt = OffsetDateTime.parse(text, formatter);
OffsetDateTime odtNow = OffsetDateTime.now();
System.out.println(odtNow.isBefore(odt));
System.out.println(odtNow.isAfter(odt));
}
}
Output:
false
true
Learn more about the modern date-time API at Trail: Date Time. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Since java.sql.Timestamp extends java.util.Date, it is recommended to stop using that as well. However, for any reason, if you still want to use conversion between the modern and the legacy date-time API, use Instant as the bridge.
import java.sql.Timestamp;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String text = "2020-12-03T05:35:59.398+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
OffsetDateTime odt = OffsetDateTime.parse(text, formatter);
Instant instant = odt.toInstant();
Timestamp timestamp = new Timestamp(instant.toEpochMilli());
System.out.println(timestamp);
}
}
Output:
2020-12-03 05:35:59.398
You can use a custom DateFormatter for non-standard formats. Here is a working example for your use case.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import static java.time.temporal.ChronoField.*;
public class Main {
private static final DateTimeFormatter INPUT_NON_STANDARD_FORMAT;
static {
INPUT_NON_STANDARD_FORMAT =
new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendLiteral('.')
.appendValue(MILLI_OF_SECOND, 3)
.appendLiteral("+0000")
.toFormatter();
}
public static void main(String[] args) {
String timestamp = "2020-12-03T05:35:59.398+0000";
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, INPUT_NON_STANDARD_FORMAT);
System.out.println(localDateTime.format(dateTimeFormatter));
}
}
Output
2020-12-03 05:35:59

Categories

Resources