Solved

help with date calculation

Posted on 2004-04-12
7
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A procedure for exporting installed hotfix details of remote computers using powershell
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

14 Experts available now in Live!

Get 1:1 Help Now