skip to Main Content

i have this String that contains a date in this format "2023-04-20T00:00:00+02:00" and i want to convert it back to date in a format just like this 2023-04-20

i tried this code

 String dateString=obj.get("eventDate").toString();
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 Date date = dateFormat.parse(dateString.substring(0, 10));     
 t.setEventDate(date);

but it gave me this output Wed Apr 26 01:00:00 WAT 2023 and it’s not what am looking for

2

Answers


  1. Convert the String you have to a Date object, based on the input format…

    String dateString = "2023-04-20T00:00:00+02:00";
    SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
    Date date = inFormat.parse(dateString);
    

    Then use a different formatter to format the Date back to the desired String representation

    SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
    String outString = outFormat.format(date);
    System.out.println(outString);
    

    Which will output 2023-04-20

    Remember, date/time classes are a representation of the amount of time which has passed since some anchor point in time (ie the Unix Epoch), they do not have an intrinsic concept of "format", in fact the output of these classes should be consider debug information only, this is why we have formatters.

    The modern approach

    You should avoid making use of the java.util date/time classes, they are effectively deprecated, instead, you should be making use of the java.time API instead

    String dateString = "2023-04-20T00:00:00+02:00";
    LocalDateTime ldt = LocalDateTime.parse(dateString, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    String outString = ldt.format(DateTimeFormatter.ISO_DATE);
    System.out.println(outString);
    
    Login or Signup to reply.
  2. tl;dr

    OffsetDateTime
    .parse( "2023-04-20T00:00:00+02:00" )
    .toLocalDate()
    .toString()
    

    String manipulation

    Obviously you could simply manipulate the input string. Truncate after the first ten letters.

    String result = "2023-04-20T00:00:00+02:00".substring( 0 , 10 ) ;
    

    Or, split the string into pieces. Grab the first piece.

    String result = "2023-04-20T00:00:00+02:00".split( "T" )[ 0 ] ;
    

    java.time

    You could parse the input as a date-time value.

    Use only the java.time classes for date-time work. Never use the terribly flawed legacy classes such as Date, Calendar, and SimpleDateFormat.

    To parse your particular input, use the java.time.OffsetDateTime class. Your input consists of three parts: a date, a time-of-day, and an offset from UTC of a number of hours and minutes. To OffsetDateTime class fully represents all three parts.

    OffsetDateTime odt = OffsetDateTime.parse( "2023-04-20T00:00:00+02:00" ) ;
    

    In contrast, note that the LocalDateTime class seen in the other Answer cannot represent all three parts. That class represents only the first two of the three parts. So the use of that class there is misleading and confusing.

    After parsing, we can extract just the date portion, without the time of day, and without the offset.

    LocalDate ld = odt.toLocalDate() ;
    

    Generate text to represent that date value, in standard ISO 8601 format.

    String result = ld.toString() ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search