Home > other >  Parse Timestamp in Java
Parse Timestamp in Java

Time:09-17

I get a timestamp in the format "20210908094049.884Z". This is the last modify timestamp from an LDAP object. I use Spring Boot Ldap. I have no clue how to parse this String in a Datetime like dd.MM.yyyy HH:mm.

Can anyone help me please?

CodePudding user response:

Here is an example:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // Creating new simple date formatter with the format you've given
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");

        // Defining the input date
        String inputDate = "20210908094049.884Z";

        // Parsing the date, catching the parse exception if date is malformatted
        Date date = null;
        try {
            // Date ends on a Z, we remove this Z (Z is for timezone UTC  0:00)
            date = format.parse(inputDate.replace("Z", ""));
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Giving following output:

Wed Sep 08 09:40:49 CEST 2021

Edit: Here another even better solution from Ole V.V.

import java.time.Instant;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        Instant instant = DateTimeFormatter
                // Defining pattern to parse
                .ofPattern("yyyyMMddHHmmss.SSSXX")
                // Defining input to parse with pattern
                .parse("20210908094049.884Z", Instant::from);
        System.out.println(instant);
    }
}

Output is an instant with value:

2021-09-08T09:40:49.884Z

CodePudding user response:

java.time

I recommend that you use java.time, the modern Java date and time API, for your work with timestamps.

The LDAP timestamp format has a number of allowed variations (see the link at the bottom). The following formatter takes many of them into account, not all of them.

private static final DateTimeFormatter LDAP_PARSER = new DateTimeFormatterBuilder()
        .appendPattern("uuuuMMddHHmmss")
        .optionalStart()
        .appendPattern("[.][,]")
        .appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, false)
        .optionalEnd()
        .appendPattern("[XX][X]")
        .toFormatter(Locale.ROOT);

With this formatter we may for example parse your string into an OffsetDateTime:

    String ldapTimestampString = "20210908094049.884Z";
    OffsetDateTime timestamp = OffsetDateTime.parse(ldapTimestampString, LDAP_PARSER);
    System.out.println(timestamp);

Output is:

2021-09-08T09:40:49.884Z

Formatting

To convert the timestamp to a string containing date and time you need to decide on a time zone for that since it is never the same date nor the same time in all time zones.

Use this formatter:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

Then do:

    ZoneId zone = ZoneId.of("Pacific/Tarawa");
    ZonedDateTime dateTime = timestamp.atZoneSameInstant(zone);
    String formattedDateTime = dateTime.format(FORMATTER);
    System.out.println(formattedDateTime);

08.09.2021 21:40

Links

  • Related