• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

date question

i have two strings.
String date0 = "2005-01-16";
String today = "2005-04-09";

How can i calculate todays date - 2 days ?
and how can i compare the result with date0.

I want to pring  "before  today - 2 days" or "after today - 2 days".

Thx
0
ap458889
Asked:
ap458889
1 Solution
 
sudhakar_koundinyaCommented:
this example will help you

http://www.javaalmanac.com/egs/java.util/GetAge.html

Regards
Sudhakar
0
 
zzynxSoftware engineerCommented:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
Date todayMinusTwo = cal.getTime();

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date d0 = null;
try {
   d0 = fmt.parse(date0);
} catch (ParseException ex) {
}

if (d0.before(todayMinusTwo))
   System.out.println("Before today - 2 days");
else if (d0.after(todayMinusTwo))
   System.out.println("After today - 2 days");
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now