Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help with date calculation

Posted on 2004-04-12
7
Medium Priority
?
213 Views
Last Modified: 2010-04-01
hi...
i have a field combo box which shows dates for the last 15 days starting from today.the format is dd/mm/yyyy.
while the formating can be done with simpledate format...how can i calculate the last 15 days given the current date.i mean does anything like ( todays-date(in dd/mm/yyyy format) -15 ) work????
bcoz if the date is aug 2 ...wil it also show the dates of the previous months automatically..???

0
Comment
Question by:illusionz70
[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
  • 3
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 80 total points
ID: 10813554
Use java.util.Calendar to subtract 15 days from current date.  Rip this to bits:

import java.util.Calendar;

public class Last15 {
    public static void main(String args[]) {
        Calendar cal = Calendar.getInstance(); // creates calendar with todays date/time
        cal.add(Calendar.DATE, -15); // subtract 15 days
        for (int i = 0; i < 15; i++) {
            System.out.println(cal.getTime()); // print day (getTime returns a java.util.Date)
            cal.add(Calendar.DATE, 1); // add one day
        }
    }
}
0
 
LVL 4

Author Comment

by:illusionz70
ID: 10936487
well solved it myself ....

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

      Date todayDate1 = new Date();

      String today = sdf.format(todayDate1);

      String dateName[] = new String[15];

      GregorianCalendar[] gCalendar = new GregorianCalendar[15];

      Date[] date = new Date[15];

      for(int i=0;i<15;i++)

      {

            date[i] = new Date();

            gCalendar[i] = new GregorianCalendar();

            gCalendar[i].add(GregorianCalendar.YEAR-1,1);

            gCalendar[i].add(gCalendar[i].DAY_OF_YEAR,-i);

            date[i] = gCalendar[i].getTime();

            dateName[i]=(String)sdf.format(date[i]);            

      }

      %>
0
 
LVL 7

Expert Comment

by:searlas
ID: 10936778
What do you mean you solved it yourself?  Are you saying your ignored the calendar manipulation shown in my solution (using the add method to manipulate the date.. which you then go on to use in your solution?)

I wondered if you'd be kind enough to answer some questions about your solution:
What do you think this line does:
gCalendar[i].add(GregorianCalendar.YEAR-1,1);

(compared to gCalendar[i].add(gCalendar[i].DAY_OF_YEAR,-i) - which actually has a purpose)

Do you need to create 15 instances of GregorianCalendar and 15 instances of Date?  (i.e. do you every use them again, or do you just use the string format.)  I ask this because it seems like a particularly inefficient way of doing things.
0
 
LVL 4

Author Comment

by:illusionz70
ID: 10936966
well sorry for the statement "well solved it myself ...."
 but your solution would not work in my case as i needed 15 instatnces..becoz i need to keep track of what date is chosen.well but your post is quite an eyeopener as to how highly inefficient my code could be. but since i do use the date and calender again i guess i have to do it. i will post again as soon as i do some more tweaking ...
thanks for the post
 
0
 
LVL 7

Expert Comment

by:searlas
ID: 10937152
What really caught my eye was this:
gCalendar[i].add(GregorianCalendar.YEAR-1,1);

I don't think that actually does anything (fortunately).  What you've done there is use a constant (Calendar.YEAR) and then subtract one from it..., fortunately it does not appear to have had any bad effects, but if it was intentional, then it would be as mad as changing:

gCalendar[i].add(gCalendar[i].DAY_OF_YEAR,-i);

to:

gCalendar[i].add(gCalendar[i].HOUR_OF_DAY-5,-i);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 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