0

I have looked around for help on this, but again, it's just one of those things that I cannot find a suitable answer to my specific issue.

Here's 2 very detailed (and helpful) SO posts that I've looked at:

Change date format in a Java string

Java string to date conversion

This is what I have:

//Date Formatter
SimpleDateFormat dateFormatter1 = new SimpleDateFormat("yyyy-MM-dd", new Locale("EN"));
SimpleDateFormat dateFormatter2 = new SimpleDateFormat("dd MMMM yyyy", new Locale("EN"));

//Convert to Date
Date dateToParse = dateFormatter1.parse("2024-01-01");

//Format OUTPUT date
String dateAsString = dateFormatter1.format(dateToParse);

System.out.println(dateAsString); //01 January 2024

//Convert OUTPUT date from STRING to DATE
Date dateToReturn = dateFormatter1.parse(dateAsString);

System.out.println(dateToReturn.toString()); //Mon Jan 01 00:00:00 SAST 2024

Note:

I used both DateFormatter and SimpleDateFormatter but got the same output.

The outputs are very different and what I am trying to achieve is to have my String created as a Date object in the exact same format.

I feel I am missing something but I just cannot figure out what.

  1. The code I provided is a snippet from a bigger function that returns type Date
  2. The function wasn't created by myself, I'm picking up from where someone else left off
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Eitel Dagnin
  • 959
  • 4
  • 24
  • 61
  • Not that it matters here, but you shouldn't be using SimpleDateFormat – OneCricketeer Aug 22 '21 at 13:37
  • @OneCricketeer Agreed, I will actually change it to `DateFormatter`. I tried both ways, but ended with the same result. :) – Eitel Dagnin Aug 22 '21 at 13:38
  • Does this answer your question? [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string) – Gawron Aug 22 '21 at 13:42
  • 4
    Date instances **do not have a format**. Neither does LocalDate or ZonedDate. A date has a format when you output the date as a String. – Gilbert Le Blanc Aug 22 '21 at 13:42
  • 2
    I recommend you don’t use `DateFormat`, `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the first two in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 22 '21 at 13:43
  • 1
    @OneCricketeer Thank you for the feedback and the answer below. I will look at changing the function to use `LocalDate` or `DateTimeFormatter` instead. :) – Eitel Dagnin Aug 22 '21 at 13:51
  • @OleV.V. Yes, that is my fault. I updated the question to mention that the code is a snippet from a larger function and in my post I didn't correctly add the part where the `String` date is changed to a `Date` object and then formatted. – Eitel Dagnin Aug 22 '21 at 13:52
  • @EitelDagnin - Instead of wasting time editing your questions further, I recommend you go through the answers to linked questions (duplicate target). I am sure you will be able to understand this concept well after going through even one of the linked Q/As. Feel free to comment if, for any reason, you still have any difficulty. – Arvind Kumar Avinash Aug 22 '21 at 13:59
  • @GilbertLeBlanc Thank you for this. This is what I wasn't understanding - In my mind, I was expecting that if I have formatted the date (that outputs as a `String`) that I could just convert it back to a `Date` object but that it would retain it's `String` format (if that makes sense). Anyway, I am using `LocalDate` now and have changed the way the function works. :) – Eitel Dagnin Aug 22 '21 at 14:37

2 Answers2

0

Whenever you call a .toString() method on a Date, then the DateFormatter is not taken into account. The format of date you get is just a matter of default toString implementation of Date class.

To use formatter while printing, try something like this (instead of the last line in your snippet): System.out.println(dateFormatter1.format(dateToReturn));

Gawron
  • 85
  • 1
  • 11
  • Thank you for the answer, definitely makes sense :). I think I may have left out an important part of my post, which I didn't think to include, but will update it now. The thing is not that I really want to print it out, it's actually a snippet of code from a function that returns type Date and where I call that function, I cannot "reuse" the date format since it's changed to ISO8601 as mentioned by OneCricketeer above – Eitel Dagnin Aug 22 '21 at 13:43
  • 1
    Assuming you can change the method to return a `LocalDate`which also hasn’t got any inherent format, what would keep you from formatting it into a string to your users’ liking after it’s returned from the method? – Ole V.V. Aug 22 '21 at 13:55
  • @OleV.V. Yeah, thank you. I am looking to do this (change it to `LocalDate`). The reason I made this post was actually because of formatting it into a `String` after the `Date` was returned and not getting the correct value. But as mentioned, I am using outdate and problematic `DateFormatters` :) – Eitel Dagnin Aug 22 '21 at 14:06
0

Date.toString() returns always returns a string in the format dow mon dd hh:mm:ss zzz yyyy.

You'll need to reuse the formatter's format method to get the initial string you expect

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245