Hi all,
I have a date stored in a Date variable, which I need to compare
against todays date to see if it in the current month.
I have to use java.util.Date to do this (college assignment...)
Is there a way to get just the month from a Date var, or perhaps
compare two Date vars against the month?
Thanks in advance,
Conor
Ingo R. Homann - 20 Sep 2007 11:06 GMT
Hi,
> Hi all,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Conor
Take a look at the API:
int getMonth()
Deprecated. As of JDK version 1.1, replaced by
Calendar.get(Calendar.MONTH).
Concerning your subject and "college assignment": You may use
java\text\DateFormat.html, but that would not be my choice.
Hth,
Ingo
Lew - 20 Sep 2007 14:20 GMT
> Hi,
>
[quoted text clipped - 19 lines]
> Concerning your subject and "college assignment": You may use
> java\text\DateFormat.html, but that would not be my choice.
Make that java.text.DateFormat
Documented at
<http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html>
Your professor teaches you Date and makes you use deprecated methods instead
of Calendar?
<http://java.sun.com/javase/6/docs/api/java/util/Date.html>
> Prior to JDK 1.1, the class Date ... allowed the interpretation of dates
> as year, month, day, hour, minute, and second values. It also allowed the
[quoted text clipped - 3 lines]
> and the DateFormat class should be used to format and parse date strings.
> The corresponding methods in Date are deprecated.
You should educate your professor that Java has advanced since version 1.1.
Ten years is forever in the computer field.
Even version 1.4 is obsolescent.
Someone should take this professor away from teaching.

Signature
Lew
Roedy Green - 20 Sep 2007 18:59 GMT
>Your professor teaches you Date and makes you use deprecated methods instead
>of Calendar?
I was once asked to speak on a panel on computers in education circa
1992. I told a story of one of my apprentices being giving an
assignment which was clearly a minor modification from one given
designed to be solved with punched cards. The education system which
often uses textbooks decades old could not keep up with the evolution
of computers. To my great embarrassment, one of the other panelists
turned out to be my apprentice's professor who had devised the problem
and could not be convinced I did not tell this story just to humiliate
him.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 20 Sep 2007 19:47 GMT
>> Your professor teaches you Date and makes you use deprecated methods instead
>> of Calendar?
[quoted text clipped - 8 lines]
> and could not be convinced I did not tell this story just to humiliate
> him.
In the case of Java education, the school could use java.sun.com and other web
resources at no cost to themselves or effort to stay up to date.
Shoot, I just looked at Sun's JEE tutorial and it's completely different from
a week ago. I didn't have to do anything to keep my copy up to date.
I don't see how this professor who's teaching the OP to use methods that were
deprecated (in Java 1.1 for God's sake!) has even the remotest excuse, and if
they're reading this newsgroup and are embarrassed, I for one am rather glad I
said so.
There is no excuse pedagogical or otherwise for damaging your students' education.

Signature
Lew
kcwong - 20 Sep 2007 11:11 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Conor
Which Java version are we talking about?
As of Java 1.1, java.util.Date's getXXX() methods are deprecated.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#getDay()
The replacement is the Calendar class, which provides an add() method.
Technically you could do this:
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, -5); // Five days ago
But I guess that would be cheating and you will get zero marks. ;)
I think the exercise is to do date calculations... so get the value in
a long:
long l = d.getTime();
Use l to calculate, and use setTime(long) when you're done. The link
to Java API doc I posted will tell you what that long value means.
Roedy Green - 20 Sep 2007 18:54 GMT
On Thu, 20 Sep 2007 09:56:35 -0000, conmulligan
<conmulligan@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>Is there a way to get just the month from a Date var, or perhaps
>compare two Date vars against the month?
Have you checked if Date has a getMonth method?
If not, you will have to convert to a GregorianCalendar object, which
has means to extract the month.
Another means is to convert to String and extract the month.
see http://mindprod.com/jgloss/date.html
http://mindprod.com/jgloss/calendar.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Eric Sosman - 21 Sep 2007 02:09 GMT
> Hi all,
>
[quoted text clipped - 5 lines]
> Is there a way to get just the month from a Date var, or perhaps
> compare two Date vars against the month?
It's not clear what "in the current month" means. Today
is 20-Sep-2007, so 10-Sep-2007 almost certainly qualifies. But
what about 11-Sep-2001? It's clearly in September, as today is,
but not in the same September. What about 31-Sep-2007, or
52-Aug-2007? Do you need to worry about time zones, daylight
savings, leap years, and suchlike?
Whatever is meant, you'll probably find the tools you need
in the Calendar class. Build Calendars from the current date
and from your given Date, and have at it.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid