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:
Code :
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;
}
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
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.
Re: java question regarding lilian calender
Quote:
Originally Posted by
Mr.777
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.
Re: java question regarding lilian calender
Quote:
Originally Posted by
KevinWorkman
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.
Re: java question regarding lilian calender
Quote:
Originally Posted by
Mr.777
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.
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.
Re: java question regarding lilian calender
Quote:
Originally Posted by
Mr.777
@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
I actually told what was true.
No, you didn't. Norm's answer stands as the correct one.
Quote:
Originally Posted by
Mr.777
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.
Re: java question regarding lilian calender
Alright. Now Peace out KevinWorkman and Thanks for advise. I am sorry Norm.