Yes, although it is not as beautiful as it could be:
import org.joda.time.*; import org.joda.time.chrono.*; import org.joda.time.field.*; public class Test { public static void main(String[] args) { GregorianChronology calendar = GregorianChronology.getInstance(); DateTimeField field = calendar.dayOfMonth(); for (int i = 1; i < 12; i++) { LocalDate date = new LocalDate(2010, i, 1, calendar); System.out.println(field.getMaximumValue(date)); } } }
Please note that I hard-coded the assumption that there are 12 months, and we are interested in 2010. I clearly chose the Gregorian chronology, although - in other chronologies you would have received different answers, of course, (And the "12 months" cycle will not be a valid assumption either ...)
I went for LocalDate , not DateTime , to get the value, to emphasize (albeit weakly) that this value does not depend on the time zone.
It is still not as easy as it seems to think. I do not know what will happen if you use one chronology to build LocalDate , but set the maximum field value in another chronology. I have some ideas on what might happen knowing a certain amount about Joda's time, but this is probably not a good idea :)
Jon Skeet Sep 22 '10 at 19:52 2010-09-22 19:52
source share