package date; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.io.PrintWriter; import java.io.StringWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * This is a sample for parsing and formatting dates, with java.util.* and Joda-time classes. *
*Joda-time can be found at http://joda-time.sourceforge.net/, * and is licensed (like this class) with the Apache Source License, version 2.0. *
*These classes are licensed under the Apache Software License, available at *
*ParseDate takes a Formatter and a Parser as parameters, and uses those to parse and format the dates; * you can mix and match parsers and formatters if you like. *
*The definitions of Formatter and Parser, as well as their implementations, are package-protected * classes at the end of this source file primarily for examples' sake. In the real world, you'd break all seven * of them into their own source files. *
*This class is licensed under the Apache Software License, available at * http://www.apache.org/licenses/LICENSE-2.0.html. * No guarantees are made for fitness of use for any purpose whatsoever, and no responsibility is assigned to * its author for the results of any use. Note section 7 of the ASL 2.0, please, and if someone dies because of * this class, I'm sorry, but it's not my fault. I warned you. */ public class ParseDate { Formatter formatter; Parser parser; public ParseDate(Formatter formatter, Parser parser) { this.formatter = formatter; this.parser = parser; } public Formatter getFormatter() { return formatter; } public void setFormatter(Formatter formatter) { this.formatter = formatter; } public Parser getParser() { return parser; } public void setParser(Parser parser) { this.parser = parser; } public static void main(String[] args) throws Exception { Formatter jodaFormatter = new JodaFormatter(), javaUtilFormatter = new JavaUtilFormatter(), bruteForceFormatter = new BruteForceFormatter(); Parser jodaParser = new JodaParser(), javaUtilParser = new JavaUtilParser(); String sampleDate = "Aug 28, 2008 08:00:12"; ParseDate pd = new ParseDate(jodaFormatter, jodaParser); pd.display(sampleDate); System.out.println("-------------------"); pd.setFormatter(javaUtilFormatter); pd.setParser(javaUtilParser); pd.display(sampleDate); System.out.println("-------------------"); pd.setFormatter(bruteForceFormatter); pd.display(sampleDate); } private void display(String sampleDate) throws Exception { System.out.println(formatter.format(parser.getMillis(sampleDate))); } } interface Formatter { String format(long millis); } interface Parser { long getMillis(String sampleDate) throws Exception; } class JodaFormatter implements Formatter { static DateTimeFormatter fmt = DateTimeFormat.forPattern("'Month :' MM\n'Day :' dd\n'Year :' yyyy\n" + "'Hour :' HH\n'Minute:' mm\n'Second:' ss"); public String format(long millis) { return fmt.print(millis); } } class JodaParser implements Parser { static DateTimeFormatter fmt = DateTimeFormat.forPattern("MMM dd, yyyy HH:mm:ss"); public long getMillis(String sampleDate) { return fmt.parseDateTime(sampleDate).getMillis(); } } class JavaUtilFormatter implements Formatter { static SimpleDateFormat sdf = new SimpleDateFormat("'Month :' MM\n'Day :' dd\n'Year :' yyyy\n" + "'Hour :' HH\n'Minute:' mm\n'Second:' ss"); public String format(long millis) { return sdf.format(new Date(millis)); } } class BruteForceFormatter implements Formatter { public String format(long millis) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); Calendar c = Calendar.getInstance(); c.setTimeInMillis(millis); pw.printf("Month : %02d\nDay : %02d\nYear : %04d\nHour : %02d\nMinute: %02d\nSecond: %02d\n", c.get(Calendar.MONTH) + 1, // because months start with 0, and the external representation doesn't c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.YEAR), c.get(Calendar.HOUR), c.get(Calendar.MINUTE), c.get(Calendar.SECOND)); return sw.getBuffer().toString(); } } class JavaUtilParser implements Parser { static SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss"); public long getMillis(String sampleDate) throws ParseException { return sdf.parse(sampleDate).getTime(); } }