java date calculations

Discussion in 'Java' started by tdd, May 14, 2004.

  1. tdd

    tdd Guest

    I am trying to calculate the weekStartDate. When I set the date to
    05/15/2004 it returns 05/17/2004 as the beginning date. It should
    return 05/10/2004. What am I doing wrong?

    See following:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Calendar;

    public class DateUtil {
    private Date target;
    private Calendar c = Calendar.getInstance();;
    private static final SimpleDateFormat dateFormatter = new
    SimpleDateFormat("MM/dd/yyyy");

    public DateUtil ( String date ){
    try{
    target = dateFormatter.parse(date);
    }
    catch (Exception e){

    }
    }

    public String getWeekStartDate(){
    c.setTime(target);
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return dateFormatter.format(c.getTime());
    }

    public static void main(String args[]){
    DateUtil d = new DateUtil("5/15/2004");
    String s = d.getWeekStartDate();
    System.out.println("Start Date:" + s);
    }
    }
     
    tdd, May 14, 2004
    #1
    1. Advertising

  2. tdd

    Roedy Green Guest

    On 14 May 2004 09:47:06 -0700, (tdd) wrote or
    quoted :

    >I am trying to calculate the weekStartDate. When I set the date to
    >05/15/2004 it returns 05/17/2004 as the beginning date. It should
    >return 05/10/2004. What am I doing wrong?


    for an easier way, see http://mindprod.com/products.html#BIGDATE

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 14, 2004
    #2
    1. Advertising

  3. tdd schrieb:
    > I am trying to calculate the weekStartDate. When I set the date to
    > 05/15/2004 it returns 05/17/2004 as the beginning date. It should
    > return 05/10/2004. What am I doing wrong?


    Don't know. On my system it returns "Start Date:05/10/2004" :)

    Bye
    Michael
     
    Michael Rauscher, May 14, 2004
    #3
  4. tdd

    Chris Smith Guest

    tdd wrote:
    > I am trying to calculate the weekStartDate. When I set the date to
    > 05/15/2004 it returns 05/17/2004 as the beginning date. It should
    > return 05/10/2004. What am I doing wrong?


    You're assuming that setting DAY_OF_WEEK will cause the Calendar to move
    backward instead of forward. That's not clear in the documentation I'm
    reading. In any case, you can ensure this works by using looping doing
    a Calendar.add(Calendar.DATE, -1) and comparing DAY_OF_WEEK each time.

    Incidentally, Monday as the first day of the week? That's a new one to
    me. Must be a cultural/local thing. Calendar's documentation mentions
    that this is set according to the current Locale automatically, so I'd
    assume that unless you have a reason to do otherwise, you should leave
    it as is.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 14, 2004
    #4
  5. tdd

    Roedy Green Guest

    On 14 May 2004 09:47:06 -0700, (tdd) wrote or
    quoted :

    >I am trying to calculate the weekStartDate. When I set the date to
    >05/15/2004 it returns 05/17/2004 as the beginning date. It should
    >return 05/10/2004. What am I doing wrong?


    is your clock set correctly? See
    http://mindprod.com/jgloss/setclock.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 14, 2004
    #5
  6. tdd

    Roedy Green Guest

    On Fri, 14 May 2004 11:27:39 -0600, Chris Smith <>
    wrote or quoted :

    >Incidentally, Monday as the first day of the week? That's a new one to
    >me. Must be a cultural/local thing. Calendar's documentation mentions
    >that this is set according to the current Locale automatically, so I'd
    >assume that unless you have a reason to do otherwise, you should leave
    >it as is.

    in France, it is Monday = 2. In the US, it is Sunday=1 so much for
    0-based.

    The USA uses the term weekEND to refer to Saturday and Sunday, yet
    they say the week starts with Sunday, yet they say God rested on the
    7th day, and celebrate that on Sunday. Christians therefore should
    reject 0-based indexing on religious grounds. But then nothing about
    dates make any sense. Read the notes in
    http://mindprod.com/jglossl/bigdate.html and
    http://mindprod.com/jgloss/leap.html


    For my collection of date gotchas see
    http://mindprod.com/jgloss/gotchas.html#DATE




    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 14, 2004
    #6
  7. tdd

    Chris Smith Guest

    OT - Re: java date calculations

    Roedy Green wrote:
    > The USA uses the term weekEND to refer to Saturday and Sunday, yet
    > they say the week starts with Sunday, yet they say God rested on the
    > 7th day, and celebrate that on Sunday.


    Just in terms of interesting history, throughout the history of the
    Judeo-Christian world (and thus most of Western civilization) Sunday has
    always been considered the beginning of the week. The decision of the
    early Christian Church to celebrate the risen Christ on Sunday was a
    change from earlier Jewish practice, and Christians fully realized that
    it was not the seventh day. Jewish Christians definitely observed the
    Sabbath, in addition to celebrating eucharist on Sunday. Only after
    centuries of forgetting do modern Christians tend to think of Sunday as
    being the Sabbath because it's a convenient way to interpret more of
    Jewish scripture as applying to them.

    The term "weekend", on the other hand, I don't know about. I'd assume
    it comes from the 5-day work week, and the idea that you get that time
    off after the long week of work. As such, it would be a rather new
    innovation -- post Industrial Revolution, anyway.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 14, 2004
    #7
  8. tdd

    P.Hill Guest

    Re: OT - Re: java date calculations

    Chris Smith wrote:

    > Roedy Green wrote:
    >
    >>The USA [...] they say God rested on the
    >>7th day, and celebrate that on Sunday.


    > [...] Christians fully realized that
    > it was not the seventh day. Jewish Christians definitely observed the
    > Sabbath, in addition to celebrating eucharist on Sunday.


    Chris you beat me to the post. Roedy you've got to be careful with
    statements about all X, it shows a bias and usually can't be supported.
    In this case shows a lack of knowledge of the underlying 1700+ year
    history of Sunday ceremony/rest day.

    The term that drifted is "Sabbath", the practice that changed was
    when to rest and do religious ceremonies, the move was explicitly done. Which
    day is the 1st day of the week in the 7 day cycle hasn't actually moved.
    If you think it did I might ask how you
    would define such a move? Some prescriptive declaration from a historic
    document? Some common usage? "Weekend" comes close, but it seems to be in
    struggle with Sunday as the first day of the week. For example, some parts of
    the world put Monday in the far left column, but Sunday on the far left is much
    more common.

    Roedy, the following is equivalent:
    >in France, it is Monday = 2. In the US, it is Sunday=1 so much for
    >0-based.


    Did you mean Monday = 1?

    The point for Java Programmers is that Sunday = 1 and Saturday = 7,
    it is not uncommon to find DOW week calculations where a 0 works as well
    as a 7 when expecting to end up on Saturday. This is not always the case, so
    everyone should be careful.

    -Paul
     
    P.Hill, May 14, 2004
    #8
  9. tdd

    P.Hill Guest

    tdd wrote:

    Curiously if we move the SetFirstDayOfWeek up, so that it you
    are left with pushing a date in, moving to a MONDAY and getting
    the resulting date, the result is the Monday of the current week.

    The simplist way to demonstrate that is to just move the line
    up in the getWeekStartDate routine.

    > public String getWeekStartDate(){

    c.setFirstDayOfWeek(Calendar.MONDAY);
    > c.setTime(target);
    > c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    > return dateFormatter.format(c.getTime());
    > }


    Running the class with just the one-line change results in
    LAST Monday.
    05/15/2004
    Start Date:05/10/2004

    Starting with last Monday results in last Monday.
    05/10/2004
    Start Date:05/10/2004

    Starting with next Monday results in next Monday.
    05/17/2004
    Start Date:05/17/2004

    Apparently there is some unanticipated interaction between
    SetFirstDayOfWeek and the other sets.

    -Paul
     
    P.Hill, May 14, 2004
    #9
  10. Chris Smith wrote:
    > Incidentally, Monday as the first day of the week? That's a new one to
    > me. Must be a cultural/local thing.


    Nop. ISO Standard 8601:2000 (was already in the old ISO 8601:1988).

    /Thomas
     
    Thomas Weidenfeller, May 17, 2004
    #10
  11. tdd

    Roedy Green Guest

    On Mon, 17 May 2004 12:58:12 +0200, Thomas Weidenfeller
    <> wrote or quoted :

    >Nop. ISO Standard 8601:2000 (was already in the old ISO 8601:1988).


    that's why BigDate has both getIsoDayOfWeek and getDayOfWeek

    see http://mindprod.com/jgloss/products.html#BIGDATE

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 17, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,279
    Michael Borgwardt
    May 30, 2004
  2. hbetts3

    4/3/2028 and Date Calculations

    hbetts3, Jan 5, 2005, in forum: Java
    Replies:
    11
    Views:
    844
    P.Hill
    Jan 5, 2005
  3. androtech

    Week calculations (Date/Time)

    androtech, Jan 15, 2004, in forum: ASP General
    Replies:
    2
    Views:
    268
    dlbjr
    Jan 15, 2004
  4. Dr John Stockton

    Date Calculations

    Dr John Stockton, Mar 30, 2005, in forum: Javascript
    Replies:
    0
    Views:
    90
    Dr John Stockton
    Mar 30, 2005
  5. Replies:
    8
    Views:
    141
    Dr John Stockton
    Sep 17, 2005
Loading...

Share This Page