aspose file tools*
The moose likes Java in General and the fly likes How to derive YYYYMMDD from DAY_IN_YEAR? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to derive YYYYMMDD from DAY_IN_YEAR?" Watch "How to derive YYYYMMDD from DAY_IN_YEAR?" New topic
Author

How to derive YYYYMMDD from DAY_IN_YEAR?

Reddy Gurram
Greenhorn

Joined: Oct 19, 2004
Posts: 10
For a DAY_IN_YEAR calculate YYYYMMDD
If I have a DAY_IN_YEAR (say DIY == 199)
(a) Get DAY_IN_YEAR now say DIYnow
(b) Check if DIYnow > DIY then
YYYY = THIS_YEAR_YYYY;
else
YYYY = THIS_YEAR_YYYY - 1;
(c) Then for YYYY calculate MMDD for the given DIY (i.e for 199)

Do any of you know how to implement this in Java?

Cheers,
Gurram
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
create a calendar instance
add to the DATE field( -DAY_OF_YEAR + 199)
use SimpleDateFormat() to display calendar.getTime() the way you want
Reddy Gurram
Greenhorn

Joined: Oct 19, 2004
Posts: 10
Thanks Michael!

For some reason the following code is not printing the right DAY_IN_YEAR. Any ideas???
////////////////////BGN/////////////////////
public static void main(String[] args) {
Calendar cal = java.util.GregorianCalendar.getInstance();
Calendar gCalYDay = GregorianCalendar.getInstance();
System.out.println("gCalYDay.DAY_OF_YEAR:" + gCalYDay.DAY_OF_YEAR);
}
////////////////////END/////////////////////
Output looks as below:
-------------BGN-----------------
gCalYDay.DAY_OF_YEAR:6
-------------END-----------------

Cheers,
Gurram
Reddy Gurram
Greenhorn

Joined: Oct 19, 2004
Posts: 10
Oops! Michael,
What happens if (DIY == 365) and (DIYnow == 1) ???
Leap year or not?
Reddy Gurram
Greenhorn

Joined: Oct 19, 2004
Posts: 10
I Guess I need to use the below logic...
//////////////// bgn ////////////////
DIY01_this_year_or_last_year = SOME_KNOWN_VALUE;
find DIYnow;
find DateNow;


if ( DIYnow > DIY01_this_year_or_last_year ){
//
// Both DIYs in the same year
//
Date01 = DateNow - DIYnow + DIY01_this_year_or_last_year;
} else {
//
// DIY01 is in last year
//
last_year = Get this_year-1;
if (last_year LEAP_YEAR)
{x = 366 - DIY01;}
else
{x = 365 - DIY01;}
Date01 = DateNow - x - DIYnow;
}
//////////////// end ////////////////

For some reason still "gCalYDay.DAY_OF_MONTH" is not getting the actual DAY_IN_YEAR :-(.

...Gurram
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you need
gCalYDay.get(Calendar.DAY_OF_YEAR)

not
gCalYDay.DAY_OF_YEAR
Reddy Gurram
Greenhorn

Joined: Oct 19, 2004
Posts: 10
Thanks Michael!!!
That worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to derive YYYYMMDD from DAY_IN_YEAR?