Format Date in java

  • DateFormatUtils.format Utility method is used to format date from date to string.
  • You can use this method with the help of commons-lang-2.6.jar.
  • You can download this jar from Here.

  1. package commonsUtils;
  2. import java.util.Date;

  3. import org.apache.commons.lang.time.DateFormatUtils;

  4. public class SampleRunnable {

  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. Date today = new Date();
  8. String firstPattern = DateFormatUtils.format(today,"MM/dd/yyyy HH:mm:ss");
  9. System.out.println("Date to String in MM/dd/yyyy HH:mm:ss::"+firstPattern);
  10. String secondPattern = DateFormatUtils.format(today,"dd/MM/yyyy HH:mm:ss");
  11. System.out.println("Date to String in dd/MM/yyyy HH:mm:ss::"+secondPattern);
  12. String thirdPattern = DateFormatUtils.format(today,"MM/dd/yyyy");
  13. System.out.println("Date to String in MM/dd/yyyy::"+thirdPattern);
  14. String fourthPattern = DateFormatUtils.format(today,"dd/MM/yyyy");
  15. System.out.println("Date to String in dd/MM/yyyy::"+fourthPattern);
  16. }

  17. }


Output :

 Date to String in MM/dd/yyyy HH:mm:ss::07/17/2013 15:06:30
 Date to String in dd/MM/yyyy HH:mm:ss::17/07/2013 15:06:30
 Date to String in MM/dd/yyyy::07/17/2013
 Date to String in dd/MM/yyyy::17/07/2013



No comments:

Post a Comment