Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

error in Date function

Posted on 2003-10-28
10
Medium Priority
?
337 Views
Last Modified: 2012-05-04
Hi
I am using date function as I have to display the date and day of one week starting from today in one comboBox and adding 6 months to this date in another combobox(as in MS project Information for Periodic Process)
For this I am using the following function which returns me a day and date, but the day returned is false.
Can anyone help me out or provide me any other simple answer

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

for(int i = 0; i<7; i++)
    jComboBox11.addItem(getDate(new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+6, cal.get(Calendar.DAY_OF_MONTH)+i ), "E dd.MM.yy"));

public String getDate(Date dt, String format) {
    Date d = new Date(dt.getTime());
    if(format== null)
        format = "dd.MM.yyyy";
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);

    return sdf.format(d);
  }

Thanks Balap
0
Comment
Question by:balap
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 

Author Comment

by:balap
ID: 9634264
when the day is Tuesday it returns me wednesday and so on. one day added to the present day of the week.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 9634753
Can you add 6 months to this month (10) giving the 16th month, and call that a valid date?

I *think* you are trying to create a date that is the 28.16.2003 which is a bit wrong...

Try:

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cal.add( Calendar.MONTH, 6 ) ;
for( int i = 0 ; i < 7 ; i++ )
{
  jComboBox11.addItem( getDate( new Date( cal.getTimeInMillis() ), "E dd.MM.yy" ) ) ;
  cal.add( Calendar.DAY_OF_YEAR, 1 ) ;
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634774
Hang on...

it is adding 6 months...

the 28th of April (which is six months away) IS a Wednesday...

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 9634782
Just because today is a tuesday, does not mean that in 6 months time it is a tuesday too...

Months are non-uniform (as are years)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634890
WHat you *might* be meaning to do, is add 26 weeks instead of 6 months...

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cal.add( Calendar.WEEK_OF_YEAR, 26 ) ;
for( int i = 0 ; i < 7 ; i++ )
{
  jComboBox11.addItem( getDate( new Date( cal.getTimeInMillis() ), "E dd.MM.yy" ) ) ;
  cal.add( Calendar.DAY_OF_YEAR, 1 ) ;
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634899
Which gives you Tuesday the 27th April...

Not 6 months, but near enough, and the same day as today...
0
 

Author Comment

by:balap
ID: 9640037
Hi I tested it for the present date also. but it gives me thursday althought it is wednesday today.
  for(int i = 0; i<7; i++)
    jCBStartDate.addItem(getDate(new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)+i ), "E dd.MM.yy"));
0
 

Author Comment

by:balap
ID: 9640072
Thanks for the answer. i give u full points
0
 

Author Comment

by:balap
ID: 9640099
anyway this date(int yy, int mm, int dd) constructor has been depricated. your answer has solved both the answers but i coulnt understand why its giving me wrong day inspite of the right date.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9640357
The day was right too :-)

It's just that 6 months from today (Wednesday) is a Thursday...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question