Sunday, February 23, 2014

DateFormating dot java

package util;

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateFormating {
   
    public Date getDate(String dateFormate, String dateString) {
        //03/06/2012
        DateFormat formater = new SimpleDateFormat(dateFormate);
       
        try {
               Date datevalue= formater.parse(dateString);
               return  datevalue;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
   
   
    public String getDateTime(Date dateString) {
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
        String date = format.format(dateString);
        return date;
    }
   
    public String getDate(String dateString) {
        try {
            if(!dateString.isEmpty()){
                String oldDateFormat="yyyy-MM-dd";
                String newDateFormat="MM/dd/yyyy";
                SimpleDateFormat formater = new SimpleDateFormat(oldDateFormat);
                Date datevalue= formater.parse(dateString);
                formater.applyPattern(newDateFormat);
                return  formater.format(datevalue);
            }else{
                return "";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
   
    //format a date for database
    //@ getDate     this will take date from Date instance
    public Date dateFormatDB(String getDate) {
        DateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
        Date datevalue;
        try {
            datevalue = formater.parse(getDate);
            return datevalue;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
   
  //format a date for the user
    //@ getDate     this will take date from Date instance
    public String dateFormatUser(Date getDate) {
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        String date = format.format(getDate);
        return date;
    }
   
    //format a date for the user
    //@ getDate     this will take date from Date instance
    public String dateFormatUser1(Date getDate) {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        String date = format.format(getDate);
        return date;
    }
  //format a date for the user
    //@ getDate     this will take date from Date instance
    public String dateFormatDBOracle(Date getDate) {
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String date = format.format(getDate);
        return date;
    }
    public String dateFormatDBOracleD(Date getDate) {
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        String date = format.format(getDate);
        return date;
    }
   
    public int dateCount(String dateFrom,String dateTo){
        Date fromDate=dateFormatDB(dateFrom);
        Date toDate=dateFormatDB(dateTo);
        int dateFromInt=fromDate.getDate();
        int dateToInt=toDate.getDate();
        int totalDays=dateFromInt-dateToInt;
        return totalDays;
    }
   
   
    public Date getCalendarDate(String dateFormate, String dateString) {  //Only for  "dd/MM/yyyy"
       
        Calendar cal= Calendar.getInstance();
        Date date=null;
        DateFormat formater = new SimpleDateFormat(dateFormate);

              try {
                       Date dateValue= formater.parse(dateString);
                    cal.setTime(dateValue);  
                    date=  cal.getTime();                                 
              } catch (ParseException e) {
                  e.printStackTrace();
                 
              }          
         return  date;
      
    }
   
    public static void main(String[] args){
       
        DateFormating dfff= new DateFormating();
       
       
//    System.out.println(    dfff.getCalendarDate("MM/dd/yyyy hh:mm aa", "03/05/2013 03:14 PM"));
        System.out.println(    dfff.dateFormatDBOracle(new Date()));
       
       
       
    }

}

No comments:

Post a Comment