public
Created

Proposal for Easy Challenge #118

  • Download Gist
DateFormat.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
package fr.daniel.dateFormat;
 
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 
public class DateFormat {
 
private static Map<String, String> linkJavaISO = new HashMap<>();
static {
linkJavaISO.put("%l", "%1\\$tL");
linkJavaISO.put("%s", "%1\\$tS");
linkJavaISO.put("%m", "%1\\$tM");
linkJavaISO.put("%h", "%1\\$tI");
linkJavaISO.put("%H", "%1\\$tH");
linkJavaISO.put("%c", "%1\\$tp");
linkJavaISO.put("%d", "%1\\$td");
linkJavaISO.put("%M", "%1\\$tm");
linkJavaISO.put("%y", "%1\\$tY");
}
 
public static String dateFromISO(String format){
for (Entry<String, String> entry : linkJavaISO.entrySet()){
format = format.replaceAll(entry.getKey(), entry.getValue());
}
 
return String.format(format, Calendar.getInstance());
}
 
public static void main(String[] args) {
System.out.println(dateFromISO("%s.%l"));
System.out.println(dateFromISO("%s:%m:%h %M/%d/%y"));
System.out.println(dateFromISO("The minute is %m! The hour is %h."));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.