*Month*,

*Day*, and

*Year*. But, these variables are integers instead of dates.

Now, I need to assign those variables to something recognizable by a Calendar. My buddy (Google) has suggested that I use the Calendar class for this.

I'm getting lost on how to go about doing this. The error message I get is: Calendar.x cannot be resolved to type.

Here is where the values are defined:

And, here is where I'm trying to assign those integer values to a calendar value:

Can someone provide me with an explanation as to where I'm going wrong with my logic?

Thanks in advance,

Ryan

and here is a short hint :

there are more convenient set methods which makes your life easier. It also might be confusing that setting month starts with 0 (= january) but setting day of month starts with 1.

And, through the help of Viktor, I was able to get the values selected from the combo box, and create a calendar month, day, and year out of them.

And, of course I have the current date:

Where I'm getting lost at this point is how to perform calculations against this data. How would I go about subtracting the date entered in the dropdown boxes from the current date?

Ryan Wamsat wrote:Where I'm getting lost at this point is how to perform calculations against this data. How would I go about subtracting the date entered in the dropdown boxes from the current date?

Unfortunately, it's a bit fiddly with Java's Date/Calendar API, but the basic idea is this:

1. A day contains 86400 (24*60*60) seconds, or 86400000 milliseconds.

2. Both Date and Calendar can be converted to a millisecond time

*of the same origin*.

3. Subtract the greater time from the lesser and you have the difference

*in milliseconds*.

The "fiddly" bit comes in converting those time offsets to dates (by which I mean a yymmdd-type date; NOT a Java Date):

1. A Java Date does NOT have a timezone.

2. A Java Calendar

*does*.

If the two timezones are the same (usually the "default" one for your machine), then you can simply truncate the two time values

*before you do the subtraction*; however, if they are different; then

*you*have decide what a "date" actually means for your purposes:

For example:

2 AM GMT on 1/1/2012 == 9PM EST 31/12/2011

Are these two "dates" different? Only you can say: after all, they represent the same

*instant*in time.

Welcome to Java Dates (in fact, date/times in general). If you want a nicer API for them, you might want to take a look at Joda Time; but it doesn't alter the fact that

__you__need to define what a "date" means for your purposes.

Winston

PS: If you want

*proper*validation of the components, I think you'd be better off having your dropdown lists in year/month/day order.

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Winston Gutkowski wrote:Welcome to Java Dates (in fact, date/times in general). If you want a nicer API for them, you might want to take a look at Joda Time; but it doesn't alter the fact thatyouneed to define what a "date" means for your purposes.

Winston

PS: If you wantpropervalidation of the components, I think you'd be better off having your dropdown lists in year/month/day order.

Winston,

Thank you for the response. I hadn't actually considered that information, and I'll have to figure out how to handle it.

However, the info that I am trying to figure out is how I construct a mathematical equation for the calendar variables I've already defined. Meaning, I have the Calendar variables, and I've been able to set .MONTH, .DAY_OF_MONTH, and .YEAR.

But, how do I go about using that information??? An example would really be appreciated.

And, I don't understand the

*proper*validation comment. What makes that proper over the mmddyyyy format?

Thank you,

Ryan

Ryan Wamsat wrote:And, I don't understand thepropervalidation comment. What makes that proper over the mmddyyyy format?

Nothing; maybe my bad wording.

What I mean is that if your drop-downs are in MDY order, you can't use the result of the

*previous*one to validate the next.

Part of good program design is anticipating issues and preventing them

*before they happen*and, for me, one of the signs of a well-designed website is that it populates its date dropdowns in a way that PREVENTS me from making a mistake.

So, if I choose 'June' from a Month dropdown, then I will not even see '31' in the Day dropdown. Obviously, that leaves Feb 29th; and to me the only time you should see 29 in a day dropdown for February is when the Year is a leapyear. So Year should be the

*first*one I get to choose.

Make sense?

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Winston Gutkowski wrote: Make sense?

Yes, that makes a lot of sense. But, that kind of dynamic logic is well beyond my current skill level. I hope to get there soon, though.

Can you assist me with using my variables to perform the math? I've tried:

My thinking that this would take the current date and subtract out the variables set in the cal variable. But, this didn't work.

Another thought that I had was take the three variables (.MONTH, .DAY_OF_MONTH, and YEAR) and try to stuff them into a new variable in order. And, then perform the math. But, I have a feeling that I'm going at it wrong that way.

Any insight would be helpful.

Thanks,

Ryan

Ryan Wamsat wrote:But, how do I go about using that information??? An example would really be appreciated.

Not quite sure what you want. Are you asking: how do I get that information back?

Have a look at

`Calendar.get()`.

Specifically, to get, eg, the Month, from a Calndar field, you use:

`myCalendar.get(Calendar.MONTH)`

However, the info that I am trying to figure out is how I construct a mathematical equation for the calendar variables I've already defined.

Again, you'll have to clarify what you mean by a 'mathematical equation'. Java has no equivalent of Joda Time's (see above)

`Period`or

`Interval`classes, so you basically have to code it yourself.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Ryan Wamsat wrote:Can you assist me with using my variables to perform the math? I've tried:

