Home arrow static arrow Java Programming [Archive] - Current Week dates
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Current Week dates
This topic has 29 replies on 2 pages.    « Previous | 1 | 2 |

Posts:5,904
Registered: 04/03/99
Re: Current Week dates  
Jul 29, 2004 4:55 AM (reply 15 of 29)



 

The purpose of that code is to calculate how many days to deduct from the current date to get the first day of the current week. It's complicated by the fact that Monday is not necessarily the first day of the week for a given locale.

If you're somewhere where the week starts on a Sunday does the "fiscal week" for your purposes still start on a Monday ?

If it does, just use the lookup technique illustrated earlier. To get a calendar representing a specified week, just set the DAY_OF_YEAR field to 1, then roll forward (n - 1) weeks (-1 because you're already IN week 1).

Dave.
 

Posts:57
Registered: 6/15/04
Re: Current Week dates  
Jul 29, 2004 5:18 AM (reply 16 of 29)



 
Hi Dave,

it's all messed up

I am getting thoroughly confused as to how to extract dates for a given week of current year

I would like the week to start from Monday having value1 and end with Sunday having value 7

Could u help ?

Thanx

Pooja
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 5:23 AM (reply 17 of 29)



 
Did you not try my solution in your other thread?? http://forum.java.sun.com/thread.jsp?thread=540574&forum=31

DateFormat df = DateFormat.getDateInstance();Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - calendar.get Calendar.DAY_OF_WEEK));for (int count = 0; count < 7; count++) {     System.out.println(df.format(calendar.getTime()));    calendar.add(Calendar.DATE, 1);}
 

Posts:57
Registered: 6/15/04
Re: Current Week dates  
Jul 29, 2004 5:39 AM (reply 18 of 29)



 
Hi yawmark,

I tried ur solution,

I am getting the output
Jul 25, 2004
Jul 26, 2004
Jul 27, 2004
Jul 28, 2004
Jul 29, 2004
Jul 30, 2004
Jul 31, 2004 and not

Jul 26, 2004
Jul 27, 2004
Jul 28, 2004
Jul 29, 2004
Jul 30, 2004
Jul 31, 2004
Aug 1, 2004

Pooja
 

Posts:5,904
Registered: 04/03/99
Re: Current Week dates  
Jul 29, 2004 5:40 AM (reply 19 of 29)



 

I would like the week to start from Monday having
value1 and end with Sunday having value 7

According to you, however, Calendar returns 1 for getFirstDayOfWeek();

That's Sunday. So in your locale, Sunday is the first day of the week. If you don't want that behaviour, you can either change the locale settings on your machine (if it's set wrongly) or use the lookup approach.

Could u help ?

If you can't figure out how to use the lookup approach, I'll help you after you've made an attempt and posted the results, but not before that. Others may be more generous.

Dave.
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 5:47 AM (reply 20 of 29)



 
import java.text.*;import java.util.*;    class CurrentWeekDemo {    public static void main(String[] args) {        new CurrentWeekDemo().go();    }        public void go() {                Calendar calendar = Calendar.getInstance(); // for my locale, first day is Sunday        printCurrentWeek(calendar);        System.out.println();        calendar = new GregorianCalendar(2004, Calendar.JULY, 25);        calendar.setFirstDayOfWeek(Calendar.MONDAY); // set first day as Monday        printCurrentWeek(calendar);    }                    public void printCurrentWeek(Calendar calendar) {        DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);        System.out.println("Original Date: " + df.format(calendar.getTime()));        int firstDayOfWeek = calendar.getFirstDayOfWeek();        int currentDay = calendar.get(Calendar.DAY_OF_WEEK);                int offset = firstDayOfWeek - currentDay;        if (currentDay < firstDayOfWeek) offset -= 7;        calendar.add(Calendar.DATE, offset);        for (int count = 0; count < 7; count++) {                System.out.println(df.format(calendar.getTime()));                calendar.add(Calendar.DATE, 1);        }    }    }

RESULTS:
Original Date: Thursday, July 29, 2004
Sunday, July 25, 2004
Monday, July 26, 2004
Tuesday, July 27, 2004
Wednesday, July 28, 2004
Thursday, July 29, 2004
Friday, July 30, 2004
Saturday, July 31, 2004

Original Date: Sunday, July 25, 2004
Monday, July 19, 2004
Tuesday, July 20, 2004
Wednesday, July 21, 2004
Thursday, July 22, 2004
Friday, July 23, 2004
Saturday, July 24, 2004
Sunday, July 25, 2004
 

Posts:5,904
Registered: 04/03/99
Re: Current Week dates  
Jul 29, 2004 5:49 AM (reply 21 of 29)



 

set first day as Monday

****, I didn't think of that (!)

Dave.
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 5:51 AM (reply 22 of 29)



 
Hi yawmark,

I tried ur solution,

I'm a little freaked out by someone who will type out "solution", but not "your"... :o)

The offset needed adjustment to ensure the current day fell within the week. Hopefully you'll be able to see what's going on with the code.

Everyone feel free to critique my example (as if I could stop you)... :o)
 

Posts:5,904
Registered: 04/03/99
Re: Current Week dates  
Jul 29, 2004 6:27 AM (reply 23 of 29)



 

I'm a little freaked out by someone who will type out
"solution", but not "your"... :o)

Still, it's better than mixing up "you're" and "your"

Everyone feel free to critique my example (as if I
could stop you)... :o)

