By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Help with date / calendar calculations.

P: n/a
In short, what I am trying to do is, based on a date, calculate the week of
year (as described in ISO 8601), then calculate the first and last date in
this week period and return them in the format CCYYMMDD. Sounds easy
enough, right??

I am attempting to accomplish this by creating a GregorianCalender which
will get me the week of year. Then by changing the day of week to 1 (start
of week) i'm trying the get the first day of the week, and day_of_week = 7
should get me the last. Maybe?

I think I may have successfully done this by using an instance of the
GregorianCalendar with the no argument constructor, which appears to
default to the current system date/time. However, when I attempt to do the
same with the GregorianCalendar(int year, int month, int date) constructor,
it does not work. It seems that my attemps to set the DAY_OF_WEEK value
have no effect?

Some code samples below,
Thanks for any help you can offer...
Carl.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
cg_news wrote:
In short, what I am trying to do is, based on a date, calculate the week
of year (as described in ISO 8601), then calculate the first and last date
in this week period and return them in the format CCYYMMDD. Sounds easy
enough, right??

I am attempting to accomplish this by creating a GregorianCalender which
will get me the week of year. Then by changing the day of week to 1 (start
of week) i'm trying the get the first day of the week, and day_of_week = 7
should get me the last. Maybe?

I think I may have successfully done this by using an instance of the
GregorianCalendar with the no argument constructor, which appears to
default to the current system date/time. However, when I attempt to do the
same with the GregorianCalendar(int year, int month, int date)
constructor, it does not work. It seems that my attemps to set the
DAY_OF_WEEK value have no effect?

Some code samples below,
Thanks for any help you can offer...
Carl.


Doh!
Code:
-start code--------------------------------------
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
public class WeekTest {

private GregorianCalendar calendar;
public WeekTest(GregorianCalendar calendar) {
this.calendar = calendar;
this.calendar.setFirstDayOfWeek(GregorianCalendar. MONDAY);
this.calendar.setMinimalDaysInFirstWeek(4);
System.out.println("\nStarting as: " +
calendar.getTime().toString());
}

public WeekTest(int year, int month, int date) {
this(new GregorianCalendar(year, month, date, 1, 1));
}
public WeekTest() {
this(new GregorianCalendar());
}
public String getWeek(){
String format = "yyyyww";
return doFormat(format, calendar);
}
public String getStartDate(){
calendar.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.MONDAY);
calendar.get(GregorianCalendar.MILLISECOND);
System.out.print("Week Start: " +
calendar.getTime().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format, calendar);
}
public String getEndDate(){
calendar.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.SUNDAY);
System.out.print("Week End: " +
calendar.getTime().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format, calendar);
}
private String doFormat(String format, GregorianCalendar gc){
SimpleDateFormat sdf = new SimpleDateFormat(format);
FieldPosition fpos = new FieldPosition(0);

StringBuffer b = new StringBuffer();
StringBuffer sb = sdf.format(gc.getTime(), b, fpos);

return sb.toString();
}
public static void main (String[] args) {

WeekTest wsw = new WeekTest(2002,
GregorianCalendar.OCTOBER, 6);
System.out.println("Week: " + wsw.getWeek());
System.out.println(wsw.getStartDate());
System.out.println(wsw.getEndDate());
WeekTest wsw2 = new WeekTest();
System.out.println("Week: " + wsw2.getWeek());
System.out.println(wsw2.getStartDate());
System.out.println(wsw2.getEndDate());
System.exit(0);
}
}

-end code--------------------------------------
Jul 17 '05 #2

P: n/a
Date date = ...;

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(date);
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
cal.set(cal.DAY_OF_WEEK, cal.SUNDAY);
Date modifiedDate = cal.getTime();

"cg_news" <cg_news@earth-N_0_S_P_A_M-link.net> wrote in message
news:vH*****************@newsread1.news.pas.earthl ink.net...
cg_news wrote:
In short, what I am trying to do is, based on a date, calculate the week
of year (as described in ISO 8601), then calculate the first and last date in this week period and return them in the format CCYYMMDD. Sounds easy
enough, right??

I am attempting to accomplish this by creating a GregorianCalender which
will get me the week of year. Then by changing the day of week to 1 (start of week) i'm trying the get the first day of the week, and day_of_week = 7 should get me the last. Maybe?

I think I may have successfully done this by using an instance of the
GregorianCalendar with the no argument constructor, which appears to
default to the current system date/time. However, when I attempt to do the same with the GregorianCalendar(int year, int month, int date)
constructor, it does not work. It seems that my attemps to set the
DAY_OF_WEEK value have no effect?

Some code samples below,
Thanks for any help you can offer...
Carl.
Doh!
Code:
-start code--------------------------------------
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
public class WeekTest {

private GregorianCalendar calendar;
public WeekTest(GregorianCalendar calendar) {
this.calendar = calendar;
this.calendar.setFirstDayOfWeek(GregorianCalendar. MONDAY);
this.calendar.setMinimalDaysInFirstWeek(4);
System.out.println("\nStarting as: " +
calendar.getTime().toString());
}

public WeekTest(int year, int month, int date) {
this(new GregorianCalendar(year, month, date, 1, 1));
}
public WeekTest() {
this(new GregorianCalendar());
}
public String getWeek(){
String format = "yyyyww";
return doFormat(format, calendar);
}
public String getStartDate(){
calendar.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.MONDAY);
calendar.get(GregorianCalendar.MILLISECOND);
System.out.print("Week Start: " +
calendar.getTime().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format, calendar);
}
public String getEndDate(){
calendar.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.SUNDAY);
System.out.print("Week End: " +
calendar.getTime().toString() + "

= ");
String format = "yyyyMMdd";
return doFormat(format, calendar);
}
private String doFormat(String format, GregorianCalendar gc){
SimpleDateFormat sdf = new SimpleDateFormat(format);
FieldPosition fpos = new FieldPosition(0);

StringBuffer b = new StringBuffer();
StringBuffer sb = sdf.format(gc.getTime(), b, fpos);

return sb.toString();
}
public static void main (String[] args) {

WeekTest wsw = new WeekTest(2002,
GregorianCalendar.OCTOBER, 6);
System.out.println("Week: " + wsw.getWeek());
System.out.println(wsw.getStartDate());
System.out.println(wsw.getEndDate());
WeekTest wsw2 = new WeekTest();
System.out.println("Week: " + wsw2.getWeek());
System.out.println(wsw2.getStartDate());
System.out.println(wsw2.getEndDate());
System.exit(0);
}
}

-end code--------------------------------------

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.