|
-
Sep 25th, 2002, 10:49 AM
#1
Thread Starter
Hyperactive Member
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.
-
Sep 25th, 2002, 12:09 PM
#2
Hyperactive Member
how do you know it is not 2001?
-
Sep 25th, 2002, 12:15 PM
#3
Thread Starter
Hyperactive Member
There is some assumption required here.
I will never be processing a file older than a year.
-
Sep 25th, 2002, 12:55 PM
#4
Hyperactive Member
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
-
Sep 25th, 2002, 12:59 PM
#5
Hyperactive Member
do you know how to get the date from the file name?
do you know how to get todays date?
-
Sep 26th, 2002, 11:31 AM
#6
Thread Starter
Hyperactive Member
Yes and Yes.
I'm just not a Java programmer so the syntax of comparing the 2 is not familiar to me.
-
Sep 26th, 2002, 12:48 PM
#7
Hyperactive Member
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
}
-
Sep 26th, 2002, 02:19 PM
#8
Thread Starter
Hyperactive Member
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?
-
Sep 26th, 2002, 03:28 PM
#9
Hyperactive Member
Calendar is an easy way to get the date, don't get too much easier.
-
Sep 26th, 2002, 03:38 PM
#10
Thread Starter
Hyperactive Member
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;
-
Sep 26th, 2002, 03:51 PM
#11
Hyperactive Member
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?
-
Sep 26th, 2002, 03:53 PM
#12
Hyperactive Member
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?
-
Sep 26th, 2002, 04:02 PM
#13
Hyperactive Member
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;
-
Sep 27th, 2002, 10:40 AM
#14
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|