|
-
Nov 30th, 2004, 12:36 PM
#1
Thread Starter
Hyperactive Member
Odd Date format conversion
I have a file that I receive from another company. In it there are dates in this format:
2004001
or
2004294
The 2004 is the year, obviously, but the next 3 digits give me the month & day.
001 = 1/1
002 = 1/2
003 = 1/3
...
059 = 2/28
060 = 2/29 in a leap year, but 3/1 in a regular year
061 = 3/1 in a leap year, but 3/2 in a regular year
and so on.
I need to be able to convert the date I receive into a date that looks like this:
YYYYMMDD or 20041130
Any suggestions?
Thanks!
-
Nov 30th, 2004, 12:46 PM
#2
First, you slice off the year. Then you check if it's a leap year.
Code:
boolean isleap(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
Then you walk the months and substract their days from the day-in-year count until you reach a point where you have less or equal left than the next month has days. This gives you both the month and the day in it.
Edit: never mind the above. Just thought of a better way, since this is Java (I'm too fixed on C++).
Just slice the date into year and days.
Code:
java.util.Calendar c = new java.util.GregorianCalendar();
c.set(java.util.Calendar.YEAR, year);
c.set(java.util.Calendar.DAY_OF_YEAR, days);
java.text.DateFormat df = new java.text.SimpeDateFormat("yyyyMMdd");
String result = df.format(c.getTime());
Last edited by CornedBee; Nov 30th, 2004 at 12:54 PM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 30th, 2004, 04:15 PM
#3
Thread Starter
Hyperactive Member
Thanks for the suggestion. This is what I ended up going with. Not real pretty, but it works.
Code:
public static boolean isLeap(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}//End isLeap
public static String convertDate(String strDateIn)
{
String strToReturn = "";
String strYear = strDateIn.substring(0,4);
boolean blnIsLeap = isLeap(Integer.parseInt(strYear));
int intDays = Integer.parseInt(strDateIn.substring(4));
if(blnIsLeap)
{
if(intDays < 32)
{
//January
strToReturn = strYear + "01" + padString("" + intDays,2,"0",true);
}
else if(intDays < 61)
{
//February
strToReturn = strYear + "02" + padString("" + (intDays-31),2,"0",true);
}
else if(intDays < 92)
{
//March
strToReturn = strYear + "03" + padString("" + (intDays-60),2,"0",true);
}
else if(intDays < 122)
{
//April
strToReturn = strYear + "04" + padString("" + (intDays-91),2,"0",true);
}
else if(intDays < 153)
{
//May
strToReturn = strYear + "05" + padString("" + (intDays-121),2,"0",true);
}
else if(intDays < 183)
{
//June
strToReturn = strYear + "06" + padString("" + (intDays-152),2,"0",true);
}
else if(intDays < 214)
{
//July
strToReturn = strYear + "07" + padString("" + (intDays-182),2,"0",true);
}
else if(intDays < 245)
{
//August
strToReturn = strYear + "08" + padString("" + (intDays-213),2,"0",true);
}
else if(intDays < 275)
{
//September
strToReturn = strYear + "09" + padString("" + (intDays-244),2,"0",true);
}
else if(intDays < 306)
{
//October
strToReturn = strYear + "10" + padString("" + (intDays-274),2,"0",true);
}
else if(intDays < 336)
{
//November
strToReturn = strYear + "11" + padString("" + (intDays-305),2,"0",true);
}
else
{
//December
strToReturn = strYear + "12" + padString("" + (intDays-335),2,"0",true);
}
}
else
{
if(intDays < 32)
{
//January
strToReturn = strYear + "01" + padString("" + intDays,2,"0",true);
}
else if(intDays < 60)
{
//February
strToReturn = strYear + "02" + padString("" + (intDays-31),2,"0",true);
}
else if(intDays < 91)
{
//March
strToReturn = strYear + "03" + padString("" + (intDays-59),2,"0",true);
}
else if(intDays < 121)
{
//April
strToReturn = strYear + "04" + padString("" + (intDays-90),2,"0",true);
}
else if(intDays < 152)
{
//May
strToReturn = strYear + "05" + padString("" + (intDays-120),2,"0",true);
}
else if(intDays < 182)
{
//June
strToReturn = strYear + "06" + padString("" + (intDays-151),2,"0",true);
}
else if(intDays < 213)
{
//July
strToReturn = strYear + "07" + padString("" + (intDays-181),2,"0",true);
}
else if(intDays < 244)
{
//August
strToReturn = strYear + "08" + padString("" + (intDays-212),2,"0",true);
}
else if(intDays < 274)
{
//September
strToReturn = strYear + "09" + padString("" + (intDays-243),2,"0",true);
}
else if(intDays < 305)
{
//October
strToReturn = strYear + "10" + padString("" + (intDays-273),2,"0",true);
}
else if(intDays < 335)
{
//November
strToReturn = strYear + "11" + padString("" + (intDays-304),2,"0",true);
}
else
{
//December
strToReturn = strYear + "12" + padString("" + (intDays-334),2,"0",true);
}
}
System.out.println(strDateIn + " = " + strToReturn);
return strToReturn;
}//End convertDate
/******************************************************************************
NAME: padString
PURPOSE: Used to pad a string, either left or right, with whatever padding
character is passed in
ACCEPTS: String strDataIn - The string to pad
int intMaxLength - The length to pad to
String strPadWith - The string to pad with
boolean blnLeftPad - True to left pad, false to right pad
RETURNS: A String that has been padded appropriately.
REVISIONS:
Ver Date Author Description
---------- ----------- ------------- ---------------------------------------
1.0 20030917 merick01 Initial Creation of Method
******************************************************************************/
public static String padString(String strDataIn, int intMaxLength, String strPadWith, boolean blnLeftPad)
{
String strToReturn = "";
if (blnLeftPad == true)
{
while ((strToReturn.length() + strDataIn.length())<intMaxLength)
{
strToReturn = strToReturn + strPadWith;
}
strToReturn = strToReturn + strDataIn;
}
else
{
strToReturn = strToReturn + strDataIn;
while (strToReturn.length()<intMaxLength)
{
strToReturn = strToReturn + strPadWith;
}
}
return strToReturn;
}//End padString
-
Nov 30th, 2004, 04:41 PM
#4
Have you read the edit on my post? The stuff about GregorianCalendar? It's a far superior method.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 30th, 2004, 04:52 PM
#5
Thread Starter
Hyperactive Member
I have got to learn to refresh before posting.
I can't believe I spent all that time on so much code when this little bit did it.
Thank you SO much!
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
|