473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with date / calendar calculations.

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
2 14852
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mick White | last post by:
According to the Safari browser the world began on "Fri Dec 13 1901 15:45:52 GMT-0500", but I need to be able to get around this limitation. I am interested in dates from 1500 to 1901, as far as...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
19
by: Melvin G. Sheppers | last post by:
I want to display the time in a particular timezone (US Central) regardless where the computer is located. I have written the script below which displays the time in the Eastern, Central, Mountain,...
3
osward
by: osward | last post by:
Hi, everyone, I had managed to make use of the date link from a simple calendar script to my query table. When I click on the date's link or Prev and Next Month link, The table first row will be...
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
7
by: William (Tamarside) | last post by:
Please, if you have the time and knowledge to help me I'd truly appreciate it! I need to build a calendar page that displays available/unavailable info from a DB and colour a cell according to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.