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?
Asked
Active
Viewed 87 times
-3
-
2What date would you want if the input is literally `"Unknown"`? This seems pointless. That is not a date in any meaningful sense. – Joachim Sauer Nov 17 '21 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 – Ester Nov 17 '21 at 14:18
-
4There is no "empty date". Either your variable is `null` or it has an instance of `LocalDate` or a similar object. – McPringle Nov 17 '21 at 14:20
-
3You 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. – GhostCat Nov 17 '21 at 14:25
-
Similar: [How can I parse an empty LocalDate in Java? \[closed\]](https://stackoverflow.com/questions/69980061/how-can-i-parse-an-empty-localdate-in-java) – Ole V.V. Nov 17 '21 at 15:08
1 Answers
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.

swpalmer
- 3,890
- 2
- 23
- 31
-
3I 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. – Erich Kitzmueller Nov 17 '21 at 14:22
-
1Exactly, indiscriminately turning all parsing errors into that single case is bad advise. – GhostCat Nov 17 '21 at 14:25
-
@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. – swpalmer Nov 17 '21 at 14:36
-
-
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. – Ole V.V. Nov 17 '21 at 15:09
-
@OleV.V. Not a solution, but a manner to identify what's going on if problems happen. It's not entirely clear what the requirements are. Should the code simply fail if it encounters a date that is neither "Unknown" nor parseable, or should it continue on a "best efforts" basis with placeholder values or null for unparseable dates? – swpalmer Nov 17 '21 at 17:07