java date calculations

T

tdd

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);
}
}
 
M

Michael Rauscher

tdd said:
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
 
C

Chris Smith

tdd said:
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
 
R

Roedy Green

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
 
C

Chris Smith

Roedy said:
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
 
P

P.Hill

Chris said:
Roedy said:
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

P.Hill

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
 
T

Thomas Weidenfeller

Chris said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top