;-)

I think your example makes assumptions about the values returned by get(Calendar.DAY_OF_WEEK); In theory (unless I've missed something) though probably not in practice, a third party implementation could choose completely random values for these constants !

I think mine doesn't. Because I was in that sort of mood when I wrote it...

Dave.
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 6:41 AM (reply 24 of 29)



 
Still, it's better than mixing up "you're" and "your"

The other day, I saw "u'r" (apostrophe and all) in place of "your". Yikes.

I think your example makes assumptions about the
values returned by get(Calendar.DAY_OF_WEEK); In
theory (unless I've missed something) though probably
not in practice, a third party implementation could
choose completely random values for these constants !

You're right about the assumption, but it doesn't strike me as an unreasonable one, given the constant values provided by the Calendar class. As a developer, I would expect an extension of Calendar to use the appropriate constants and not some arbitrary value.

From the API:
public static final int DAY_OF_WEEK Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY. 

BUT, you raise a valid point - what if Sun decides to change the underlying constants? Then it's all borked. Back to the drawing board.... :o)
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 6:44 AM (reply 25 of 29)



 
How's this (eliminates calculations based on constant values)?
public void printCurrentWeek(Calendar calendar) {    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);    System.out.println("Original Date: " + df.format(calendar.getTime()));    while(calendar.get(Calendar.DAY_OF_WEEK) != calendar.getFirstDayOfWeek()) {        calendar.add(Calendar.DATE, -1);    }    for (int count = 0; count < 7; count++) {            System.out.println(df.format(calendar.getTime()));            calendar.add(Calendar.DATE, 1);    }}    
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 6:45 AM (reply 26 of 29)



 
I actually like that better than the first version, anyway... :o)
 

Posts:5,904
Registered: 04/03/99
Re: Current Week dates  
Jul 29, 2004 6:54 AM (reply 27 of 29)



 
The other day, I saw "u'r" (apostrophe and all) in
place of "your". Yikes.

(shudder)

You're right about the assumption, but it doesn't
strike me as an unreasonable one, given the constant
values provided by the Calendar class. As a developer,
I would expect an extension of Calendar to use the
appropriate constants and not some arbitrary value.



From the API:
public static final intDAY_OF_WEEK Field number for get and set indicating the day of theweek. This field takes valuesSUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,and SATURDAY. 

BUT, you raise a valid point - what if Sun decides to
change the underlying constants? Then it's all borked.

Well, I was thinking of some rather hypothetical third party Java implementation really. While assuming they were insanely assigning the arbitrary order Thurs, Mon, Sat, Tues, Weds, Sun, Fri. I was coding the original example while knocking together a calendar-display gizmo, and after being slightly taken aback by the lunar month of Undecimber*, I was in that sort of "right, mess with me will you ?" frame of mind.

Back to the drawing board.... :o)

Your subsequent version looks clean to me (I'd cobbled something similar together after seeing your first cut, but yours is better implemented).

Dave.

*Undecimber. The month you wish it was when you wake up with a spectacular hangover on 1st January.

 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 6:59 AM (reply 28 of 29)



 
*Undecimber. The month you wish it was when you wake
up with a spectacular hangover on 1st January.

LOL!

Thanks for the comments about my code; it always helps to have another set of eyes. As a relative newcomer to the field, I need all the guidance I can get. :o)
 

Posts:24,036
Registered: 2/3/03
Re: Current Week dates  
Jul 29, 2004 7:01 AM (reply 29 of 29)



 
P.S. @Pooja:

When u post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes ur code much easier to read and prevents accidental markup from array indices like [i].
 
This topic has 29 replies on 2 pages.    « Previous | 1 | 2 |