Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Finding is a given date is a month end date??

Reply
Thread Tools

Finding is a given date is a month end date??

 
 
rsujatha@gmail.com
Guest
Posts: n/a
 
      05-06-2006
Can anybody tell me how to find if a particular date I fetch from the
DB is a month end or not??

 
Reply With Quote
 
 
 
 
Jeroen V.
Guest
Posts: n/a
 
      05-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Can anybody tell me how to find if a particular date I fetch from the
> DB is a month end or not??
>


You could add 1000*60*60*24 milliseconds to the date (see
java.util.GregorianCalendar) and check whether the month value has
changed after that.

Jeroen
 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      05-06-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Can anybody tell me how to find if a particular date I fetch from the
> DB is a month end or not??
>

Assuming you're using a western calendar, the exact length of the months is
very well known: April, June, September and November always have 30 days;
February has 28 days except when it is a leap year, in which case it has 29
days; all the remaining months always have 31 days. You can easily write a
method to determine if a given date goes beyond the end of a month; I've
done it in 4 or 5 languages over the years, including Java.

Remember, the rule for leap years is that is the year is a multiple of 100,
e.g. 1700, 1800, 1900, 2000, the year is only a leap year if the entire year
is evenly divisible by 400. Therefore, 1700, 1800, and 1900 are NOT leap
years but 2000 IS a leap year. If the year is NOT a multiple of 100, e.g.
2004, it IS a leap year if it is evenly divisible by 4.

--
Rhino




 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      05-06-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Can anybody tell me how to find if a particular date I fetch from the
> DB is a month end or not??


I would wrap a GregorianCalendar in my own utility class and have methods
that determine that by setting the calendar to the correct month:

private GregorianCalendar calendar = new GregorianCalendar();
// or instantiate with locale/timezone specifing version

public int getMaxDayInMonthofDate(Date myDate) {

synchronized (calendar) {
calendar.setTime(myDate); // instance of Date
int maxDayInMyDatesMonth = calendar.getActualMaximum();
}
return maxDayInMyDatesMonth;
}

--
Mike W


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-06-2006
On 6 May 2006 03:14:30 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>Can anybody tell me how to find if a particular date I fetch from the
>DB is a month end or not??


The direct way is to use two tables of month lengths selected by
isLeap( yyyy ). and compare dd with that entry.

With BigDate you could use d.addDays(1) then check if d.getMM() == 1.

see http://mindprod.com/jgloss/bigdate.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-09-2006
Jeroen V. wrote:
> (E-Mail Removed) wrote:
>
>> Can anybody tell me how to find if a particular date I fetch from the
>> DB is a month end or not??
>>

>
> You could add 1000*60*60*24 milliseconds to the date (see
> java.util.GregorianCalendar) and check whether the month value has
> changed after that.


Except in timezones which include Daylight savings where there is
one day which is 23 hours long.

-Paul
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-09-2006
Rhino wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>
>>Can anybody tell me how to find if a particular date I fetch from the
>>DB is a month end or not??
>>

>
> Assuming you're using a western calendar, the exact length of the months is
> very well known:


Sure, so well known it is already contained in the
java.util.GregorianCalendar class, so forget the rules, the software
already knows them

-Paul
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-09-2006
VisionSet wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>
>>Can anybody tell me how to find if a particular date I fetch from the
>>DB is a month end or not??

>
>
> I would wrap a GregorianCalendar in my own utility class and have methods
> that determine that by setting the calendar to the correct month:


I would go further and subclass GregorianCalendar
since having an easy bit of API to know that a date currently in
the Calendad (probably pushed in via setDate(Date) is the end of
the month might be just what I need for this application.

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MyCal extends GregorianCalendar {
public boolean isLastDay(Date myDate) {
return
getActualMaximum(Calendar.MONTH) == get(Calendar.DAY_OF_MONTH);
}
}

OO can be a useful, if not beautiful, thing.

-Paul
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      05-09-2006
P.Hill wrote:

> > You could add 1000*60*60*24 milliseconds to the date (see
> > java.util.GregorianCalendar) and check whether the month value has
> > changed after that.

>
> Except in timezones which include Daylight savings where there is
> one day which is 23 hours long.


Nitpick: The one that's 25 hours long would be more of a problem.

-- chris


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-09-2006
"P.Hill" <(E-Mail Removed)> writes:

> Jeroen V. wrote:
>> You could add 1000*60*60*24 milliseconds to the date (see
>> java.util.GregorianCalendar) and check whether the month value has
>> changed after that.

>
> Except in timezones which include Daylight savings where there is
> one day which is 23 hours long.


Luckily, it's not at the beginning or end of a month, so it won't
change the result in this case.

It's still much safer to add on to the date in a lenient calendar.
Seeing 1000*60*60*24 (or 864E5) is reason to worry in most cases.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Days in a given date range for a given month......... Lord0 Java 1 04-19-2006 04:54 PM
Finding date 1 day earlier than a given date! Edward Perl Misc 3 09-08-2004 04:51 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments