Solved

help with date calculation

Posted on 2004-04-12
7
193 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
  • 3
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 20 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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