-2

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

  • What is t? What is the output? – aled May 07 '23 at 02:25
  • The code is correct. Even the substring line alone would produce 2023-04-20. I suggest stepping over the code with a debugger and looking at each stage of the code to understand where the other date is coming from. – Shai Almog May 07 '23 at 02:26
  • 3
    A `Date` has no format. If you want it in a specific format, then you should make a `String`. – Sweeper May 07 '23 at 02:26
  • this is the output the code gave me :Wed Apr 26 01:00:00 WAT 2023 this is the output am trying to get : 2023-04-20 – massaoud ghassen May 07 '23 at 02:27
  • 2
    @massaoudghassen You're not listening - **`Date` DOES NOT have any concept of format** - it is simply a container for the amount of time which has passed since the Unix Epoch. What you're asking CAN NOT be achieved in the way you want it. Instead, you take `Date` value and use a different formatter to format the `Date` to a `String` using your desired format. (Sorry if I seem "mean", but this question gets asked all the time (the second time I've seen in 24 hours) and the answer is always the same - format the `Date` to a `String`) – MadProgrammer May 07 '23 at 02:31
  • @MadProgrammer sry that comment was meant for the guy on top of the comments am still new to stack overflow community , it's not that i don't wanna do that it's just i can't because am getting the date from a json and not from a datePicker or something like that – massaoud ghassen May 07 '23 at 02:46
  • @massaoudghassen Well you can. You're either starting with a `Date` or `String`, but the process is the same - use a different formatter to format the `Date` to `String` format you want. So based on your example, you take the `String` value, convert it to a `Date` using one formatter and the use a second formatter to return a new `String` value – MadProgrammer May 07 '23 at 02:57
  • look the setEventDate accepts type Date only when i try passing localDate it gives an error that's why am trying to convert the string into a date so i can pass it and when am displaying it am converting it again i've saw ur answer down below and am trying to implement it in my work thanks btw – massaoud ghassen May 07 '23 at 03:06
  • You are still missing the point. The problem in your application is not in the code that you showed us. The real problem is in the code that *uses* the `Date` value that you set using `t.setEventDate(date)`. It is *that* code that is using the wrong format. – Stephen C May 07 '23 at 03:17
  • this is a uni project so am sure there will be those kind of problems everywhere anw i followed what u told me and yeah it works just fine now thanks a lot – massaoud ghassen May 07 '23 at 03:19
  • My guess is that your code just calls (or called ... if you have changed it) `Date.toString()` on the `Date` object. That uses the hard-wired format specified here: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--. – Stephen C May 07 '23 at 03:24
  • 1
    BTW do **not** use `java.util.Date` - this class was outdated by the not-so-new classes from `java.time` (introduced in Java 8 / 2014 - actually released Java 20 / 2023) – user16320675 May 07 '23 at 06:58
  • @user16320675 this isn't true for Codename One – Shai Almog May 08 '23 at 02:03

2 Answers2

3

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);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

tl;dr

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

See this code run at Ideone.com.

2023-04-20

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() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154