473,231 Members | 1,719 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,231 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 14845
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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...

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.