• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to derive YYYYMMDD from DAY_IN_YEAR?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael!!!
That worked.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic