-3
     SimpleDateFormat dateformat2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    Date date = new Date();
    String d=dateformat2.format(date);

Now I want to iterate from current date to next 3 month date.I want to date format need to same. Any suggestion?

MCMastery
  • 3,099
  • 2
  • 20
  • 43
nand
  • 517
  • 2
  • 13
  • 29
  • What did you try? Also, please clarify a little. What do you mean by _I want to date format need to same_? – MCMastery Jul 01 '15 at 16:35
  • Familiarize yourself with [Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html). – VGR Jul 01 '15 at 17:07
  • possible duplicate of [Java: Iterating through start and end dates, best practice?](http://stackoverflow.com/questions/3109501/java-iterating-through-start-and-end-dates-best-practice) – Basil Bourque Jul 01 '15 at 23:05

1 Answers1

0

I'm not entirely sure what you mean in your question, but I'm guessing it has to do with iterating over dates for 3 months. If that is the case, you can try something like this:

for(int i = 0; i < month; i++) {
    for(int j = 0; j < day; j++) {
        //do something...
}}

Just set month to be the amount of months to iterate over (in this case 3), and day to be the number of days per month.

If the months in question don't necessarily have a consistent amount of days each, then you can try something a little different like this:

First set a variable to the days total to iterate. You can get at this by calculating from a set definition of days in each respective month. Then just iterate over the days

for(int i = 0; i < daysLeftToIterate; i++) {
    //do something...
}

Edit:

After looking into Calendar, I think there may be a way to use that efficiently to solve your issue. You can define a Calendar object in "lenient mode" as described on the API:

"Calendar has two modes for interpreting the calendar fields, lenient and non-lenient. When a Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1."

By doing that, you can define to search over a set amount of days, instead of months. For example, you can define the entire period of time as Month=January, Day=90, which would give the date of the 90th day in the year.

  • That 'if' is redundant - _no_ three consecutive months ever have the same number of days. – Toby Speight Jul 01 '15 at 17:02
  • i am thinking to implement calendar api for getting nex day like Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, +1); but not sure how to link date and calendar ?? – nand Jul 01 '15 at 17:23
  • got one way to doing it effectively String d3="2015-07-01 00:00:00"; SimpleDateFormat dateformat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Calendar calobj = Calendar.getInstance(); Date date = new Date(); Date date3 = dateformat3.parse(d3); Calendar cal= DateUntility.dateToCalendar(date3); cal.add(Calendar.DATE, +3); System.out.println(cal.getTime()); for (int i = 2; i < 15; i++) { cal.add(Calendar.DATE, i); d3=dateformat3.format(cal.getTime());} – nand Jul 02 '15 at 04:04