- 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.
- package commonsUtils;
- import java.util.Date;
- import org.apache.commons.lang.time.DateFormatUtils;
- public class SampleRunnable {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Date today = new Date();
- String firstPattern = DateFormatUtils.format(today,"MM/dd/yyyy HH:mm:ss");
- System.out.println("Date to String in MM/dd/yyyy HH:mm:ss::"+firstPattern);
- String secondPattern = DateFormatUtils.format(today,"dd/MM/yyyy HH:mm:ss");
- System.out.println("Date to String in dd/MM/yyyy HH:mm:ss::"+secondPattern);
- String thirdPattern = DateFormatUtils.format(today,"MM/dd/yyyy");
- System.out.println("Date to String in MM/dd/yyyy::"+thirdPattern);
- String fourthPattern = DateFormatUtils.format(today,"dd/MM/yyyy");
- System.out.println("Date to String in dd/MM/yyyy::"+fourthPattern);
- }
- }
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