I am supposed to be makig a calendar program that will eventually allows the user to insert a number and the date will come up according to that number. But before we do that I need help from someone to give me small steps until we get it working correctly.
This calendar program is supposed to have a number of if then statements. Here is my calendar program so far. Right now my calendar has a constructor identifying a month, day, and year. I need help with the next method( ToString). I want this method to have something like if calendarMonth =1 then it equals January, or if calendarMonth = 2 then it is Febuary and so on.. I also want it to say if it doesn't equal a number 1-12 then it should print out invalid number in a string. -
-
import java.util.Calendar;
-
-
public class Date
-
{
-
int calendarDay;
-
int calendarMonth;
-
int calendarYear;
-
int currentDate;
-
-
public Date(int day, int month, int year)
-
{
-
if((calendarMonth >=1) && (calendarMonth <=12))
-
-
calendarMonth = month;
-
else
-
{
-
calendarMonth = 0;
-
calendarDay = 0;
-
}
-
-
if ((calendarDay >=1) && (calendarDay <=31))
-
-
calendarDay = day;
-
else
-
calendarDay = 0;
-
-
-
}
-
-
public String toString()
-
{
-
String calMonth;
-
{
-
{
-
if (calendarMonth != (>=1 && <=12))
-
calMonth = "invalid number";
-
}
-
-
{
-
if(calendarMonth == 1)
-
calMonth = "January";
-
return calMonth;
-
-
-
-
-
-
-
elseif(calendarMonth == 2)
-
calMonth = "Febuary";
-
return calMonth;
-
}
-
-
-
}
-
-
-
}
-
Do you see what I am trying to say in line 36, which is an error. I am trying to say if a number does not equal a number between 1 and 12 then calendarMonth equals a string saying invalid number 38 3046
1.) -
if(month < 1 || month > 12) {
-
calMonth = "Invalid Month";
-
}
-
2.) Multiple statements inside the if must be enclosed in braces -
if(month == 1) {
-
calMonth = "January";
-
return calMonth;
-
}
-
3.) If you are supposed to write your own calendar class then why are you importing the one from the java.util package?
4.) Enums do a better job for storing constants with properties. Have a read at what they are about.
5.) At the top of the Java forum threads listing is a thread titled "Read this First". It contains links that you need ... badly.
Ok thanks alot for the first method, I will try it out when I get the chance. As for the import calendar class, I thought I was supposed to type that in even though I am making my own calendar program. And I willd read the thread that states "Read this First". I am sorry for that
JosAH 11,448
Recognized Expert MVP
This is what boolean expressions are all about : if a condition X is true you want to do something; it X is not true you want to do something else: -
if (X) {
-
// do something
-
}
-
if (X) {
-
// don't do anything
-
}
-
else {
-
// do something else
-
}
-
That empty if statement in the second case is clumsy; the Java language offers a few operators that help us: -
if (!X) {
-
// do something else
-
}
-
That looks a bit better already. Your condition X is: -
month >= 1 && month <= 12
-
So your condition for an illegal month number looks like this: -
if (!(month >= 1 && month <= 12)) {
-
// do something else
-
}
-
I literally substituted your condition for X but I had to add a couple of parentheses to make it work. Here's the boolean logic part: every expression !(A&&B) can be rewriten as (!A) || (!B). De Morgan figured that out. Lets apply that rule: -
if (!(month >= 1) || !{month <= 12)) {
-
// do something else
-
That doesn't look much better; we can do better: if it is not so that a month is greater or equal to one, it must be less than one. The same applies for a month not being less or equal than twelve. Our last version looks like this: -
if (month < 1 || month > 12) {
-
// do something else
-
}
-
Which is exactly what was suggested in an above reply. btw, that Calendar interface stinks.
kind regards,
Jos
Ok thanks alot Jos. I tried out the statement and here is what it looks like - public String toString()
-
{
-
String calMonth;
-
{
-
if (calendarMonth < 1 || calendarMonth > 12)
-
{
-
calMonth = "Invalid Date"; // do something else
-
return calMonth;
-
}
-
-
-
if(calendarMonth == 1)
-
{
-
calMonth = "January";
-
return calMonth;
-
}
-
-
-
if(calendarMonth == 2)
-
{
-
calMonth = "Febuary";
-
return calMonth;
-
}
-
}
-
-
}
I changed Month into calendarMonth in line 5 so that the compiler would recongnize the variable. I also have an error stating that I am missing a return statement. Or is the error displayed because I have yet to insert all of the months.
Nepomuk 3,112
Recognized Expert Specialist
You have to insert the other months and the last one should be something like - else
-
{
-
calMonth = "December";
-
return calMonth;
-
}
otherwise it will not realise that all of the possible cases are dealt with.
Apart from that, the code block after the initial String calMonth; doesn't need braces (although they aren't wrong either). Oh, and I'd recommend using else if(calenderMonth == x) for all 2 <= x <= 11.
Greetings,
Nepomuk
Thanks for replying. And I tried programming the if statements without the braces, but the compiler states that I have an else statement without an if statement. I inserted the method, but now I have an error stating that I have a missing return statement. - public String toString()
-
{
-
String calMonth;
-
{
-
if (calendarMonth < 1 || calendarMonth > 12)
-
{
-
calMonth = "Invalid Date"; // do something else
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 1)
-
{
-
calMonth = "January";
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 2)
-
{
-
calMonth = "Febuary";
-
return calMonth;
-
}
-
-
-
-
else if(calendarMonth == 3)
-
{
-
calMonth = "March";
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 4)
-
{
-
calMonth = "April";
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 5)
-
{
-
calMonth = "May";
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 6)
-
{
-
calMonth = "June";
-
return calMonth;
-
}
-
-
-
else if(calendarMonth == 7)
-
{
-
calMonth = "July";
-
return calMonth;
-
}
-
-
else if(calendarMonth == 8)
-
{
-
calMonth = "August";
-
return calMonth;
-
}
-
-
else if(calendarMonth == 9)
-
{
-
calMonth = "September";
-
return calMonth;
-
}
-
-
else if(calendarMonth == 10)
-
{
-
calMonth = "October";
-
return calMonth;
-
}
-
-
else if(calendarMonth == 11)
-
{
-
calMonth = "November";
-
return calMonth;
-
}
-
-
else if(calendarMonth == 12)
-
{
-
calMonth = "December";
-
return calMonth;
-
}
-
-
-
-
}
-
-
}
ok nevermind that error. I see the problem. I just simply deleted all of the return calMonth; methods and just put one at the end. I also set String calMonth = null;
Ok sorry for the double post, but I could not find the edit button for some reason. The next method I will have to do is a leap year method. I am first gonna see if I can do it on my own before I ask for your help. I will let you know in time.
Once again I am sorry for the double post, but the edit post button was not up for some reason. Anyway I am trying to get the leap year to work for my calendar, but before I do that I felt that I had to identify what a year is first.
So here is my whole program. I thought If I give you my whole program again, it will make it easier for you guys to understand. I want to first make sure that I am on the right track. The part that I inserted was line 26 - line 30. The next step that I want to do is make the leap year method. Nepomuk 3,112
Recognized Expert Specialist
Well, those year correcting lines can't be quite correct - just try out a few years: - if(calendarYear <= 1999)
-
calendarYear = year + 1900;
-
else
-
calendarYear = year + 2000;
Say, calendarYear = 2000, then you'll end up in the else branch and that will produce the year 4000. If you enter 00 instead, you'll get 1900. So overthink those lines again, will you?
Apart from that, the algorithm for calculating if a year is a leap year can be found on Wikipedia, so try to translate it to Java code. It's really not difficult.
Greetings,
Nepomuk
Ok I think I see what you are saying. When I tested out the method I did recieve a number like 3890 when I entered 1990. So I changed the lines and is this what you mean. It looked correct to me. - if(calendarYear <= 1999)
-
-
calendarYear = year + 00;
-
else
-
calendarYear = year + 00;
ok nevermind. That has to be right because I got the leap year method to work perfectly(Well at least when I tested it). Here is the leap year method: - public String leapYear()
-
{
-
String calYear = null;
-
if (calendarYear % 4 == 0)
-
{
-
-
if (calendarYear % 100 != 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
else if (calendarYear % 400 == 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
}
-
else
-
{
-
calYear = calendarYear + " is not a leap year";
-
return calYear;
-
}
-
-
-
-
return calYear;
-
}
Ok the next thing I must do is create a DaysInMonth method, that returns the correct number of days in a month. This may be a little challenging. Ill try it out myself, but feel free to give me somewhere to start.
JosAH 11,448
Recognized Expert MVP
@OP: I've seen these huge posts of you passing by again and again; have a look at the following solution for the month names; no comment because it's a spoiler: -
private static final String[] months= {
-
"month < 1",
-
"January", "February", "March", "April", "May", "June",
-
"July", "August", "September", "October", "November", "December",
-
"month > 12"
-
};
-
...
-
public static String monthName(int month) {
-
return months[Math.min(13, Math.max(0, month))];
-
}
-
Do with it what you want but please note that there's no repeating code in there.
kind regards,
Jos
Thanks alot Jos. I will try out the code. I wasn't even close to what you had.
Nepomuk 3,112
Recognized Expert Specialist
OK, now this code here: - if(calendarYear <= 1999)
-
-
calendarYear = year + 00;
-
else
-
calendarYear = year + 00;
works, but it's completely useless. What ever the result, it always adds 0 to the year.
What I expect you were trying to do is make sure, that if e.g. 99 is entered, that is counted as 1999. What I'd suggest you do is something like this: - Choose some year, which should separate the dates. I'll explain more about this later, but basically it's what you did with 1999. (A common choice is 1970 by the way, for various reasons.)
- Check, if the year given is under 100
- If so, check if it would count as a year before or after the date you chose. So for example, would the number 60 count as before or after 1970 (so would it be 1960 or 2060). Then change the year it according to that result.
- If not, it will probably be bigger than, say, 1900 and you don't have to do anything.
@falconsx23
OK, let's have a quick look at that method. Say, we enter the year 2000 (which wasn't a leap year), it will check: - if (1900 % 4 == 0) // true
-
{
-
-
if (1900 % 100 != 0) // false
-
{
-
//...
-
}
-
else if (1900 % 400 == 0) // false
-
{
-
//...
-
}
-
}
-
//...
So, no output? I tested it and it didn't work for me.
What you want can be done with the algorithms I posted in my last answer, so have another look at those.
Greetings,
Nepomuk
JosAH 11,448
Recognized Expert MVP @Nepomuk
Ahem, it was a leap year because 2000 can be evenly divided by 400.
kind regards,
Jos
umm Nepomuk, are you taling about something like this when you talked about my calendar year mathod. - if(calendarYear < 100)
-
{
-
-
if (calendarYear > 40)
-
{
-
calendarYear = calendarYear + 1900;
-
}
-
else
-
{
-
calendarYear = year + 2000;
-
}
-
-
-
}
And for some reason I cannot seem to enter 08 as the year. It will say that the integer is too large. And it always adds the year to 2000. Im getting confused here. Little help or explanation please. You are right Nepomuk that is what i was trying to do with the year. For example when I entered 99 for the year it would just come out as 2099.
JosAH 11,448
Recognized Expert MVP @falconsx23
For the compiler an integer that starts with a zero introduces a number in octal representation: 00, 01, 02 ... 07. There is no digit 8 nor 9 in an octal number hence the compiler wining.
kind regards,
Jos
ps. Do you want to introduce new millenium bugs or what?
Ok JosAH thanks. Than made sense. And umm no I do not want you to introduce me into new millenium bugs yet. But now now I am trying to revise the constructor to use the daysInMonth method to determine whether the
date is valid. I have an idea on how to do this, but Im not to sure that it will work.
JosAH 11,448
Recognized Expert MVP @falconsx23
Take the array approach again: -
int daysInMnt[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-
Only when there are 28 days in the month you should check the year: if it's a leap year add one.
kind regards,
Jos
Thanks again. I am terrible when it comes to arrays. Thats why I try my best to stay away from them. I have a hard time understanding them. But thanks for the help
JosAH 11,448
Recognized Expert MVP @falconsx23
You can't stay away from them, you have to deal with them. An array is like a block of identical houses. The first house has number 0, the next has number 1, the next 2, the next 3 and so forth. Every 'house' can contain one single thing, just as one single variable can contain one single thing. So if I have this:
or I can have this: -
int[] thing= new int[42];
-
The first example is just one int and the second one is a block of 42 houses where each one can contain one int. Their house numbers are 0,1,2 ... 41 (check that) and an integer in house, say, 32, can be reached as thing[32].
There's not much more to arrays.
kind regards,
Jos
Ok thanks for the quick explanation. I actually understand what you mean. But am I supposed to use your method in a If statement. That seems the only way that I can determine if a date is valid or not. Also I know I said revise my constructor to determine whether the date is valid or not , but would it make sense to put the statment you gave me into the constructor.
I tried using the int daysInMnt array as an If statement in my constructor and it would not work
Ok here is my program. It took me a while and I am still not finished. This calendar prgrom now returns the number of days in a month when a month is entered including when Febuary is a leap year. I also revised my constructor using the daysInMonth method to determine if the number of days in a month are valid or invalid.
Now I have to write a function called changeDate that takes a day, month, and year as parameters. And if the date is valid it will change the date and return true. If invalid it will make no changes and return false. - import java.util.Calendar;
-
public class Date
-
{
-
int calendarDay;
-
int calendarMonth;
-
int calendarYear;
-
int currentDate;
-
-
public Date(int day, int month, int year)
-
{
-
-
if((calendarMonth >=1) && (calendarMonth <=12))
-
-
calendarMonth = month;
-
-
else
-
{
-
calendarMonth = 0;
-
calendarDay = 0;
-
}
-
-
-
if ((day > 0) && (day <= month))
-
-
calendarDay = day;
-
else
-
calendarDay = 0;
-
-
-
if(calendarYear <= 1999)
-
-
-
-
calendarYear = year + 00;
-
-
else
-
-
calendarYear = year + 00;
-
-
-
-
-
}
-
-
public String toString()
-
{
-
String calMonth = null;
-
-
if (calendarMonth < 1 || calendarMonth > 12)
-
{
-
calMonth = "Invalid Date"; // do something else
-
-
}
-
else if(calendarMonth == 1)
-
{
-
calMonth = "January";
-
-
}
-
-
-
else if(calendarMonth == 2)
-
{
-
calMonth = "Febuary";
-
-
}
-
-
-
-
else if(calendarMonth == 3)
-
{
-
calMonth = "March";
-
-
}
-
-
-
else if(calendarMonth == 4)
-
{
-
calMonth = "April";
-
-
}
-
-
-
else if(calendarMonth == 5)
-
{
-
calMonth = "May";
-
-
}
-
-
-
else if(calendarMonth == 6)
-
{
-
calMonth = "June";
-
-
}
-
-
-
else if(calendarMonth == 7)
-
{
-
calMonth = "July";
-
-
}
-
-
else if(calendarMonth == 8)
-
{
-
calMonth = "August";
-
-
}
-
-
else if(calendarMonth == 9)
-
{
-
calMonth = "September";
-
-
}
-
-
else if(calendarMonth == 10)
-
{
-
calMonth = "October";
-
-
} else if(calendarMonth == 11)
-
{
-
calMonth = "November";
-
-
}
-
-
else if(calendarMonth == 12)
-
{
-
calMonth = "December";
-
-
-
-
}
-
-
-
return calMonth;
-
-
-
}
-
-
public String leapYear()
-
{
-
String calYear = null;
-
if (calendarYear % 4 == 0)
-
{
-
-
if (calendarYear % 100 != 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
else if (calendarYear % 400 == 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
}
-
else
-
{
-
calYear = calendarYear + " is not a leap year";
-
return calYear;
-
}
-
-
-
-
return calYear;
-
}
-
-
-
public static final String[] months=
-
{
-
-
"month < 1",
-
"January", "February", "March", "April", "May", "June",
-
"July", "August", "September", "October", "November", "December",
-
"month > 12"
-
};
-
-
public static String monthName(int month)
-
{
-
return months[Math.min(13, Math.max(0, month))];
-
-
} public int daysInMonth(int month)
-
{
-
-
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12 )
-
{
-
calendarDay = 31;
-
}
-
-
else if(month == 4 || month == 6 || month == 9 || month == 11)
-
calendarDay = 30;
-
-
else if(calendarYear % 4 == 0 && month == 2 )
-
-
calendarDay = 29;
-
else if(month == 2)
-
calendarDay = 28;
-
-
-
-
-
-
else
-
{
-
calendarDay = 0;
-
-
}
-
-
return calendarDay;
-
}
-
-
public String changeDate(int day, int month, int year)
-
{
-
String g = null
-
if(calendarMonth = month && calendarDay = day && calendarYear = year + 00)
-
g = "True";
-
else
-
g = "False";
-
-
return g
-
-
}
-
-
-
-
-
}
Lines 211- 219 is what I tried to change
JosAH 11,448
Recognized Expert MVP
This entire thing is getting messier by the day; first design the API of your class, i.e. define which methods you have and what they're supposed to do; don't think about how they're supposed to do it.
Also define a small contract, e.g. you don't want to carry around an invalid Date object, e.g. new Date(42, 42, 42) is not supposed to succeed, the constructor can simply throw an IllegalArgumentException (one of the standard exception classes).
Get rid of your double implementations (e.g. your toString() method is the same as the monthName() method). Define separate methods for separate tasks, e.g. define a small boolean isLeapYear(int year) method and use it where needed.
Only keep your current code to laugh at and pick out the few useful parts but don't keep on hacking on this non-designed monster.
Also study arrays, you need them and study the boolean type because things such as 'return "True"' are preposterous.
A last tip: look for the file src.zip in your JDK installation directory. If contains the source code for all the standard classes that come with your distribution. Read (parts of) the code for the GregorianCalendar class and see for yourself.
kind regards,
Jos
Nepomuk 3,112
Recognized Expert Specialist @JosAH
You're right, I changed the year a few times and forgot to change that "wasn't" into a "was".
About the code, I'll have another look at it later, but I'm sure Jos' advice is good, so keep to that.
Greetings,
Nepomuk
Ok this is the last exercise and I have been struggling with this the past two days. It has to do the following.
**
* Method that returns the difference in days
* between this date and another date.
* If dates are equal, then the result is 0
* If current date (this) is before the
* parameter date (that) then answer is positive.
* @return
*/
I put my program in javadoc, but I have no idea on how to upload it onto this site or thread so that you guys can view it. - /**
-
* A calendar program that returns
-
* a date, by month, day and year
-
* @return
-
*/
-
-
import java.util.Calendar;
-
public class Date
-
{
-
int calendarDay;
-
int calendarMonth;
-
int calendarYear;
-
int currentDate;
-
-
/**
-
* A constructor with year, month, and day
-
* specified as parameters.
-
* @return
-
*/
-
public Date(int day, int month, int year)
-
{
-
/**
-
* Month is on range from 1 -12
-
*/
-
if((calendarMonth >=1) && (calendarMonth <=12))
-
calendarMonth = month;
-
else
-
{
-
calendarMonth = 0;
-
calendarDay = 0;
-
}
-
-
/**
-
* The days is the number that is
-
* greater than 0 and less than
-
* or equal to the month.
-
* month is the number of days in a month.
-
* @return
-
*/
-
if ((day > 0) && (day <= month))
-
calendarDay = day;
-
else
-
calendarDay = 0;
-
-
/**
-
* To determine the year.
-
* calendar year = year.
-
* when user puts in a year.
-
* Ex 1998
-
* Program returns 1998.
-
* @return
-
*/
-
if(calendarYear <= 1999)
-
calendarYear = year;
-
else
-
calendarYear = year;
-
}
-
-
/**
-
* This will return a string like Febuary
-
* for the current date.
-
* If the month or dat is wrong than
-
* it is set to invalid date.
-
* @return
-
*/
-
public String toString()
-
{
-
String calMonth = null;
-
if (calendarMonth < 1 || calendarMonth > 12)
-
{
-
calMonth = "Invalid Date"; // do something else
-
}
-
else if(calendarMonth == 1)
-
{
-
calMonth = "January";
-
}
-
else if(calendarMonth == 2)
-
{
-
calMonth = "Febuary";
-
}
-
else if(calendarMonth == 3)
-
{
-
calMonth = "March";
-
}
-
else if(calendarMonth == 4)
-
{
-
calMonth = "April";
-
}
-
else if(calendarMonth == 5)
-
{
-
calMonth = "May";
-
}
-
else if(calendarMonth == 6)
-
{
-
calMonth = "June";
-
}
-
else if(calendarMonth == 7)
-
{
-
calMonth = "July";
-
}
-
else if(calendarMonth == 8)
-
{
-
calMonth = "August";
-
}
-
else if(calendarMonth == 9)
-
{
-
calMonth = "September";
-
}
-
else if(calendarMonth == 10)
-
{
-
calMonth = "October";
-
}
-
else if(calendarMonth == 11)
-
{
-
calMonth = "November";
-
}
-
else if(calendarMonth == 12)
-
{
-
calMonth = "December";
-
}
-
return calMonth;
-
}
-
/**
-
* A leap year method.
-
* Every year that is divisble by 4
-
* is a leap year.
-
* A year is not a leap year if it is divisble by 100.
-
* If year is divisible by 400 than it is a leap year.
-
* Method returns "true" is year is a leap year.
-
* @return
-
*/
-
-
public String leapYear()
-
{
-
String calYear = null;
-
if (calendarYear % 4 == 0)
-
{
-
if (calendarYear % 100 != 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
else if (calendarYear % 400 == 0)
-
{
-
calYear = calendarYear + " is a leap Year";
-
return calYear;
-
}
-
}
-
else
-
{
-
calYear = calendarYear + " is not a leap year";
-
return calYear;
-
}
-
return calYear;
-
}
-
-
public static final String[] months=
-
{
-
"month < 1",
-
"January", "February", "March", "April", "May", "June",
-
"July", "August", "September", "October", "November", "December",
-
"month > 12"
-
};
-
-
public static String monthName(int month)
-
{
-
return months[Math.min(13, Math.max(0, month))];
-
}
-
/**
-
* method that returns number of days in a month.
-
* @return
-
*/
-
public int daysInMonth(int month)
-
{
-
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
-
calendarDay = 31;
-
else if(month == 4 || month == 6 || month == 9 || month == 11)
-
calendarDay = 30;
-
else if(calendarYear % 4 == 0 && month == 2 )
-
calendarDay = 29;
-
else if(month == 2)
-
calendarDay = 28;
-
else
-
{
-
calendarDay = 0;
-
}
-
return calendarDay;
-
}
-
-
/**
-
* boolean method that takes day, month and year as parameters.
-
* When user enters a valid date than the date is changed.
-
* If user inserts invalid date then date stays the same.
-
* @return
-
*/
-
public boolean changeDate(int day, int month, int year)
-
{
-
if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
-
return true;
-
else
-
return false;
-
}
-
-
public int differenceInDates(int day, int month)
-
{
-
if(this
-
(
-
return
-
}
-
}
JosAH 11,448
Recognized Expert MVP
You still have that loooong method toString() while you also have my method monthName(); how come?
w.r.t. your question: just suppose you have a method isValid(int year, int month, int day); then your changeDate() method can be as simple as: -
public boolean changeDate(int year, int month, int day) {
-
if (isValid(year, month, day)) {
-
this.year= year;
-
this.month= month;
-
this.day= day;
-
return true;
-
}
-
return false;
-
}
-
Your do have the logic for the isValid() method scattered all over the place and repeated here and there. Tidy up your class (design).
kind regards,
Jos
To be honest I have no idea why monthName is still there. I will change it asap. And my question previously was I needed a method that Method that can do the following. I have no idea on how to approach this. The following is the same way it was given to me from my assignment.
returns the difference in days between this date and another date.
If dates are equal, then the result is 0
If current date (this) is before the
parameter date (that) then answer is positive.
JosAH 11,448
Recognized Expert MVP @falconsx23
Google for "Zeller's congruence". It's just one formula that converts a Gregorian date to a Julian day number; the difference between two Julian day numbers is the difference in days between two dates.
kind regards,
Jos
Ok thanks for the help. I will try it out and let you know how it goes
Ok ummm. I do not want to use that "Zeller's congruence" only because I do not think my professor would want us to use it. So I need help finding another way on how to get the difference between two dates(mainly if statements). This is what I came up with. I know it is not right, in fact far from being right.
How can I use the keywords "this" and "that" to get the difference between two dates? Is is possible? - public int differnceInDate(int day, int month, int year)
-
{
-
-
if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
-
{
-
-
currentDate = daysInMonth(month);
-
differenceBetweenDates = currentDate - dateEntered
-
-
}
-
-
else
-
{
-
currentDate = 0;
-
}
-
-
return daysInMonth(month);
-
-
-
}
JosAH 11,448
Recognized Expert MVP @falconsx23
When I was young my professor really valued a bit of initiative. Zeller's congruence is easy: simply plug in the year, month and date and you've got a Julian date number. It is the best way to do these type of calculations.
kind regards,
Jos
sorry JosAH, but I chose to do it this way even though your way may seem better and easier. This way worked for me even though it took me a while. I also had to make some minor changes in my constructor.
If you need me to explain what is going on I will. - if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
-
{
-
-
currentDay = calendarDay - day;
-
-
-
-
currentMonth = calendarMonth - month;
-
currentDay = currentMonth * 30 + currentDay;
-
-
-
-
currentYear = calendarYear - year;
-
currentDay = currentYear * 365 + currentDay;
-
-
-
}
-
-
else
-
{
-
currentDay = 0;
-
}
-
-
return currentDay;
-
-
-
}
JosAH 11,448
Recognized Expert MVP
That doesn't work for leap years and/or months that don't have thirty days. But be my guest to turn that in. Good luck with it.
kind regards,
Jos
Yea you a right. I may turn it in since it is already pass the deadline. But I will continue to work on it more to day. But could I just simply insert an if statement saying that - if calDay = 31 then currentMonth * 31 + currentDay.
Thats not how I would really write it, I am just trying to give you an idea.
JosAH 11,448
Recognized Expert MVP @falconsx23
Yes and you have to do that for every month; don't forget the leap year check. When you're finished you've reinvented what Zeller had done ages ago.
kind regards,
Jos
Just wanted to thank you for helping me
JosAH 11,448
Recognized Expert MVP @falconsx23
You're welcome of course; did you finish your project?
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: clemke |
last post by:
Hello
My department has a SQL server database that is used to all facets of
production within our department. This includes key production job
steps, project names, numbers and descriptions.
...
|
by: Shuffs |
last post by:
Could someone, anyone please tell me what I need to amend, to get this
function to take Sunday as the first day of the week?
I amended the Weekday parts to vbSunday (in my code, not the code...
|
by: Larry R Harrison Jr |
last post by:
Sometime, if an error has occured or even if a user has answered NO to a
MsgBox Y/N question, the Lebans calendar control will prevent the form from
closing.
I know the Lebans calendar control...
|
by: Shyguy |
last post by:
Is it possible to create a calendar that shows previous input data and
also allows for input of new data?
|
by: tigrfire |
last post by:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR
This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and...
| |
by: dunk_king |
last post by:
Hi Anybody,
i´m trying to show a calendar (dayview) in my own vb.net programm.
This calendar sheet should get all items (of this day) of my local
outlook calendar (Outlook 2000). Is something...
|
by: GV |
last post by:
Hi all,
New to developing in VS 2005 ASP 2.0
Trying to have a easy pop calender for a button on a web page.
I keep getting a error message in IE6 that says:
Line 69
Char 3
Error:...
|
by: Tony Girgenti |
last post by:
Hello.
Developing a VS2005, SP4, VB, .NET 2.0, ASP.NET 2.0, web site program using
a calendar.
I tried using the validators to see if i can validate dates using a calendar
control. It does...
|
by: slapsh0t11 |
last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment:
Here is my class file...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |