06 September, 2012

Format Dates in Java

I posted before about different date classes in Java and conversions between them.

Sometimes I want to display date type in different formats in Java like DD-MM-RRRR , DD-MON-YYYY , MON-YY .... etc

I will post about about different Java Classes that can be used for this purpose.
1- java.text.SimpleDateFormat
2- java.text.DateFormat
3- org.apache.commons.lang.time.DateFormatUtils


1- SimpleDateFormat Class
I will use java.text.SimpleDateFormat class for formatting Date and time also
You can use anyone from below patterns
Example
   public static void main(String[] args) {  
   
     String pattern = "dd-MM-yyyy";  
   
     SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);  
   
     String dateAfterPattern = simpleDateFormat.format(new java.util.Date());  
   
     System.out.println("dateAfterPattern=" + dateAfterPattern);  
   
   }  
   
2- DateFormat ClassI will use java.text.DateFormat class for formatting too
Example
     String dateAfterFormat = DateFormat.getDateInstance(DateFormat.MEDIUM).format((new java.util.Date()));  
   
     System.out.println("dateAfterPattern=" + dateAfterFormat);  


3- DateFormatUtils Class
I will use org.apache.commons.lang.time.DateFormatUtils helper class from formatting date and time.

Example
   public static void main(String[] args) {  
   
     String pattern = "dd-MM-yyyy";  
   
     String dateAfterPattern = DateFormatUtils.format((new java.util.Date()), "MM/dd");  
   
     System.out.println("dateAfterPattern=" + dateAfterPattern);  
   
   }  

Thanks

7 comments:

  1. Another option is to use Joda Time library (http://joda-time.sourceforge.net/userguide.html#Formatters).

    ReplyDelete
  2. You can also use String.format() (see http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#dt)

    ReplyDelete
  3. Or you can simply use PrettyTime
    http://ocpsoft.org/prettytime/

    ReplyDelete
  4. in 3rd example String pattern = "dd-MM-yyyy";
    is unused variable

    ReplyDelete
  5. please write somewhere that SimpleDateFormat is NOT THREADSAFE; I wouldn't want someone who learns it from this post to use it a public static final...
    Also Apache's commons Lang FastDateFormat is worth mentioning since it is almost a drop-in replacement for SimpleDateFormat and IS threadsafe.

    ReplyDelete
  6. Thanks for all the information ,it was very helpful i really like that you are providing information on core and advance java ,being enrolled in
    advance and core java http://www.wiziq.com/course/1779-core-and-advance-java-concepts i was looking for such information on advance and core java and your information helped me a lot. I really like that you are providing such information. Thanks.

    ReplyDelete
  7. Welcome to our website
    java training that is the largest professional java training site where java,
    java training, online java course, java training in Virginia,
    java programming are available

    more detail.......
    online java course
    java programming
    java training in Virginia

    ReplyDelete

How to Pass Parameters to ActionListener in ADF

In some cases, it is required to pass a value to ActionListener of ADF Button. The method that can be invoked by actionListeners has only...