Skip to content

anonymous /ReplaceApp.java

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Submission for Easy Challenge #118
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
interface DateTimeComponent {
public String write(final Calendar calendar);
}
class Year implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.YEAR));
}
}
class Month implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
int monthValue = calendar.get(Calendar.MONTH);
String monthString;
switch(monthValue) {
case Calendar.JANUARY:
monthString = "1";
break;
case Calendar.FEBRUARY:
monthString = "2";
break;
case Calendar.MARCH:
monthString = "3";
break;
case Calendar.APRIL:
monthString = "4";
break;
case Calendar.MAY:
monthString = "5";
break;
case Calendar.JUNE:
monthString = "6";
break;
case Calendar.JULY:
monthString = "7";
break;
case Calendar.AUGUST:
monthString = "8";
break;
case Calendar.SEPTEMBER:
monthString = "9";
break;
case Calendar.OCTOBER:
monthString = "10";
break;
case Calendar.NOVEMBER:
monthString = "11";
break;
case Calendar.DECEMBER:
monthString = "12";
break;
default:
monthString = "";
}
return monthString;
}
}
class Day implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
}
}
class Hour12 implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
int hourValue = calendar.get(Calendar.HOUR);
return String.valueOf(hourValue == 0 ? 12 : hourValue);
}
}
class Hour24 implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
}
}
class Minute implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.MINUTE));
}
}
class Second implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.SECOND));
}
}
class Millisecond implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
return String.valueOf(calendar.get(Calendar.MILLISECOND));
}
}
class AMPM implements DateTimeComponent {
@Override
public String write(final Calendar calendar) {
int AMPMValue = calendar.get(Calendar.AM_PM);
String AMPMString = "";
switch(AMPMValue) {
case Calendar.AM:
AMPMString = "AM";
break;
case Calendar.PM:
AMPMString = "PM";
break;
}
return AMPMString;
}
}
class Replacer {
private Map<String,DateTimeComponent> replacements;
public Replacer(Map<String,DateTimeComponent> replacementsInit) {
replacements = replacementsInit;
}
public String replace(String string, Calendar calendar) {
for(Entry<String,DateTimeComponent> replacmentPair :
replacements.entrySet()) {
string = string.replace(replacmentPair.getKey(),
replacmentPair.getValue().write(calendar));
}
return string;
}
}
public class ReplaceApp {
public static void main(String[] args) {
Map<String,DateTimeComponent> replacements = new HashMap<>();
replacements.put("%l", new Millisecond());
replacements.put("%s", new Second());
replacements.put("%m", new Minute());
replacements.put("%h", new Hour12());
replacements.put("%H", new Hour24());
replacements.put("%d", new Day());
replacements.put("%M", new Month());
replacements.put("%y", new Year());
replacements.put("%c", new AMPM());
Replacer replacer = new Replacer(replacements);
// Tests
Calendar calendarNow = Calendar.getInstance();
String[] testStrings = {
"%s.%l",
"%s:%m:%h %M/%d/%y",
"The minute is %m! The hour is %h.",
"24 hour time: %H:%m:%s.%l",
"12 hour time: %h:%m:%s.%l %c"
};
for(String testString : testStrings) {
System.out.println(replacer.replace(testString, calendarNow));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.