PDA

Click to See Complete Forum and Search --> : Yesterday's Date? - RESOLVED!


rockies1
Jul 1st, 2003, 02:34 PM
I have some code that works to calculate yesterday's date and it has worked great, until today.

Anyone have something that works correctly?

Here's mine:
public static String CalcYestDate()
{
//Set up date to run for
java.util.Calendar gc;
java.util.Date curDate;
String strCurDate = null;
String fileDate = new String();

gc = GregorianCalendar.getInstance ( ); // Gets the current Calendar instance
gc.roll(gc.DATE,false);

curDate = gc.getTime ( ); // Gets the Date time part of gc
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyyMMdd" ); //sets up the correct format for the date
fileDate = sdf2.format ( curDate ); //Changes the date into the correct date format

return(fileDate);
}//End CalcYestDate

When I ran it today it returned 20030731 instead of 20030630.

Thanks!

Morgan

Dillinger4
Jul 1st, 2003, 03:08 PM
import java.util.Date;
import java.text.DateFormat;


public class D{
public static void main(String[] args){

long day = 60 * 60 * 1000 * 24;

Date today = new Date(System.currentTimeMillis());
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);

System.out.println(df.format(today));

today.setTime(today.getTime() - day);

System.out.println(df.format(today));
}
}

rockies1
Jul 1st, 2003, 03:19 PM
Worked perfectly, thank you!!

:D

Dillinger4
Jul 1st, 2003, 03:21 PM
No problem. Anytime. :)