skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Date Util in Java

Posted by Admin at 8:58 AM – 0 comments
 

This is basically a reusable code which helps us to format the Dates as our wish. These methods save us a lot of time which we might waste in searching code for these basic validations.


package com.test;

import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class DateUtil {

 /**
  *  
  */
 public DateUtil() {
  super();
  // TODO Auto-generated constructor stub
 }


 
 public long getDays(String dte1, String dte2, String dteFormat) {
  long rtnDays = 0;
  // dteFormat example - yyyy-MM-dd
  try {
   DateFormat format = new SimpleDateFormat(dteFormat);
   rtnDays = (((Date) format.parse(dte2)).getTime() - ((Date) format
     .parse(dte1)).getTime())
     / (60 * 60 * 24 * 1000);
  } catch (Exception e) {
   System.out.println("getDays() Exception " + e.getMessage());
  }
  return rtnDays;
 }

 public String getDate(String format) {
  String rtnStr = "";
  Format formatter = null;
  try {
   formatter = new SimpleDateFormat(format);
   rtnStr = formatter.format(new Date());
  } catch (Exception e) {
   System.out.println("getDate() Exception " + e.toString());
  }
  return rtnStr;
 }

 
 public Date getCurrentDate(String dateFormat) {
  Date rtnDte = null;
  try {
   SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
   rtnDte = sdf.parse(new Date().toString());
  } catch (Exception e) {
   System.out.println("getCurrentDate() Exception " + e.toString());
  }
  return rtnDte;
 }
 
 
 public Date getDate(String dateFormat, String dateStr) {
  Date rtnDte = null;
  try {
   SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
   rtnDte = sdf.parse(dateStr);
  } catch (Exception e) {
   System.out.println("getDate() Exception " + e.toString());
  }
  return rtnDte;
 }

 

 public String getDateString(String dateFormat, String dateStr) {
  String rtnStr = null;
  try {
   SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
   rtnStr = sdf.format(sdf.parse(dateStr));
  } catch (Exception e) {
   System.out.println("getDateString() Exception " + e.toString());
  }
  return rtnStr;
 }

 
  public String getDateTime(String dateFormat, Date aDate) {
        SimpleDateFormat df = null;
        String rtnStr = "";

        if (aDate == null) {
            System.out.println("aDate is null!");
        } else {
            df = new SimpleDateFormat(dateFormat);
            rtnStr = df.format(aDate);
        }

        return (rtnStr);
    }

}


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