Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to derive YYYYMMDD from DAY_IN_YEAR?

 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Michael,
What happens if (DIY == 365) and (DIYnow == 1) ???
Leap year or not?
 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need
gCalYDay.get(Calendar.DAY_OF_YEAR)

not
gCalYDay.DAY_OF_YEAR
 
Reddy Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael!!!
That worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic