0

I have an ArrayList of integer numbers that represent day-of-month.For example my list contains 2,8,15(always in increasing order) so it will represent dates 2nd ,8th and 15th of any month.On these dates i have to generate some invoice.So like if I am generating invoice on 15th of any month then i need to get data from some table where date should be between previous invoice date(which is 8 according to arraylist) and 15th.Like if I am generating invoice on 2nd of any month then i need to get data between previous invoice date(which will be 15th of previous month according to arraylist) and 2nd of current month.

So my question is like if my code is running today and date is 2nd then how will i get previous invoice date as java date object.So that i can pass today's date and previous invoice date to filter out and get data from the table.

4 Answers4

0

You need to use java8+ or jodatime. Java 8 adopted jodatime so that's why there's a distinction. Jodatime's api is exactly what you are looking for.

SwampDev
  • 657
  • 5
  • 9
  • FYI: The Joda-Time project is now in maintenance mode with the team advising migration to the java-time classes. For use before Java 8, see the ThreeTen-Backport project and ThreeTenABP project. – Basil Bourque Oct 05 '16 at 17:44
0

You can just use the following code:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");

the string that you are parsing, which in this example is "21/12/2012" can be changed to whatever you want.
Hope this helps, if not, let me know what issue you're facing.

Anish Kumar
  • 1,465
  • 16
  • 20
0

Your question is confusing, but I can get you started. I do suggest you rewrite your question to be crystal clear, for your own benefit, to come to understand your problem thoroughly in plain language before you start coding.

First use precise terms to clarify your thinking. You do not start with an ArrayList of date, you have an ArrayList of integer numbers that represent day-of-month.

Next you need today's date. That requires a time zone. For any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate today = LocalDate.now( z );

You might find YearMonth class handy.

YearMonth ym = YearMonth.from( today );

You can get a date from that YearMonth.

LocalDate ld = ym.atDay( 15 );

You can get last day of month.

LocalDate endOfMonth = ym.atEndOfMonth();

You can do math, adding or subtracting months to the YearMonth.

YearMonth previousYm = ym.minusMonths( 1 );

You can retrieve a day-of-month from a LocalDate.

int dayOfMonth = myLocalDate.getDayOfMonth();

You can increment LocalDate with plus and minus methods.

LocalDate nextDate = myLocalDate.plusDays( 1 );

You should be able to put those pieces together to perform your particular business logic.

Tip: Stay away from the troublesome old legacy date-time classes. Use only java.time classes.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • @dheerajsinghal But you still have not written out your rules in generic language without referring to the example numbers. Write out the rules first, then follow with examples. This will make your coding *so* much easier. And, as I said, I gave you all the pieces you need. Stack Overflow is not a free-of-cost code-writing service. Indeed, I challenge you to post your own Answer here with working code based on what I showed you here. – Basil Bourque Oct 05 '16 at 17:54
  • in generic term , from the list i have to find index of today's date day(if there in the list) and then i have to get the day which is there in the list at previous index and the convert that into java date object. – dheeraj singhal Oct 05 '16 at 18:03
0

finally i solved my problem from below code.

private Date getPreviousInvoiceDateFromList(List<String> invoiceDatesList, int dayOfMonth,
      int lastDayOfMonth) {
    String dayOfMonthStr = String.valueOf(dayOfMonth);
    if (invoiceDatesList.size() == 1) {
      return new LocalDate().minusMonths(1).toDate();
    }
    int prevInvoiceDateIndex = -1;
    int todaysDateIndex = invoiceDatesList.indexOf(dayOfMonthStr);
    if (todaysDateIndex == 0) {
      prevInvoiceDateIndex = invoiceDatesList.size() - 1;
      String prevInvoiceDate = invoiceDatesList.get(prevInvoiceDateIndex);
      int prevInvoiceDateVal = Integer.parseInt(prevInvoiceDate);
      int noOfDaysDiff = lastDayOfMonth - prevInvoiceDateVal;
      int totalDaysDiff = noOfDaysDiff + dayOfMonth;
      return new LocalDate().minusDays(totalDaysDiff - 1).toDate();
    } else {
      prevInvoiceDateIndex = todaysDateIndex - 1;
      String prevInvoiceDate = invoiceDatesList.get(prevInvoiceDateIndex);
      int prevInvoiceDateVal = Integer.parseInt(prevInvoiceDate);
      int diff = dayOfMonth - prevInvoiceDateVal;
      return new LocalDate().minusDays(diff).toDate();
    }
  }