Home arrow static arrow Java Programming [Archive] - Dates Of Current Week
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Dates Of Current Week
This topic has 8 replies on 1 page.

Posts:57
Registered: 6/15/04
Dates Of Current Week  
Jul 22, 2004 11:47 PM



 
Hi Friends,

I want to get the days of the current week along with the dates

I am writing the following code

import java.util.*;

class TryDate
{
public static void main(String args[])
{
Calendar calendar = new GregorianCalendar();
java.util.Date date=new java.util.Date();
calendar.setTime(date);
System.out.println("YEAR : " + calendar.get(Calendar.YEAR));
System.out.println("MONTH : " + calendar.get(Calendar.MONTH));
System.out.println("DAY OF MONTH : " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY OF YEAR : " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK OF YEAR : " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK OF MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY OF WEEK : " + calendar.get(Calendar.DAY_OF_WEEK));
}

}

I am getting the output as

YEAR : 2004
MONTH : 6
DAY OF MONTH : 23
DAY OF YEAR :205
WEEK OF YEAR : 30
WEEK OF MONTH: 4
DAY OF WEEK : 6

My question is I want to get the dates of the current week of current year i.e. week30 of year 2004

How do i get it?

Thanx

Pooja
 

Posts:1,995
Registered: 12/10/00
Re: Dates Of Current Week  
Jul 22, 2004 11:58 PM (reply 1 of 8)



 
Set the calendar to the first day of this week and make a loop by adding one day until you reach the end of the week.

HTH
Mike
 

Posts:37
Registered: 7/23/04
Re: Dates Of Current Week  
Jul 23, 2004 5:22 AM (reply 2 of 8)



 
hello here is your solution.
you can see the code which I given here
If you want any difficulty thn contact me at chirag813@yahoo.com

import java.util.*;

public class WeekDates {

public static void main(String args[])
{
Calendar calendar = new GregorianCalendar();
java.util.Date date=new java.util.Date();
calendar.setTime(date);
System.out.println("YEAR : " + calendar.get(Calendar.YEAR));
System.out.println("MONTH : " + calendar.get(Calendar.MONTH));
System.out.println("DAY OF MONTH : " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY OF YEAR : " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK OF YEAR : " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK OF MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY OF WEEK : " + calendar.get(Calendar.DAY_OF_WEEK));

Date startDt=new Date();
startDt.setDate((calendar.get(Calendar.DAY_OF_MONTH)-calendar.get(Calendar.DAY_OF_WEEK))+1);
for(int i=0;i<=6;i++)
{
Date dt=new Date();
dt.setDate(startDt.getDate());
startDt.setDate(startDt.getDate()+1);
System.out.println(" Date "+(i+1)+ ": "+dt.toString());
}
}

}

 

Posts:24,036
Registered: 2/3/03
Re: Dates Of Current Week  
Jul 23, 2004 5:26 AM (reply 3 of 8)



 
hello here is your solution.
you can see the code which I given here
If you want any difficulty thn contact me at
chirag813@yahoo.com

First, when you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read and prevents accidental markup from array indices like [i].

Second, don't ask people to reply by private email. Most Forum regulars believe solving problems should be a public, transparent process during which a first try at an answer can and should be corrected if someone more knowledgeable notices that it is incomplete or incorrect. Also, they get some of their reward for being respondents from being seen to be competent and knowledgeable by their peers (not to mention the possibility of collecting some of those precious [url=http://forum.java.sun.com/rewardFaq.jsp]Duke Dollars[/url]).
 

Posts:24,036
Registered: 2/3/03
Re: Dates Of Current Week  
Jul 23, 2004 5:40 AM (reply 4 of 8)



 
hello here is your solution.
you can see the code which I given here
If you want any difficulty thn contact me at
chirag813@yahoo.com

Deprecated methods (e.g., Date.setDate()) are not good examples to use when providing solutions to readers, IMHO...
 

Posts:5,627
Registered: 3/22/04
Re: Dates Of Current Week  
Jul 23, 2004 5:43 AM (reply 5 of 8)



 
I agree with yawmark. Why are you using the Date class when you need only use the Calendar class? Most, if not all of Date's methods are deprecated.
 

Posts:10
Registered: 6/3/04
Re: Dates Of Current Week  
Jul 23, 2004 5:44 AM (reply 6 of 8)



 
Second, don't ask people to reply by private
email
.

TRUE
 

Posts:24,036
Registered: 2/3/03
Re: Dates Of Current Week  
Jul 23, 2004 5:48 AM (reply 7 of 8)



 
Example:
DateFormat df = DateFormat.getDateInstance();Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK));for (int count = 0; count < 7; count++) {    System.out.println(df.format(calendar.getTime()));    calendar.add(Calendar.DATE, 1);}
 

Posts:466
Registered: 6/5/01
Re: Dates Of Current Week  
Jul 23, 2004 6:05 AM (reply 8 of 8)



 
If you want any difficulty thn contact me at
chirag813@yahoo.com

After looking at the code you posted, I would say yes, the OP should contact you if he "want any difficulty".
 
This topic has 8 replies on 1 page.