1
"2021-09-17 11:48:06 UTC"

I want to parse the following string and create a LocalDateTime object or an Instant

I know you can write something like this

String dateTime = "2021-09-17 11:48:06 UTC";
LocalDateTime dt = LocalDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

How do I deal with the UTC part in the string?

Tendai
  • 71
  • 6
  • No. I know how to format strings like how they are doing it in the post you referred me to. I want to know how do I deal with that `UTC` part? – Tendai Sep 17 '21 at 12:16
  • "2021-09-17 11:48:06 UTC" isn't a local date time: it's a date time, because it has a time zone. Parse it to a DateTime, then use `toLocalDateTime()`. – Andy Turner Sep 17 '21 at 12:24
  • Oh ok, that's different. Thanks you for edit. And yes, such as says by Andy, you have to use global datetime instead of local – Elikill58 Sep 17 '21 at 12:26

2 Answers2

5

Never hardcode the standard timezone text like UTC, GMT etc.

Never hardcode the standard timezone text like UTC, GMT etc. which DateTimeFormatter is already capable of handling in the best way.

Parse the given Date-Time string using the pattern, uuuu-MM-dd HH:mm:ss VV into a TemporalAccessor from which you can get the Instant as well as the LocalDateTime.

Demo:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-09-17 11:48:06 UTC";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss VV", Locale.ENGLISH);
        TemporalAccessor temporalAccessor = dtf.parse(strDateTime);

        Instant instant = Instant.from(temporalAccessor);
        LocalDateTime ldt = LocalDateTime.from(temporalAccessor);

        System.out.println(instant);
        System.out.println(ldt);
    }
}

Output:

2021-09-17T11:48:06Z
2021-09-17T11:48:06

ONLINE DEMO

Alternatively:

Parse the given Date-Time string using the pattern, uuuu-MM-dd HH:mm:ss VV into a ZonedDateTime from which you can get the Instant as well as the LocalDateTime.

Demo:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-09-17 11:48:06 UTC";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss VV", Locale.ENGLISH);

        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
        Instant instant = Instant.from(zdt);
        LocalDateTime ldt = zdt.toLocalDateTime();

        System.out.println(zdt);
        System.out.println(instant);
        System.out.println(ldt);
    }
}

Output:

2021-09-17T11:48:06Z[UTC]
2021-09-17T11:48:06Z
2021-09-17T11:48:06

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. 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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
3

"2021-09-17 11:48:06 UTC" isn't a local date time: it's a date time, because it has a time zone. And because your time has a time zone, it doesn't match your pattern, which doesn't.

If your time strings always end with exactly "UTC", you can make that a literal in the pattern:

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'");

If you need to handle other time zones than UTC, you can use z:

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

but note that this is for parsing a zoned date time; from that, you can extract the local date time:

ZonedDateTime zdt = ZonedDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
LocalDateTime ldt =z dt.toLocalDateTime();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243