-3

I want to compare date1(2013/1/21 21:22:30) with date2(now) in one method

I want to get how much date,hours,minutes and second Remaining form Date 1

String mDate,mHour,mMinute,mSecond;

DateComparedateTime(String Date1)
{
 // compare  Date1 with now 
   mDate = Difference date with Date1 and Now;
   mHour = Difference hour with Date1 and Now;
   mMinute = Difference minute with Date1 and Now;
   mSecond = Difference second with Date1 and Now;
}

how can create this method ?

Saeed Hashemi
  • 984
  • 4
  • 22
  • 55

3 Answers3

9
Calendar currentDate = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


    String dateNow = sdf.format(currentDate.getTime());
      Date today =  new Date(dateNow);
      Date finalDay = null;
      finalDay = (Date) sdf.parse("MM/dd/yyyy");
    int numberOfDays=(int)((finalDay.getTime()-today.getTime())/(3600*24*1000));
RadAl
  • 404
  • 5
  • 23
  • 1
    how can get numberOfHours and numberOfMinuts and NumberOfSecond? – Saeed Hashemi Jan 15 '13 at 12:48
  • 'int numberOfDays=(int)((finalDay.getTime()-today.getTime())/(1000));' will give you the number of seconds . int numberOfDays='(int)((finalDay.getTime()-today.getTime())/(3600*1000));' will give you number of hours .. – RadAl Jan 15 '13 at 12:59
  • I have a Date with string type and want to compare now – Saeed Hashemi Jan 15 '13 at 13:38
5

you can use Date.compareTo() method.

DateComparedateTime(String date1)
{
    System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date1).compareTo(new Date()))
 }
PermGenError
  • 45,977
  • 8
  • 87
  • 106
3

this might help you...follow it carefully..

Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
System.out.println("calender time in millies" + c.getTimeInMillis());
System.out.println("calender time in millies" + c.getTime());
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(c.getTimeInMillis());
System.out.println("time in seconds :: " + diffInSec);
ashish.n
  • 1,234
  • 14
  • 30