Home > OS >  convert to ISO dateTime in java
convert to ISO dateTime in java

Time:10-12

 I have to convert String "15-08-2021" to  DateTime  yyyy-MM-dd'T'HH:mm:ss'Z' 

''' Tried the following way,which didnt work.

    String toDateString= "15-08-2021";
    DateTime dt= new DateTime(toDateString);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
     DateTime result=formatter.parse(dt);  

CodePudding user response:

java.time

The java.util Date-Time API 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*.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ENGLISH);
        LocalDate date = LocalDate.parse("15-08-2021", dtfInput);
        ZonedDateTime zdt = date.atStartOfDay(ZoneId.of("UTC"));
        System.out.println(zdt);

        // Custom format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
        String formatted = dtfOutput.format(zdt);
        System.out.println(formatted);
    }
}

Output:

2021-08-15T00:00Z[UTC]
2021-08-15T00:00:00Z

ONLINE DEMO

Learn more about the modern Date-Time API* from 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. Note that Android 8.0 Oreo already provides support for java.time.

CodePudding user response:

Are you using Joda-Time? You may consider upgrading to java.time, the modern Java date and time API (you certainly don’t want to downgrade to SimpleDateFormat and the other troublesome date and time classes from Java 1.0 and 1.1).

Using Joda-Time

With Joda-Time I would declare a formatter like this for parsing:

private static final DateTimeFormatter DATE_PARSER =
        DateTimeFormat.forPattern("dd-MM-yyyy")
            .withZoneUTC();

Then the conversion goes like this:

    String toDateString = "15-08-2021";
    DateTime dt = DateTime.parse(toDateString, DATE_PARSER);
    System.out.println(dt);

Output:

2021-08-15T00:00:00.000Z

Output includes three decimals on the seconds. It may not be a problem. The output agrees with the ISO 8601 standard that I think you intended to refer to.. If you need to get rid of them, you need a second formatter, but fortunately it’s built in:

    String formattedDt = dt.toString(ISODateTimeFormat.dateTimeNoMillis());
    System.out.println(formattedDt);

2021-08-15T00:00:00Z

Consider java.time

From the Joda-Time home page:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

For this option see the good answer by Arvind Kumar Avinash.

What went wrong in your code?

It seems you confused the concepts of formatting and parsing. You tried the parse method of SimpleDateFormat. In its day it was used for converting a String in a predefined format into a Date (another long outdated class). I am guessing that you want approximately the opposite: the formatting of a DateTime into a string in predefined format. Your other issue is that SimpleDateFormat never was able to handle Joda-Time DateTime objects (or any other class by the same name).

Links

  • Related