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

9 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. 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
  7. Exactly, you're very kind of us about comment!.
    代写cs 北美

    ReplyDelete
  8. Enroll for Core Java Training and employ your talent on different IDEs. Earn about the very best 5 IDEs to apply Java. Online Advanced JAVA Training

    ReplyDelete

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...