Solved

help with date calculation

Posted on 2004-04-12
7
197 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
We take a look at some of the most common obstacles that IT teams run into as they work relentlessly to keep all the alarms and sirens from going off at once.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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