The moose likes Java in General and the fly likes How to convert Number of Days to MM and DD Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to convert Number of Days to MM and DD" Watch "How to convert Number of Days to MM and DD" New topic
Author

How to convert Number of Days to MM and DD

caartic sub
Greenhorn

Joined: Apr 20, 2011
Posts: 2
Hi,

I am looking for a conversion logic that could convert the number of days to months & days.

e.g: If CCYY = 2011 and JJJ = 100, the converted CCYYMMDD should look like: 20110409

Thanks in advance.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2816
    
    2
Both GregorianCalendar can do calculations with day of year (which seems to be what your JJJ number represents) as well as other more common fields like month and day of month. Either could be used to solve this. If the inputs and outputs are text, I'd probably use SimpleDateFormat; otherwise GregorianCalendar.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Welcome to the Ranch!

Check out java.util.Calendar:
But you'll find that returns 20110410, not 20110409. That's because April 10th is the 100th day of the year: 31 for January, 28 for February and 31 for March makes 90. That leaves 10 for April.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
caartic sub
Greenhorn

Joined: Apr 20, 2011
Posts: 2
@Mike: Thanks.
@Rob: Thanks a lot. Worked like a charm.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to convert Number of Days to MM and DD
 
Similar Threads
Days between java.sql.Date and java.util.Date objects
Adding days to January 1, 1900
conversion
method to convert positive number to negative
How to convert from binary to int?