0

I'm using a ZonedDateTime to get the current date-time. Because I dont want it with the time, I'm making it the format of uuuu-MM-dd and it gives me back a String as 2021-08-23

ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuu-MM-dd" ); 
String date_string = zdt.format(formatter);
System.out.println(date_string);//2021-08-23

Now how can I convert this String 2021-08-23 to a java.util.Date? I want the date to look exactly like the String... This is what I have tried but it won't get me the date looking like the string

SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter1.parse(date_string); 

Will give me the date in this format: Mon Aug 23 00:00:00 EEST 2021... How can I convert my string into a date and keep the date looking like the string?

JohnnySmith88
  • 162
  • 1
  • 10
  • 4
    Why do you need `java.util.Date`? Incidentally that class doesn't have a format. What you're seeing is the result of a default formatter being applied to it. You should be using your own formatter with `String s = formatter1.format(date)` – g00se Aug 23 '21 at 19:10
  • How can I do that with another date class? – JohnnySmith88 Aug 23 '21 at 19:11
  • 3
    Still not sure why you need `util.Date` – g00se Aug 23 '21 at 19:13
  • Like what @g00se mentioned, the `java.util.Date` is the object representation of the string date. If you're willing to override `java.util.Date`'s toString() method into your own custom Date, you can do that. However, I am not sure what's the point of doing it in the first place. If your goal is to have a date in a certain string format, then use a string and not the `Date` object. – nmina Aug 23 '21 at 19:15
  • 1
    A `ZonedDateTime` can be converted to an `Instant`, which has the `toDate()` method. However, you say you want a `Date`, but you have not explained why you want this. To me, this looks like an XY problem. – MC Emperor Aug 23 '21 at 19:19
  • You may want to use `LocalDate`. It hasn’t got any inherent format either, but its `toString` method happens to produce the format you asked for `uuuu-MM-dd`, Use `zdt.toLocalDate()` for the conversion. – Ole V.V. Aug 23 '21 at 19:35
  • 1
    I recommend you neither use `SimpleDateFormat` nor `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and possibly other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 23 '21 at 19:37
  • What makes you think that you want an object with a specific format? It is ill advised because it’s mixing model and presentation, things that you should keep separate. And it is not possible with any class from the standard library. – Ole V.V. Aug 23 '21 at 19:39

1 Answers1

2

tl;dr

LocalDate.now().toString()

2021-08-23

Better to specify desired/expected time zone rather than rely implicitly on the JVM’s current default time zone.

LocalDate.now( ZoneId.of( "Africa/Tunis" ) ).toString()

Details

LocalDate

If you want a date-only, use LocalDate.

LocalDate ld = myZonedDateTime.toLocalDate() ;

The java.time.Date class is terribly flawed, and should be avoided. The class was supplanted years ago by the modern java.time classes defined in JSR 310.

If you must use this class to interoperable with old code not yet updated to java.time, you can convert back and forth between the legacy classes and their replacements. Look for the new conversion methods added to the old classes.

java.util.Date is not a date!

Understand that among its many flaws is the name of java.util.Date. That class represents not a date but a moment in time as seen in UTC. So its replacement is java.time.Instant, not LocalDate.

java.util.Date d = java.util.Date.from( myZonedDateTime.toInstant() );

You asked:

How can I convert my string into a date and keep the date looking like the string?

Understand that none of these date-time classes are text. They have their own internally defined representation of a date-time value, not String.

It sounds like you simply want text in standard ISO 8601 format (YYYY-MM-DD) for a date-only value. The java.time classes use the standard ISO 8601 formats by default when generating/parsing strings. So no need to define a custom formatting pattern.

String output = myZonedDateTime.toLocalDate().toString() ;

Date-time objects have no format, are not text

You asked:

Now how can I convert this String 2021-08-23 to a java.util.Date? I want the date to look exactly like the String

What you request makes no sense.

A java.util.Date object is a date-time object, not text, not String. A java.util.Date object has no “format”.

Likewise, a java.time.ZonedDateTime object is a date-time object, not text, not String. A java.time.ZonedDateTime object has no “format”.

And so too, a java.time.LocalDate object is a date object, not text, not String. A java.time.LocalDate object has no “format”.

These date-time objects have a toString method that generates text representing the embedded value using a default format. But you should not conflate that generated text with the original date-time object.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154