Shek
Shek

Reputation: 1613

Trying to convert Date and Time String to ISO Date and Time String in Java 8

I am getting following String for date and time from the Database 20180309220001 I am trying to convert this DateTime String to the following pattern- Target Pattern 2016-10-06T00:00:00-04:00

To achieve the above pattern I have written following java method

 public static String dateConvertorISO(String dateAndTimeStamp) {
        try {
            LocalDateTime originalLocalDateTimeStamp = LocalDateTime.parse(dateAndTimeStamp,
                    DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            ZoneId usEastern = ZoneId.of("America/New_York");
            return ZonedDateTime.of(originalLocalDateTimeStamp, usEastern).toString();
        } catch (Exception e) {
            LOGGER.error("Cannot convert date-time: {} to ISO because {}", dateAndTimeStamp, e.getMessage());
        }
        return dateAndTimeStamp;

    }

But I am not able to match the pattern, above given method, is returning following - 2018-03-09T22:00:01-05:00[America/New_York]

Upvotes: 0

Views: 385

Answers (3)

Shek
Shek

Reputation: 1613

Thanks for the help and nudge in the right direction, I have updated my method to following and now I am getting the desired date and time pattern.

 public static String dateConvertorISO(String dateAndTimeStamp) {
        try {
            return ZonedDateTime.of(
                    LocalDateTime.parse(dateAndTimeStamp, DateTimeFormatter.ofPattern("yyyyMMddHHmmss")),
                    ZoneId.of("America/New_York")).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        } catch (Exception e) {
            LOGGER.error("Cannot convert date-time: {} to ISO because {}", dateAndTimeStamp, e.getMessage());
        }
        return dateAndTimeStamp;

    }

Upvotes: 0

D-rk
D-rk

Reputation: 5919

Instead of toString() use DateTimeFormatter.ISO_OFFSET_DATE_TIME.format():

    public static String dateConvertorISO(String dateAndTimeStamp) {
    try {
        LocalDateTime originalLocalDateTimeStamp = LocalDateTime.parse(dateAndTimeStamp,
                DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        ZoneId usEastern = ZoneId.of("America/New_York");

        ZonedDateTime zonedDateTime = ZonedDateTime.of(originalLocalDateTimeStamp, usEastern);
        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime);
    } catch (Exception e) {
        LOGGER.error("Cannot convert date-time: {} to ISO because {}", dateAndTimeStamp, e.getMessage());
    }
    return dateAndTimeStamp;
}

Upvotes: 3

humblefoolish
humblefoolish

Reputation: 429

I used this to remove time zone from end:

public static void main(String... iArgs)
{
    Calendar vCalendar = Calendar.getInstance();

    Date vDate = vCalendar.getTime();

    ZoneId usEastern =ZoneId.of("US/Eastern");

    ZoneId amNY = ZoneId.of("America/New_York");

    System.out.println(vDate.toInstant().atZone(amNY));
    //prints 2018-04-12T14:38:46.519-04:00[America/New_York]

    System.out.println(vDate.toInstant().atZone(usEastern));
    //prints 2018-04-12T14:38:46.519-04:00[US/Eastern]

    DateTimeFormatter noTimeZoneFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
    System.out.println(vDate.toInstant().atZone(usEastern).format(noTimeZoneFormatter));

    System.out.println(vDate.toInstant().atZone(amNY).format(noTimeZoneFormatter));

    //prints 2018-04-12T14:38:46
}

Upvotes: 0

Related Questions