[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

help with date calculation

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
illusionz70
Asked:
illusionz70
  • 3
  • 2
1 Solution
 
searlasCommented:
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
 
illusionz70Author Commented:
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
 
searlasCommented:
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
 
illusionz70Author Commented:
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
 
searlasCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now