Home > OS >  java convert date time string to zulu date time format
java convert date time string to zulu date time format

Time:09-29

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

CodePudding user response:

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 )

CodePudding user response:

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"));   

}
}

CodePudding user response:

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

  • Related