-2

I have a method that takes a user's input in this case "%YEAR%" and converts that input into the current year. The getCopyrightProperty method takes a property entered by the user and displays its output. Which works fine. But how would I take if the user input %YEAR% and display the actually year to it along with also displaying the user output.

private static final String COPYRIGHT_PROPERTY = "copyright-message";
private static final String SHOWYEAR = "" + Calendar.getInstance().get(Calendar.YEAR);

public String getCopyrightProperty(){                
    return (this.properties != null) ? this.properties.get(COPYRIGHT_PROPERTY, "") : null;
}

public String getSubstituteCurrYear(String in){                
    return in.replaceAll("%YEAR%", SHOWYEAR);            
}

Sample input would be "%YEAR% input some placeholder text" and ideally the output should be "2013 input some placeholder text"

properties.get is a sling method that gathers user input from a specific property

4

1 回答 1

1

If I understand your question correctly,

private static final String COPYRIGHT_PROPERTY  = "%YEAR% copyright-message";
private static final String SHOWYEAR            = 
        "" + Calendar.getInstance().get(Calendar.YEAR);

public String getCopyrightProperty() {
    String s = this.properties.get(COPYRIGHT_PROPERTY, "");
    if (s == null) {
        return null;
    } else {
        return getSubstituteCurrYear(s);
    }
}

public String getSubstituteCurrYear(String in) {
    return in.replaceAll("%YEAR%", SHOWYEAR);
}
于 2013-04-24T19:15:01.360 回答