Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Getting date based on days past year

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Getting date based on days past year

    Ok, I would assume someone out there has this already done, or atleast something similar that I can change.

    I'm given a number representing the number of days past January 1, 1900. I need to calculate the date the number represents. This is not for a school assignment or anything, I actually need this for a program I'm writing. It wouldnt be that bad, except I need to take into account of the differing number of days in a month and the leap years.

    My goal for this is to be able to get the Month and the Year. I dont care about the day of the month.

    If anyone has this already done or want to take a crack at it, I would be appreciative.
    Last edited by aussiemcgr; July 14th, 2010 at 02:13 PM. Reason: More Information


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting date based on days past year

    Take a look at Calendar in the java API. It will allow you to set a date, and from there you can add on specified times and recalculate the date.A simple example:
    public static void main(String[] args){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd");
    		Calendar cal = sdf.getCalendar();
    		cal.set(2010, Calendar.JULY, 14);
    		Date date = cal.getTime();
    		System.out.println(sdf.format(date));
    		for ( int i = 0; i < 30; i++ ){
    			cal.add(Calendar.HOUR, 24);
    		}
    		date = cal.getTime();
    		System.out.println(sdf.format(date));
    }

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Getting date based on days past year

    Ok, I was having a look at it.

    So tell me if I'm correct:
    Calendar cal = sdf.getCalendar();
    cal.set(2010, Calendar.JULY, 14);
    This sets the date selection to do calculations from.


    for ( int i = 0; i < 30; i++ )
    {
                cal.add(Calendar.HOUR, 24);
    }
    This moves the selection over 30 days.


    And really thats the only relevant stuff for me right?

    So instead of incrementing 30 days, if I wanted to increment 38353 days from Jan-1-1990, I would say:
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd");
    Calendar cal = sdf.getCalendar();
    cal.set(1990, Calendar.JANUARY, 1);
    for ( int i = 0; i < 38353; i++ )
    {
                cal.add(Calendar.HOUR, 24);
    }

    Am I correct?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting date based on days past year

    Pretty much...its not extremely elegant but it works. I've never verified this method for leap years and such, so you may want to verify this with a short test.

Similar Threads

  1. Copy folder based on latest date
    By ayonsoni in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 17th, 2010, 07:01 AM
  2. Day,Month,Year Concatenation Error
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 1
    Last Post: January 22nd, 2010, 04:40 AM
  3. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 11:13 AM
  4. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 03:53 AM
  5. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM