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

Popular posts from this blog

ADF : Get Current Logged User Name

OAF : Get Current Row in Table

ADF : Working with ViewCriteria