Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding out the Date two weeks ago 2

Status
Not open for further replies.

Kafenated

Programmer
May 8, 2003
11
IN
I have a java app where I need to encode a stop date into a URL. I can get todays date but need to figure out the date 14 days ago from Today.

Any Ideas Thanks in Advance
 
Code:
java.util.Date dt = java.util.Calendar.getInstance().getTime();
 long ddiff = 14 * 24 * 60 * 60 * 1000;
 long past = dt.getTime() - ddiff;
 dt = new java.util.Date( past);
 System.out.println("14 days ago was: " + dt.toLocaleString());

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
That is helpful but how do exclude the time from the date
 
Another ways could be:

Code:
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.ParseException;
import java.text.DateFormat;



class FindStopDate
{
	public static void main(String[] args) throws Exception
	{
		final int NUMBER_OF_DAYS_AGO = -14;
		DateFormat format = new SimpleDateFormat("yyyy-mm-dd");
		format.setLenient(true);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		cal.add(Calendar.DATE, NUMBER_OF_DAYS_AGO);
		String stopDate = DateFormat.getDateInstance().format(cal.getTime());
		System.out.println(stopDate);

	}
}
 
Code:
cal.add(Calendar.DATE, NUMBER_OF_DAYS_AGO);

That's better... don't know why i didn't post that [bugeyed]

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top