1

I have this pattern of date: 2000-03-05 represented by YYYY-MM-dd

The problem is, the date value can be empty, so I would get "2000-03-".

I was trying to override this behavior by using replace all, to replace the null value, but as I get the value in String, there is no null or empty value.

String.join("-", date.getYear(), date.getMonth(), date.getDay()).replaceAll(" ", "c")

How should this be done?

Thanks!

4
  • 1
    String.join("-", date.getYear(), date.getMonth(), date.getDay().length() > 0 ? date.getDay() : "c")? Commented Aug 10, 2022 at 15:12
  • 3
    the date value can be empty, so I would get "2000-03-" - What do you mean by saying "date value can be empty"? What is the type of date? Commented Aug 10, 2022 at 15:13
  • Date is a Object with three String - Year, Month and Date. On all of this three values, the user can pass them as null or empty. So in this case, I would like to replace them if a a specific char. Commented Aug 10, 2022 at 15:14
  • 1
    @iRaven Why do you need to reinvent the wheel? You can use classes from the java.time package, like LocalDate. Commented Aug 10, 2022 at 15:18

3 Answers 3

2

You could probably use a Conditional Operator aka, the ternary (?:) operator where (a ? b : c) says if a is true, evaluate b, else evaluate c. and do it like this.

String.join("-", date.getYear(), date.getMonth(), 
     date.getDay().isEmpty() ? "c" : date.getDay());

Here's an example using a record as a simple Date class.

record Date(String getYear, String getMonth, String getDay) {

Date date1 = new Date("2002", "10", "30");
Date date2 = new Date("2002", "10", "");

System.out.println(String.join("-", date1.getYear(),
        date1.getMonth(),
        date1.getDay().isEmpty() ? "c" : date1.getDay()));

System.out.println(String.join("-", date2.getYear(),
        date2.getMonth(),
        date2.getDay().isEmpty() ? "c" : date2.getDay()));

prints

2002-10-30
2002-10-c

But you may want to provide more information including your use case as more constructive help could result. And make certain you aren't using the old Date class but those classes in the java.time package.

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

Comments

0

In addition to WJS comment, I would have put everything in a String.format in order to make things clearer and more readable.

String.format("%s-%s-%s", date1.getYear(), date1.getMonth(), date1.getDay().isEmpty() ? "c" : date1.getDay());

Comments

0

You can use SimpleDateFormat as below:

String sDate ="2020-08-";  
Date date = null;
try {
    date = new SimpleDateFormat("yyyy-MM-dd").parse(sDate);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    sDate = sDate + "c";
}  
System.out.println("sDate= " +"\t"+ sDate);

Output: sDate= 2020-08-c

1 Comment

Don't be recommending SimpleDateFormat. It is outdated and flawed. All Date formatting and manipulation should be done using classes from the java.time package. And what happens if the user gets an unrelated parse exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.