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 9 of 9

Thread: java question regarding lilian calender

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java question regarding lilian calender

    I have a program that asks a user for a date, then I compute how many days from 10/15/1582 to now that is. I want to take that number, and create a method that turns that number into a nice looking date such as "mm/dd/yyyy."

    Knowing how many days from 10/15/1582, how can I create a string that displays the date since then nicely?

    This is the method that takes the date and counts the number of days since 10/15/1582:
    public int lilian()
      {
        // Returns the Lilian Day Number of this date.
        // Precondition: This Date is a valid date after 10/14/1582.
        //
        // Computes the number of days between 1/1/0 and this date as if no calendar
        // reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1. 
     
        final int subDays = 578100;  // number of calculated days from 1/1/0 to 10/14/1582
     
        int numDays = 0;
     
        // Add days in years.
        numDays = year * 365;
     
        // Add days in the months.
        if (month <= 2) 
          numDays = numDays + (month - 1) * 31;
        else 
          numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);
     
        // Add days in the days.
        numDays = numDays + day;
     
        // Take care of leap years.
        numDays = numDays + (year / 4) - (year / 100) + (year / 400);
     
        // Handle special case of leap year but not yet leap day.
        if (month < 3) 
        {
            if ((year % 4) == 0)   numDays = numDays - 1;
            if ((year % 100) == 0) numDays = numDays + 1;
            if ((year % 400) == 0) numDays = numDays - 1;
          }
     
        // Subtract extra days up to 10/14/1582.
        numDays = numDays - subDays;
        return numDays;
      }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java question regarding lilian calender

    Look at the Calendar class. It has lots of methods for working with dates.
    Also look at the DateFormat class

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: java question regarding lilian calender

    If you have number of days, you can just do like;
    1. Divide the days into number of years, months and days.
    2. Add number of years into years, months into months and days into days (Remember to split date in three different variables and while adding don't forget to handle exceptions like days can't go more than 31, months not more than 12 and etc)
    Hope this will help you.
    Try this and if you got any problem, come up with your code.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: java question regarding lilian calender

    Quote Originally Posted by Mr.777 View Post
    If you have number of days, you can just do like;
    1. Divide the days into number of years, months and days.
    2. Add number of years into years, months into months and days into days (Remember to split date in three different variables and while adding don't forget to handle exceptions like days can't go more than 31, months not more than 12 and etc)
    Hope this will help you.
    Try this and if you got any problem, come up with your code.
    This is certainly one way to do it, but Norm's suggestion handles all of that for you, as well as dealing with things like leap years, timezones, locales, etc.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: java question regarding lilian calender

    Quote Originally Posted by KevinWorkman View Post
    This is certainly one way to do it, but Norm's suggestion handles all of that for you, as well as dealing with things like leap years, timezones, locales, etc.
    Well, what Norm told was totally useless, as far as i know. The reason is he advised him to use the built in class and not encouraged him to write one.
    Well, i know that you will say, you must use built in modules in order to avoid effot. But you can not learn unless you do effort.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: java question regarding lilian calender

    Quote Originally Posted by Mr.777 View Post
    Well, what Norm told was totally useless, as far as i know. The reason is he advised him to use the built in class and not encouraged him to write one.
    Well, i know that you will say, you must use built in modules in order to avoid effot. But you can not learn unless you do effort.
    That's pretty rude, and not true at all. Norm's suggestion is the way to go. I tried to be polite because you seem like a nice guy, but you're wrong here. I'm all for writing code the long way the first time around (using arrays instead of ArrayLists, etc), but there's a difference between working your way up and simply approaching a problem incorrectly.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: java question regarding lilian calender

    @KevinWorkman: I didn't mean to be rude. I actually told what was true. Don't you think, if we have a built in function and we use it. It's really easy to use than to make one, like that, to have a clear understanding and learning too.
    Anyways, i am sorry if you think, i am rude. I didn't actually mean that.
    My bad.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: java question regarding lilian calender

    Quote Originally Posted by Mr.777 View Post
    @KevinWorkman: I didn't mean to be rude.
    Saying "what Norm said was useless" is pretty rude, even if you do disagree with him. I disagree with you, but I'm not calling your post useless.

    Quote Originally Posted by Mr.777 View Post
    I actually told what was true.
    No, you didn't. Norm's answer stands as the correct one.

    Quote Originally Posted by Mr.777 View Post
    Don't you think, if we have a built in function and we use it. It's really easy to use than to make one, like that, to have a clear understanding and learning too.
    No, I don't. The Calendar, Date, and DateFormat classes exist for a reason. If somebody was using an array in a way that would be more suitable for an ArrayList, the correct answer is not to tell them to continue using arrays incorrectly so that they can understand them. The correct answer is to tell them to use an ArrayList. There's a reason we're using Java instead of C.

    And if this is for an introductory homework assignment (and using classes such as Calendar, Date, and DateFormat is disallowed), then it's the OP's responsibility to include that in the description. Until then, Norm's answer is correct.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: java question regarding lilian calender

    Alright. Now Peace out KevinWorkman and Thanks for advise. I am sorry Norm.

Similar Threads

  1. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 05:24 PM
  2. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 06:21 PM
  3. Help with formatting spacing on a calender grid please!
    By bulx0001 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 27th, 2011, 12:53 AM
  4. Newbie Java Question
    By tinkersp in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 01:41 PM
  5. Java Question
    By NiCKYmcd in forum Java Theory & Questions
    Replies: 1
    Last Post: August 25th, 2010, 08:47 AM