Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java date calculations

Reply
Thread Tools

java date calculations

 
 
tdd
Guest
Posts: n/a
 
      05-14-2004
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);
}
}
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2004
On 14 May 2004 09:47:06 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      05-14-2004
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

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-14-2004
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2004
On 14 May 2004 09:47:06 -0700, (E-Mail Removed) (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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2004
On Fri, 14 May 2004 11:27:39 -0600, Chris Smith <(E-Mail Removed)>
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.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-14-2004
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
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-14-2004
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

 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-14-2004
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

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      05-17-2004
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:198.

/Thomas
 
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
Date calculations in multiple time zones lduperval@gmail.com Javascript 8 09-17-2005 03:26 PM
Date Calculations Dr John Stockton Javascript 0 03-30-2005 05:00 PM
4/3/2028 and Date Calculations hbetts3 Java 11 01-05-2005 09:00 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Week calculations (Date/Time) androtech ASP General 2 01-15-2004 10:11 PM



Advertisments