Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Days in a given date range for a given month.........

Thread view: 
Lord0 - 19 Apr 2006 17:02 GMT
Hi there,

I have an object with fields which represent dates and therefore a date
range.

private Date m_startDate;

private Date m_endDate;

Given another singular date (target) as an argument I want to return
all the days, in the range, where target.mon = dayrange.mon. Sorry for
the bad explanation. Maybe some examples:

If m_startDate is 15th Jan 06, m_endDate is 20th Mar 06 and target is
Jan 06

return: 15..31

If m_startDate is 15th Jan 06, m_endDate is 20th Mar 06 and target is
Feb 06

return 1..28[29]

If m_startDate is 15th Jan 06, m_endDate is 20th Mar 06 and target is
Mar 06

return 1..20

If m_startDate is 15th Jan 06, m_endDate is 20th Jan 06 and target is
Jan 06

return 15..20

This has fried my head and I am at a loss to think of a nice solution.

thanks in advance

Lord0
Thomas Weidenfeller - 19 Apr 2006 17:54 GMT
> Hi there,
>
[quoted text clipped - 4 lines]
>
> private Date m_endDate;

First of all, get rid of that annoying "m_" prefix. No one will steal
your variables while you look away, so no need to to mark them as "my
variables".

For the rest, here is a rough cut (untested). If I would have to code it
in real life I would first introduce a class like DateRange and provide
it with some intersection() method and a range Interator or similar.

 GregorianCalendar start = new GregorianCalender();
 start.setTime(m_startDate);
 // probably reset time fields to zero

 GregorianCalendar end = new GregorianCalendar();
 end.setTime(m_endDate);
 // probably reset time fields to zero

 GregorianCalendar targetStart =
    new GregorianCalendar(targetYear, targetMonth, 1);

 GregorianCalendar targetEnd = (GregorianCalendar) targetStart.clone();
 targetEnd.set(Calendar.DAY_OF_MONTH,
targetEnd.getActualMaximum(Calendar.DAY_OF_MONTH));

 GregorianCalendar resultStart = start;
 GregorianCalendar resultEnd   = end;

 if(targetStart.after(start) {
    resultStart = targetStart;
 }
 if(targetEnd.before(end)) {
    resultEnd   = targetEnd;
 }

 while(resultEnd.after(resultStart)) {
   System.out.println(resultStart);
   resultStart.add(Calendar.DAY_OF_MONTH, 1);
 }

> This has fried my head and I am at a loss to think of a nice solution.

First it has to work. Then you can make it nice. E.g. by introducing the
above mentioned DateRange (or a CalendarRange) class.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.