473,626 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GregorianCalend er 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
GregorianCalend ar 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 GregorianCalend ar(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 14875
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 GregorianCalend er 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
GregorianCalend ar 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 GregorianCalend ar(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.Field Position;
import java.text.Simpl eDateFormat;
import java.util.Grego rianCalendar;
public class WeekTest {

private GregorianCalend ar calendar;
public WeekTest(Gregor ianCalendar calendar) {
this.calendar = calendar;
this.calendar.s etFirstDayOfWee k(GregorianCale ndar.MONDAY);
this.calendar.s etMinimalDaysIn FirstWeek(4);
System.out.prin tln("\nStarting as: " +
calendar.getTim e().toString()) ;
}

public WeekTest(int year, int month, int date) {
this(new GregorianCalend ar(year, month, date, 1, 1));
}
public WeekTest() {
this(new GregorianCalend ar());
}
public String getWeek(){
String format = "yyyyww";
return doFormat(format , calendar);
}
public String getStartDate(){
calendar.set(Gr egorianCalendar .DAY_OF_WEEK,
GregorianCalend ar.MONDAY);
calendar.get(Gr egorianCalendar .MILLISECOND);
System.out.prin t("Week Start: " +
calendar.getTim e().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format , calendar);
}
public String getEndDate(){
calendar.set(Gr egorianCalendar .DAY_OF_WEEK,
GregorianCalend ar.SUNDAY);
System.out.prin t("Week End: " +
calendar.getTim e().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format , calendar);
}
private String doFormat(String format, GregorianCalend ar gc){
SimpleDateForma t sdf = new SimpleDateForma t(format);
FieldPosition fpos = new FieldPosition(0 );

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

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

WeekTest wsw = new WeekTest(2002,
GregorianCalend ar.OCTOBER, 6);
System.out.prin tln("Week: " + wsw.getWeek());
System.out.prin tln(wsw.getStar tDate());
System.out.prin tln(wsw.getEndD ate());
WeekTest wsw2 = new WeekTest();
System.out.prin tln("Week: " + wsw2.getWeek()) ;
System.out.prin tln(wsw2.getSta rtDate());
System.out.prin tln(wsw2.getEnd Date());
System.exit(0);
}
}

-end code--------------------------------------
Jul 17 '05 #2
Date date = ...;

Calendar cal = GregorianCalend ar.getInstance( );
cal.setTime(dat e);
cal.set(cal.HOU R_OF_DAY, 0);
cal.set(cal.MIN UTE, 0);
cal.set(cal.SEC OND, 0);
cal.set(cal.MIL LISECOND, 0);
cal.set(cal.DAY _OF_WEEK, cal.SUNDAY);
Date modifiedDate = cal.getTime();

"cg_news" <cg_news@eart h-N_0_S_P_A_M-link.net> wrote in message
news:vH******** *********@newsr ead1.news.pas.e arthlink.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 GregorianCalend er 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
GregorianCalend ar 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 GregorianCalend ar(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.Field Position;
import java.text.Simpl eDateFormat;
import java.util.Grego rianCalendar;
public class WeekTest {

private GregorianCalend ar calendar;
public WeekTest(Gregor ianCalendar calendar) {
this.calendar = calendar;
this.calendar.s etFirstDayOfWee k(GregorianCale ndar.MONDAY);
this.calendar.s etMinimalDaysIn FirstWeek(4);
System.out.prin tln("\nStarting as: " +
calendar.getTim e().toString()) ;
}

public WeekTest(int year, int month, int date) {
this(new GregorianCalend ar(year, month, date, 1, 1));
}
public WeekTest() {
this(new GregorianCalend ar());
}
public String getWeek(){
String format = "yyyyww";
return doFormat(format , calendar);
}
public String getStartDate(){
calendar.set(Gr egorianCalendar .DAY_OF_WEEK,
GregorianCalend ar.MONDAY);
calendar.get(Gr egorianCalendar .MILLISECOND);
System.out.prin t("Week Start: " +
calendar.getTim e().toString() + " = ");

String format = "yyyyMMdd";
return doFormat(format , calendar);
}
public String getEndDate(){
calendar.set(Gr egorianCalendar .DAY_OF_WEEK,
GregorianCalend ar.SUNDAY);
System.out.prin t("Week End: " +
calendar.getTim e().toString() + "

= ");
String format = "yyyyMMdd";
return doFormat(format , calendar);
}
private String doFormat(String format, GregorianCalend ar gc){
SimpleDateForma t sdf = new SimpleDateForma t(format);
FieldPosition fpos = new FieldPosition(0 );

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

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

WeekTest wsw = new WeekTest(2002,
GregorianCalend ar.OCTOBER, 6);
System.out.prin tln("Week: " + wsw.getWeek());
System.out.prin tln(wsw.getStar tDate());
System.out.prin tln(wsw.getEndD ate());
WeekTest wsw2 = new WeekTest();
System.out.prin tln("Week: " + wsw2.getWeek()) ;
System.out.prin tln(wsw2.getSta rtDate());
System.out.prin tln(wsw2.getEnd Date());
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
3975
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 I can determine, there are 14 possible calendar variations. Year starts on Sun, Mon..... Leap year starts on Sun, Mon.. I can label these early year "types" as a number between 0 and 13.
11
4639
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 having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
5
14884
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 geographical region. I have written a query which prompts the user for the start and end dates. It also filters for entries which pertain to the particular geographical region. I'm not sure where to go from here.
7
15487
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 DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
19
2823
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, Pacific and Alaska time zones in the US. It works okay if the computer is located in the Central timezone but not if it is located elsewhere. I know how to get the UTC time and the timezoneOffset but I can't figure out how to get from there to...
3
2037
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 that link's day and onwards. I have a problem with the page link part that it won't advance to the next page according but go back according to today's date. This is the calender script section // Display Calandar error_reporting('0'); ...
7
2361
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 date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using <html> <script...
7
2292
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 that info, but somewhere I've gone completely off the rails! Basically it is a room availability page for an intranet and should simply colour a calendar cell red if the room is booked, or green if it isn't. Rooms are typically booked by lecturers...
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.