-3

I am trying to parse a string in java that comes like this: String test="Unknown" But users data wants this string as date. Can it be a way to parse or avoid this case? For example that date to be taken like empty, instead of unknown?

5
  • 2
    What date would you want if the input is literally "Unknown"? This seems pointless. That is not a date in any meaningful sense. Commented Nov 17, 2021 at 14:17
  • I know, but sometimes the input may come as "Unknown" , if the user leaves it empty. I just want to know if there is a way to replace unknown with empty date , or if may be some workaround Commented Nov 17, 2021 at 14:18
  • 4
    There is no "empty date". Either your variable is null or it has an instance of LocalDate or a similar object. Commented Nov 17, 2021 at 14:20
  • 3
    You have to clarify your requirements. We can't tell you how to react to that "Unknown" string. You can throw an exception, you could return a data representing 1900-01-01 or something. You have to understand that there are 2 aspects here: A) detecting the case, and handling it ... and B) what to give back to the user. You are asking about "B)", and we cant tell you that, because it is YOUR product, your users. Commented Nov 17, 2021 at 14:25
  • Similar: How can I parse an empty LocalDate in Java? [closed] Commented Nov 17, 2021 at 15:08

1 Answer 1

4

Catch the exception when the Date can't be parsed and set the Date to null or some placeholder value that you know to represent an unparseable date.

edit: As GhostCat points out in the comments, you may want only "Unknown" to be treated this way, and not unparseable dates in general. Error handling can get complicated... at the very least you should be logging when the date can't be parsed. The exact requirements haven't been stated in your question so how you need to handle errors is not known.

Sign up to request clarification or add additional context in comments.

6 Comments

I would rather check for "Unknown" specifically than just handle all parse errors as if they were unknown. Maybe some date strings are given as "21.12.2018" instead of the expected "12/21/2018" and then you should at least leave a warning in the logfile.
Exactly, indiscriminately turning all parsing errors into that single case is bad advise.
@ErichKitzmueller Yes, totally agree that these cases should be logged at the least. "Unknown"could be set to a specific token Date value that represents the "Unknown" data, but care needs to be taken so the token Date values aren't interpreted elsewhere as "real" dates. There should be another indicator or wrapping class so bad dates don't get exposed.
Thanx a lot, I managed to solve it by exceptions
Logging is no solution. You will want to handle the case of "Unknown" separately exactly in order to avoid that error handling gets more complicated than necessary.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.