Solved

error in Date function

Posted on 2003-10-28
10
331 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
  • 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 50 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now