Created
January 21, 2013 18:46
-
-
Save DanielChesters/4588278 to your computer and use it in GitHub Desktop.
Proposal for Easy Challenge #118
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment