Saturday, 10 May 2014

Date Time Examples

Get Time Before Week:-

Calendar calNow = Calendar.getInstance();        
calNow.add(Calendar.DAY_OF_MONTH, -7);
Date dateBeforeAWeek = calNow.getTime();    
Timestamp beforeWeekTime = new Timestamp(dateBeforeAWeek.getTime());

Convert timestamp to string:-

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);    
           String stringTimestamp =  sdf.format(new Date(timestamp.getTime()));

Time stamp to date:-

               new Date(timestamp.getTime());

Time stamp is equals:-

            (timestamp1.compareTo(timestamp2)==0)

Timestamp greater:-

           (timestamp1.compareTo(timestamp2)==1)

Date To time stamp:-

               new Timestamp(date.getTime());

Convert String Date To Timestamp:-

    SimpleDateFormat dateFormat;
Date date;
Timestamp timestamp=null;
try {
dateFormat = new SimpleDateFormat(dateformat);
date = dateFormat.parse(sDate);
timestamp = new Timestamp(date.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
  Date is equals:-
         
          (date1.compareTo(date2)==0)


Date comparison:- 
               date1 = Mon May 12 01:55:00 IST 2014
               date2 = =Sun May 11 11:06:29 IST 2014

                date1.before(date2); result is FALSE
                date1.after(date2);  result is TRUE