Parse a string "Unknown" to date in java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
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?

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.

Related

Why does SonarQube report: The return value of "format" must be used [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 27 days ago.
Improve this question
I'm getting this code smell in SonarQube "the return value of "format" must be used".
Sample code:
ShiftStartDate.format(DateTimeFormatter.ofPattern("MMddyyy")
ShiftEndDate.format(DateTimeFormatter.ofPattern("MMddyyyy")):
I tried, but I'm not sure what to return.
format is not mutating the Date, but rater returns a new String with the provided pattern. Sonarqube is telling you, that you do nothing with the return value. So either assign it to a variable, which you will use or remove the invocations altogether.

Java regular expression is validating the date in ddMMMyyy but not validating yyyy-MM-dd [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I want to validate different kinds of strings which are of different format like
10JUN2022, 2Mx1D, 4M, 1D, TEN, ONE|TEN etc.. and I have written regular expression for that '''^([0-9A-WYZa-wyz ]+)([xX|]([0-9A-WYZa-wyz ]+))?$''' and it's working fine but I also need to validate one more string 2022-06-10, but the expression is failing.
When it comes to regex, don't try to get overly clever. Just solve the basic problem. If that takes multiple regex patterns, so be it. It's much easier to maintain and read.
I would use this for the first regex: [0-3]?\d\w{3}(1|2)\d{3}
and this for the second regex: (1|2)\d{3}(-\d{2}){2}
or combine them if you must: ([0-3]?\d\w{3}(1|2)\d{3})|((1|2)\d{3}(-\d{2}){2})

Oracle compare date [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a table with date field in it. User passes month and year. I need to return records where its date has passed year and month.
For example.
Record in table: time:'2016-01-10', time:'2016-01-01', time:'2015-01-01'.
Passed values: year:'2016', month:'01'.
And the return should be first and second records.
How to do it in oracle?
PS. I tried smth like this time>=date'2016-01-01' and time<=date'2016-01-31' but if the last day is not 31, I will get an exception.
Read this date formatting
If I understand you correct you want something like this
To_char(Your_date_col, 'YYYYMM' ) = yearparam ||monthparam

Using Regular Expression for reading JSON strings

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 hours ago.
Improve this question
I have a java string in JSON format (the conventional key:value) which I get from Amazon S3. Some of the keys have regular expression pattern defined. What is the best way to get these keys and then fetch the values corresponding to them?
I know there exists regex of javautil which I'm planning to use. Is there any other better way for accessing the regex type keys?

Java get number or currency format from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have code which contains XML which is parsed and treats all values as strings. Some of these strings are metric values with currency or % or number formatting. How can I obtain this formatting from the string and then reapply it later. The formatting is removed when numbers are cast to doubles.
<p><val>$4500.30</val><val>45.0%</val></p>
//parse out
String metric1 = "$4500.30";
String metric2 = "45.0%";
//remove special char
metric1 = metric1.replaceAll("[^A-Za-z0-9.]", "");
//I need to reapply formatting after it is removed(such as $)
You can also use a predefined instances of NumberFormats to be able to parse and format values every time you need. It's better then to have values both as plain & formatted values.
I realized this was a poor approach. Since the values were stored as string I just kept the formatting and used another copy of the string later which was converted to double.

Categories