Ah, well that

*definitely*won't work, because for an expression like that to work,

__both__variables on the right-hand side have to be

`long`too (or

`int`, but

`long`is what you want here).

Have a look through the APIs for java.util.Date and java.util.Calendar (or java.util.GregorianCalendar) and see if you can find a method that looks right and returns a

`long`.

Winston

Articles by Winston can be found here

Ryan Wamsat wrote:Yes, that makes a lot of sense. But, that kind of dynamic logic is well beyond my current skill level. I hope to get there soon, though.

Me too -and good luck with that - but it doesn't stop you from putting the fields in the correct order for when you

__do__have that skill.

Winston

Articles by Winston can be found here

Winston Gutkowski wrote:Not quite sure what you want. Are you asking: how do I get that information back?

I'm trying to figure out the number of days between the two dates. For example, if the date entered = January 1, 2012. Then today (8/10/2012) minus 1/1/2012 = 223 days.

Thanks,

Ryan

Ryan Wamsat wrote:I'm trying to figure out the number of days between the two dates. For example, if the date entered = January 1, 2012. Then today (8/10/2012) minus 1/1/2012 = 223 days.

OK, which goes back to what I was saying earlier. Are the two dates GUARANTEED to have the same timezone? If so, the calculation is exactly as I said above.

The other question is (and this applies even if timezones are the same). Is time part of the equation? Because 23:59:59 December 31, 2011 and 00:00:01 January 1, 2012 are only two

__seconds__apart; yet they are different dates. Does that make them 1 day apart for your calculation or not?

This is what I was trying to say:

*YOU have to decide*.

Winston

Articles by Winston can be found here

Unfortunately, it's a bit fiddly with Java's Date/Calendar API, but the basic idea is this:

1. A day contains 86400 (24*60*60) seconds, or 86400000 milliseconds.

2. Both Date and Calendar can be converted to a millisecond time of the same origin.

3. Subtract the greater time from the lesser and you have the difference in milliseconds.

What I'm asking is whether you can show these steps to me in code format, using the the Calendar I've defined.

For the purposes of my understanding how this works, let's assume that it's the same timezone. I'll expand upon the code later to take timezones into account.

And, at this point, I will assume that each day constitutes 1 full day. Therefore, 23:59:59 December 31, 2011 and 00:00:01 January 1, 2012 would be 1 day apart. I'm hoping that by doing this, it makes it easier to understand. I'll handle times in another calculation, after I get past this point.

Thanks again,

Ryan

Paul Clapham wrote:It seems to me you're making a lot of work for yourself. Apparently you need the user to choose a date? Then why not just use a date chooser component? It's true that Java doesn't come with one, but you can find them on the web easily enough.

I'm beginning to see that I've chosen a difficult task for myself.

But, the goal here isn't to make things easy on me. The real goal is to try to figure out how Java works...

I'm an EXTREME beginner at programming... meaning, I've spent maybe 5 hours poking around on Google, and 3 hours trying to put together this code.

So, in doing this, I'm trying to see how Java would use a Calendar function (or possibly any function that assigns separate variables [month, day, year, in this case] in a similar way) to perform calculations.

Ryan Wamsat wrote:I'm beginning to see that I've chosen a difficult task for myself.

But, the goal here isn't to make things easy on me. The real goal is to try to figure out how Java works...

I'm an EXTREME beginner at programming... meaning, I've spent maybe 5 hours poking around on Google, and 3 hours trying to put together this code.

Well I'd say that even getting

*this far*in 5 hours is DAMN impressive.

However, in picking on dates and Calendars you've chosen:

(a) A very involved subject (in most languages; not just Java).

(b) A particularly bad (IMHO; and I don't think I'm alone) part of the Java foundation API.

Believe me, I'm a computer time geek, and there are

*still*things that I have to think about after 20-odd years of being fascinated with the whole business of "computer time".

I say again: if this

*really*interests you, look at Joda Time. It's streets ahead of what Java on its own has to offer, and you can translate freely between Java

`Date`s and many of the Joda classes.

So, in doing this, I'm trying to see how Java would use a Calendar function (or possibly any function that assigns separate variables [month, day, year, in this case] in a similar way) to perform calculations.

Now, you see, that's probably not how most programmers would go at this (although it is possible). We'd generally try to

*initialize*two different date/times OF THE SAME TYPE AND TIMEZONE with a month, day, and year and then do the subtraction you're thinking of using

`getTime()`.

The problem you have is that both Date (badly named) and Calendar

__include__time; so when you call

`Calendar.getInstance()`you get a

`Calendar`that

*includes*the current time. Most correct is to set all the time fields (and there are FOUR of them) to 0 if you don't want them to be a factor. It's clunky, but unfortunately, it's what ya got when it comes to Calendar. Again, Joda Time deals with all this stuff.

Winston

Articles by Winston can be found here

- 1

Winston Gutkowski wrote:

Well I'd say that even gettingthis farin 5 hours is DAMN impressive.

Seconded. If your interest is in Java for GUI programming, I suggest leaving dates/calendars for now and using something simpler for your drop-down, like an Integer or String.