Results 1 to 14 of 14

Thread: Calculate the year

  1. #1

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

    Calculate the year

    I receive files that have the date embedded in their name, but it does not include the year.

    I am trying to figure out how I can assign the year if I provide the month and day.

    For example, I have a file named "myfile0802" and I want to know that it is 8/2/2002.

    If today were 1/1/2003 and I had "myfile1225" I'd want to know that the date should be 12/25/2002 and not 12/25/2003.

    Any help is greatly appreciated!

    If it helps, I am looking to get the date in yyyymmdd format.
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    how do you know it is not 2001?

  3. #3

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    There is some assumption required here.

    I will never be processing a file older than a year.
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  4. #4
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Here is what you need for an algorithm then

    check date of file against todays date
    if the filename has a later date in the calendar up to December then you know it is from last year. If it is less than todays date back to january than you know it is from the current year

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    do you know how to get the date from the file name?
    do you know how to get todays date?

  6. #6

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

    I'm just not a Java programmer so the syntax of comparing the 2 is not familiar to me.
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  7. #7
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    this should work, dont have java at work to test it though.

    get ints of the filemonth, fileday, todaymonth, and todayday

    Code:
    if(filemonth < todaymonth || (filemonth==todaymonth && fileday<=todayday))
    {
       // then the file would be the current year
    }
    else
    {
        //  it is from last year
    }

  8. #8

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    This is the only way I know to get a date in Java:

    Month
    Code:
    			java.util.Calendar  gc;
    			java.util.Date  curDate;
    			java.util.Date  RunDate;
    			
    			String  strCurDate = null;
    			
    			gc = GregorianCalendar.getInstance ( );
    			curDate = gc.getTime ( );	
    			SimpleDateFormat sdf1 = new SimpleDateFormat ( "MM" );
    			java.util.Calendar rcal = null;
    			rcal = GregorianCalendar.getInstance ( );
    			rcal.roll (rcal.DATE, false);//Date-1
    			RunDate = rcal.getTime ( );
    			strDateRunningFor = sdf1.format ( RunDate );
    Day
    Code:
    			java.util.Calendar  gc;
    			java.util.Date  curDate;
    			java.util.Date  RunDate;
    			
    			String  strCurDate = null;
    			
    			gc = GregorianCalendar.getInstance ( );
    			curDate = gc.getTime ( );	
    			SimpleDateFormat sdf1 = new SimpleDateFormat ( "dd" );
    			java.util.Calendar rcal = null;
    			rcal = GregorianCalendar.getInstance ( );
    			rcal.roll (rcal.DATE, false);//Date-1
    			RunDate = rcal.getTime ( );
    			strDateRunningFor = sdf1.format ( RunDate );
    Is there an easier way?
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  9. #9
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Calendar is an easy way to get the date, don't get too much easier.

  10. #10

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    Here's what I ended up with. So far, all the tests have worked...

    Thanks for all the help!

    Code:
    System.out.println("File Supplied.  Getting date from the file.");
    //SPRINTPCS0.I80140.08210153481
    String strFileIn = new String();
    strFileIn = args[1];
    int intFileMonth;
    int intFileDay;
    strDateRunningFor = args[1];
    intFileMonth = Integer.parseInt(strFileIn.substring(18,20));
    intFileDay = Integer.parseInt(strFileIn.substring(20,22));	
    System.out.println("intFileMonth " + intFileMonth + " intFileDaye " + intFileDay);	
    int intCurrentMonth;
    int intCurrentDay;
    int intCurrentYear;
    SimpleDateFormat formatterMonth = new SimpleDateFormat("MM");
    SimpleDateFormat formatterDay = new SimpleDateFormat("dd");
    SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy");
    Date currenttime = new Date();
    intCurrentMonth = Integer.parseInt(formatterMonth.format(currenttime));
    intCurrentDay = Integer.parseInt(formatterDay.format(currenttime));
    intCurrentYear = Integer.parseInt(formatterYear.format(currenttime));
    String strYear = new String();
    if(intFileMonth < intCurrentMonth || (intFileMonth==intCurrentMonth && intFileDay<=intCurrentDay))
    {
    	//Current Year
    	strYear = "" + intCurrentYear;
    }
    else
    {
    	//Previous Year
    	strYear = "" + (intCurrentYear-1);
    }
    String strMonth = new String();
    String strDay = new String();
    
    if(intFileMonth<10)
    {
    	strMonth = "0" + intFileMonth;
    }
    else
    {
    	strMonth = "" + intFileMonth;
    }
    
    if(intFileDay < 10)
    {
    	strDay = "0" + intFileDay;
    }
    else
    {
    	strDay = "" + intFileDay;
    }
    //System.out.println(strYear + "" + strMonth + "" + strDay);
    strDateRunningFor = strYear + "" + strMonth + "" + strDay;
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  11. #11
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Cool

    Some things you might want to look at.

    1) Do you need the string strFileIn? Or can you use strDateRunningFor to get the date information from since you don't modify it until the end.

    2) Might want to put all your variables you declare at the top of your function, just makes reading code a little easier, personal preference though.

    3) I see you checked out the Date class very nice job on that one, I was curious to see if you find it.

    4) when you are setting up the strYear why do you need to add nothing to the year?

  12. #12
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    An interesting test case would be for when the file date matches todays date. Does it give what you want to see? I set the if up so it would say it was the current year is that what you want?

  13. #13
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    try this...

    Code:
    System.out.println("File Supplied.  Getting date from the file.");
    //SPRINTPCS0.I80140.08210153481
    
    // Variable declars //
    int intFileMonth;
    int intFileDay;
    int intCurrentMonth;
    int intCurrentDay;
    int intCurrentYear;
    String strDateRunningFor = new String();
    String strYear = new String();
    String strMonth = new String();
    String strDay = new String();
    Date currenttime = new Date();  // get todays date
    // formats for date
    SimpleDateFormat formatterMonth = new SimpleDateFormat("MM");
    SimpleDateFormat formatterDay = new SimpleDateFormat("dd");
    SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy");
    
    strDateRunningFor = args[1];  // get filename information
    strMonth = strDateRunningFor.substring(18,20)  // get string for month
    strDay = strDateRunningFor.substring(20,22)     // get string for day
    intFileMonth = Integer.parseInt(strMonth);        // get month from filename
    intFileDay = Integer.parseInt(strDay);	           // get day from filename
    
    // temp dump out of month and day
    System.out.println("intFileMonth " + intFileMonth + " intFileDaye " + intFileDay);	
    
    // parse todays date to get month, day, and year
    intCurrentMonth = Integer.parseInt(formatterMonth.format(currenttime));
    intCurrentDay  = Integer.parseInt(formatterDay.format(currenttime));
    intCurrentYear = Integer.parseInt(formatterYear.format(currenttime));
    
    // compute the year for the filename
    if(intFileMonth < intCurrentMonth || (intFileMonth==intCurrentMonth && intFileDay<=intCurrentDay))
    {
    	//Current Year so just set year string to current year
    	strYear = intCurrentYear;
    }
    else
    {
    	//Previous Year, so we need to just subtract a year off the current year
    	strYear = (intCurrentYear-1);
    }
    
    //System.out.println(strYear + "" + strMonth + "" + strDay);
    strDateRunningFor = strYear + "" + strMonth + "" + strDay;

  14. #14
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    The above cuts out need for the if's for adding the 0 back into the string for the day and month.

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