skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Sunday, October 14, 2012

MM_DD_YYYY Date Formatter in Java

Posted by Raju Gupta at 12:00 AM – 0 comments
 
This will formte the Date to the MM-DD-YYYY format

 public static String formatDate(Date date) {
//create new calendar instance
  Calendar cal1 = Calendar.getInstance();
  String dob = "";
//check for null
  if(date==null)
//if null create new date instance
   date = new Date();
   cal1.setTime(date);
   if (cal1 != null) {
    dob = "" + ((cal1.get(Calendar.MONTH)) + 1);
    dob += "-" + cal1.get(Calendar.DATE);
    dob += "-" + cal1.get(Calendar.YEAR);
   }
 
  return dob;
 }


Labels: Date Example , Java DateFormat Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger