public
anonymous / ReplaceApp.java
Created

Submission for Easy Challenge #118

  • Download Gist
ReplaceApp.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.