Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class GetDate //
- /*
- Main will output the input, but replace certain
- keywords with the following date values:
- %l - Milliseconds (000 - 999)
- %s - Seconds (00 - 59)
- %m - Minutes (00 - 59)
- %h - Hours (1 - 12)
- %H - Hours (1 - 24)
- %c - AM/PM (AM - PM)
- %d - Day (1 - 31)
- %M - Month (1 - 12)
- %y - Year ( ---- )
- */
- import java.util.Calendar;
- class GetDate{
- private static char[] toReplace = {'l', 's', 'm', 'h', 'H', 'c', 'd', 'M', 'y'};
- private static String[] replaceValues = {"", "", "", "", "", "", "", "", ""};
- public static void main(String args[]){
- String toParse = "";
- for(int i = 0; i < args.length; ++i)
- toParse += (args[i] + " ");
- System.out.println(parseString(toParse));
- }
- public static String parseString(String a_toParse){
- String parsedString = a_toParse;
- updateValues();
- for(int i = 0; i < toReplace.length; ++i)
- parsedString = parsedString.replaceAll("%" + toReplace[i], replaceValues[i]);
- return parsedString;
- }
- public static void updateValues(){
- Calendar c = Calendar.getInstance();
- replaceValues[0] = String.valueOf(c.get(Calendar.MILLISECOND));
- replaceValues[1] = (c.get(Calendar.SECOND) < 10) // to make sure I get 04 instead of 4, etc
- ? ("0" + String.valueOf(c.get(Calendar.SECOND))) : String.valueOf(c.get(Calendar.SECOND));
- replaceValues[2] = (c.get(Calendar.MINUTE) < 10) // to make sure I get 04 instead of 4, etc
- ? ("0" + String.valueOf(c.get(Calendar.MINUTE))) : String.valueOf(c.get(Calendar.MINUTE));
- replaceValues[3] = String.valueOf(c.get(Calendar.HOUR));
- replaceValues[4] = String.valueOf(c.get(Calendar.HOUR_OF_DAY)); // 24 hour
- replaceValues[5] = ((c.get(Calendar.AM_PM)) == 1) ? "PM" : "AM"; // If value is 1, then return PM. Else AM.
- replaceValues[6] = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
- replaceValues[7] = String.valueOf(c.get(Calendar.MONTH) + 1);
- replaceValues[8] = String.valueOf(c.get(Calendar.YEAR));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement