1

For a specific month and year,I want to get the month start and end date for the ISO year using Joda time library in Java. Like say for example, I have month as March and year 2014, SO I want to know the starting and end date for the month March in ISO year.

Apart from joda-time library, Is there also any other way to get the same thing.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reena Upadhyay
  • 1,977
  • 20
  • 35
  • 2
    http://stackoverflow.com/questions/3083781/start-and-end-date-of-a-current-month check the 2nd answer – Serhiy Oct 09 '14 at 14:58

2 Answers2

5

Maybe this could help:

DateTime dt = new DateTime().withYear(2014).withMonthOfYear(3);
DateTime start = dt.withDayOfMonth(1).withTimeAtStartOfDay();
DateTime end = start.plusMonths(1).minusMillis(1);
Krayo
  • 2,492
  • 4
  • 27
  • 45
  • 2
    Rather than try to get last moment of last day, a better practice is the Half-Open approach. With Half-Open a span of time is defined where the beginning is inclusive and the ending is exclusive. So a month of date-time would be from the first moment of first day of month going up to *but not including* the first moment of the first day of the next month. One reason for this is that the last moment is infinitely divisible as a fractional second. This example uses milliseconds resolution. But this value might fail to match in a database using microseconds, or nanoseconds in Java 8's java.time. – Basil Bourque Oct 09 '14 at 20:20
2
LocalDate monthBegin = new LocalDate().withDayOfMonth(1);
LocalDate monthEnd = new LocalDate().plusMonths(1).withDayOfMonth(1).minusDays(1);
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58