Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

GetDate

By: a guest on Jan 25th, 2013  |  syntax: Java  |  size: 2.02 KB  |  views: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //class GetDate   //
  2. /*
  3. Main will output the input, but replace certain
  4. keywords with the following date values:
  5.  
  6.         %l - Milliseconds  (000 - 999)
  7.         %s - Seconds       (00  - 59)
  8.         %m - Minutes       (00  - 59)
  9.         %h - Hours         (1   - 12)
  10.         %H - Hours         (1   - 24)
  11.         %c - AM/PM         (AM  - PM)
  12.         %d - Day           (1   - 31)
  13.         %M - Month         (1   - 12)
  14.         %y - Year                  (  ----  )
  15. */
  16. import java.util.Calendar;
  17.  
  18. class GetDate{
  19.         private static char[]     toReplace     = {'l', 's', 'm', 'h', 'H', 'c', 'd', 'M', 'y'};
  20.         private static String[]   replaceValues = {"", "", "", "", "", "", "", "", ""};
  21.  
  22.         public static void main(String args[]){
  23.                 String toParse = "";
  24.                 for(int i = 0; i < args.length; ++i)
  25.                         toParse += (args[i] + " ");
  26.                 System.out.println(parseString(toParse));
  27.         }
  28.  
  29.         public static String parseString(String a_toParse){
  30.                 String parsedString = a_toParse;
  31.                 updateValues();
  32.                 for(int i = 0; i < toReplace.length; ++i)
  33.                         parsedString = parsedString.replaceAll("%" + toReplace[i], replaceValues[i]);
  34.                 return parsedString;
  35.         }
  36.        
  37.         public static void updateValues(){
  38.                 Calendar c = Calendar.getInstance();
  39.                 replaceValues[0] = String.valueOf(c.get(Calendar.MILLISECOND));
  40.                 replaceValues[1] = (c.get(Calendar.SECOND) < 10) // to make sure I get 04 instead of 4, etc
  41.                                          ? ("0" + String.valueOf(c.get(Calendar.SECOND))) : String.valueOf(c.get(Calendar.SECOND));
  42.                 replaceValues[2] = (c.get(Calendar.MINUTE) < 10) // to make sure I get 04 instead of 4, etc
  43.                                                  ? ("0" + String.valueOf(c.get(Calendar.MINUTE))) : String.valueOf(c.get(Calendar.MINUTE));
  44.                 replaceValues[3] = String.valueOf(c.get(Calendar.HOUR));
  45.                 replaceValues[4] = String.valueOf(c.get(Calendar.HOUR_OF_DAY)); // 24 hour
  46.                 replaceValues[5] = ((c.get(Calendar.AM_PM)) == 1) ? "PM" : "AM"; // If value is 1, then return PM. Else AM.
  47.                 replaceValues[6] = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
  48.                 replaceValues[7] = String.valueOf(c.get(Calendar.MONTH) + 1);
  49.                 replaceValues[8] = String.valueOf(c.get(Calendar.YEAR));
  50.         }
  51. }