7

I have a date with the format 2012-02-02(yyyy-MM-dd).

For example if todays date is 2012-02-02 i need to add one and a half days to it which would make it 2012-02-03 06:00:00.0.

And if i have a number of dates of the following format 2012-02-03 06:30:00.0(yyyy-MM-dd HH:MM:SS.SSS) , i need to compare if all these dates are less than,greater than or equal to the date to which one and a half days were added above.

The comparison should also take care of the hours while comparing if the dates are less than,greater than or equal or equal to the other date and time.

How do i achieve the same.

skaffman
  • 398,947
  • 96
  • 818
  • 769
gautam vegeta
  • 653
  • 6
  • 13
  • 28

4 Answers4

5

Simple arithmetic approach (faster)

  1. Parse the date using SimpleDateFormat that creates a Date object
  2. Use Date.getTime() to return the UTC value in long
  3. Convert 1 and half days to millis (1.5 Days = 129600000 Milliseconds) and add it to previous step
  4. Use >, < and == or after(), before() and equals() if you want to use Date object itself

API approach (slower)

  1. Use Calendar
  2. add(...) method for adding 1 and half day
  3. use before(), after() and equals() methods of Calendar
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • The simple arithmetic approach ignores time zones (well, uses UTC). So "plus 1.5 days" may lead to unexpected results in the face of daylight saving time. You may want to mention this in your answer. – derobert Feb 03 '12 at 09:50
3

well so i hope this will give you a clear idea. Calendar Documentation and SimpleDateFormat Documentaion

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String aDateString = "2012-02-02";
Date date = sdf.parse(aDateString);
System.out.println("reference date:"+date);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, 36);
System.out.println("added one and half days to reference date: "+cal.getTime());

String newDateString = "2012-02-03 06:30:00.0";
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date newDate = sdf.parse(newDateString);
System.out.println("new date to compare with reference date : "+newDate);

Calendar newCal = Calendar.getInstance();
newCal.setTime(newDate);

if(cal.after(newCal)){
    System.out.println("date is greater than reference that.");
}else if(cal.before(newCal)){
    System.out.println("date is lesser than reference that.");
}else{
    System.out.println("date is equal to reference that.");
}

OUTPUT :

reference date:Thu Feb 02 00:00:00 IST 2012
added one and half days to reference date: Fri Feb 03 12:00:00 IST 2012
new date to compare with reference date : Fri Feb 03 06:30:00 IST 2012
date is greater than reference that.
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
2

You need to use Joda date time API.

 String strDate="2012-02-02";
 DateTime dateTime=DateTime.parse(strDate);

 DateTime newDateTime=dateTime.plusHours(18); 
 System.out.println(dateTime);
 System.out.println(newDateTime);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2
  • Use SimpleDateFormat to convert String to Date

  • Set date to Calendar instance

  • use calendar.add(Calendar.HOUR, 36)

Also See

  • Joda Time API
jmj
  • 237,923
  • 42
  • 401
  • 438