Reputation:
I have a date in string like this: 10-10-2018, Which I want to be displayed like 10-Oct-2018, I am doing the following:
String date = "10-10-2018";
String newDate = date.replace(date.subString(date.indexOf("-")+1,date.lastIndexOf("-")),"October");
Instead of getting 10-October-2018 I am getting October-October-2018, why are both instances of 10 replaced although I am selecting the middle one between the "-" symbol. What am I missing.
Upvotes: 2
Views: 372
Reputation: 900
String newDate = date.replace(date.subString(date.indexOf("-")+1,date.lastIndexOf("-")),"October");
Let's unwrap this. You are implicitly
int firstHyphen = date.indexOf("-");
int lastHyphen = date.lastIndexOf("-");
But for the given string, this is the same as saying
int firstHyphen = 2;
int lastHyphen = 5;
Next, you have an implicit variable creation
String month = date.subString(firstHyphen+1, lastHyphen);
Which is the same as
String month = date.subString(3, 5);
But what substring starts at 3 and ends before 5?
String month = "10";
So we can take your original code and replace it with
String newDate = date.replace("10", "October");
It should be clear now that you are NOT restricting to just that portion of the string between the hyphens. That restriction only applied to the substring. Once you use the result of the substring in the replace, you are just passing it a string. In particular, you are passing "10"
, which appears twice in the string, so it replaces it twice.
If you want to try to make this work, you could change your original code to look like
String newDate = date.replace(date.subString(date.indexOf("-"), date.lastIndexOf("-")), "-October");
This would replace "-10"
with "-October"
. As "-10"
only occurs in the string once, it will only do one replacement.
If you have a Map
that maps the integer months to the names, you might use it like
Map<String, String> months = new HashMap<>();
months.put("10", "October");
String month = date.subString(date.indexOf("-"), date.lastIndexOf("-"));
String newDate = date.replace(month, "-" + months.get(month.subString(1)));
This builds the "-October"
on the fly and will work for the other months, assuming that you populate the map with them. But as others have noted, it may be more reliable to use Java's built-in date parsing and formatting instead.
Upvotes: 0
Reputation: 757
This is what you need :)
public static void main(String[] args) throws ParseException {
String date = "10-10-2018";
SimpleDateFormat givenFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat newFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date givenDate=givenFormat.parse(date);
System.out.println(givenDate);
String newDate=newFormat.format(givenDate);
System.out.println(newDate);
}
Upvotes: 0
Reputation: 45339
The simple problem is that you're replacing the string "10"
with "October"
in a string where there are other instances of the same substring.
Your call is equivalent to:
date.replace("10", "October");
Which will replace the first instance of "10"
too. String.replace
's docs contain this:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
A more reliable approach would be to use the date/time APIs to parse and format your date:
String date = "10-10-2018";
LocalDate d = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
String newDate = d.format(DateTimeFormatter.ofPattern("dd-MMMM-yyyy"));
newDate
evaluates to "10-October-2018"
as expected.
Upvotes: 4