Results 1 to 3 of 3

Thread: Calculate Yesterday's Date

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Calculate Yesterday's Date

    I have the following code for calculating yesterday's date:
    Code:
    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 ( "MMddyy" ); //sets up the correct format for the date
    	fileDate = sdf2.format ( curDate );  //Changes the date into the correct date format
    			
    	return(fileDate);
    }//End CalcYestDate
    Yesterday, however, it calculated 083104 instead of 073104.

    Anyone know of a more accurate method?
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It's pretty stupid that Calendars getTime() returns a Date object
    when most of the methods within the Date class are deprecated.
    Anyway.....
    Code:
     import java.util.*; 
     import java.text.*; 
    
     public class CalDate{
      public static void main(String[] args){
        Calendar  gc;
        Date  curDate;
        String  strCurDate = null;
        String fileDate = new String();
        SimpleDateFormat sdf2 = new SimpleDateFormat("MMddyy");
        gc = GregorianCalendar.getInstance();
    
        curDate = gc.getTime();
        fileDate = sdf2.format(curDate);
        System.out.println(fileDate);
    
        gc.setTimeInMillis(gc.getTimeInMillis() - 86400000);
    
        curDate = gc.getTime();
        fileDate = sdf2.format(curDate);
        System.out.println(fileDate);
      } 
     }

  3. #3

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    Eclipse doesn't like
    Code:
    gc.setTimeInMillis
    I found this code that I had for another old project that I'll try and modify:
    Code:
    import java.text.SimpleDateFormat;
    import java.util.*; 
    import java.text.*;
    
    public class DateTest
    {
    
        public static void main(String[] args)
        {
    		for(int i = 0;i<365;i++)
    		{
    		    System.out.println(DateRoller("20040804",i));
    		}
        }
        public static String DateRoller(String strDate, int intDaysToRoll)
        {
        	java.util.Date  curDate;
        	SimpleDateFormat sdf2 = new SimpleDateFormat ("yyyyMMdd");
        	Date dtTemp = new Date();		
        	try
        	{
        		dtTemp = sdf2.parse(strDate);			
        	}
        	catch(ParseException e){}
        	
        	int y = dtTemp.getYear()+1900;
        	int m = dtTemp.getMonth();
        	int d = dtTemp.getDate();
        	java.util.GregorianCalendar rcal = new GregorianCalendar(y,m,d);
        	curDate = rcal.getTime ( );
        	long msYDate = 0;
        	long msCDate = curDate.getTime();
        	msYDate = msCDate;
        	//Transforms the current date into miliseconds
        	for(int i = 0;i<intDaysToRoll;i++)
        	{
        		msYDate = msYDate - 86400000; // Subtracts off X day's worth of miliseconds
        	}
        	java.util.Date yestDate = new java.util.Date (msYDate); 			
        	return sdf2.format(yestDate);
        } //End DateRoller
    }
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width