Aleksandar Krstikj.com

Get the last day of a month in GWT

by on Sep.21, 2012, under Code, IT

A while back I implemented a report system in a project I was working on where you would send a server two dates (a ‘from date’ and a ‘to date’) and it will return results between those two dates. Well yesterday I had to extend that report system for monthly reports so I figured a drop-down menu with month selection is more convenient to the user than forcing him to manually set the dates from the first of the month to the last of that same month. So I created the drop-down menu, populated it with 12 month entries and moved on to program the logic behind the change handler. This is where I ran into a small problem.

You see, under normal circumstances when working with Java I would use the Java.Util.Calendar class for this, but unfortunately this class is not emulated in GWT (not yet at least). There is a slightly less powerful class that does calendar manipulations though, and is found under com.google.gwt.user.datepicker.client.CalendarUtil. So I figured I’ll use this. I took a quick look at the methods it offers and I wrote a quick and dirty way to get the last day of a certain month.

Here it is:

DateTimeFormat fmt = DateTimeFormat.getFormat("dd.MM.yyyy");
Date date = new Date();

date.setDate(1);
date.setMonth(selectedMonth);
dateFrom.setText(fmt.format(date));

date.setMonth(selectedMonth+1);
CalendarUtil.setToFirstDayOfMonth(date);
CalendarUtil.addDaysToDate(date, -1);
dateTo.setText(fmt.format(date));

So what I’m basically doing is this:

  1. First taking the current date by creating a new date object.
  2. Then I set the date to the first of the month and the month to the selected month from the drop-down list. (be advised here that the month starts from 0 and not 1, i.e. January = 0 and December = 11)
  3. I use that date to set the ‘date from’ field. This will represent the first of the month of the current year. (you can also change the year if you need to, just be sure to subtract 1900 when using date.setYear(), i.e. date.setYear(desiredYear - 1900))
  4. Then I set the month to the current month + 1 which moves me into the next month.
  5. Now this is where the CalendarUtil class comes into play. CalendarUtil.setToFirstDayOfMonth(date); sets the date of the date object to the first of the selected month which in this case means the first of the next month.
  6. And now CalendarUtil.addDaysToDate(date, -1); adds the specified number of days to the date. Since I have -1 specified it will actually subtract 1 from the date which means it will move from the first of the next month to the last of the currently selected month.
  7. Then I just set that date in the ‘date to’ field and make my server call.
  8. That’s it!

I hope this helps someone facing the same issue. If you have a better, more elegant solution write a comment and I’ll update the post with it.

Thanks for reading!

:, , , , ,

6 Comments for this entry

  • Sam

    Hey, that rly helps me out, thank you!

    I just needed to get the last day of the current month via getDate(), so

    Date date = new Date();
    date.setDate(1);
    date.setMonth(date.getMonth()+1);
    CalendarUtil.addDaysToDate(date, -1);

    worked fine for me. Wondered why you use

    CalendarUtil.setToFirstDayOfMonth(date);

    when you set your Date already to 1?

    Thanks for sharing! 🙂

    • Ethan Hunt

      That’s a really good question and my answer to it is: “I have no idea why I did it” 🙂

      What you’re saying makes perfect sense. I think it might be there because I was trying out different things and either date.setDate(1); or CalendarUtil.setToFirstDayOfMonth(date); is something that was leftover from previous tries. And then I just forgot about it. In any case, you definitely do not need both.

      Glad I could help 🙂

  • Giordano

    Hi, did you test this also with december?

    I’m mainly wondering if this line would work in case it is december ^^:
    date.setMonth(selectedMonth+1);

    Perhaps it should be corrected like this:
    date.setMonth( (selectedMonth+1)%12 );

    regards

    • Ethan Hunt

      Technically, you do have a point. The documentation for setMonth() specifies that it accepts input from 0-11 corresponding to January – December.

      However, the function is smart enough that if you give it a number higher then 11 like 13 it jumps into the next year.

      So in the case of December (11) when you go to date.setMonth(selectedMonth+1); the 12 you get will actually be January (0) of the following year. Then you’ll get the first of January, and -1 will give you the last day of December of the current year.

      This behavior is similar to the setDate() function which accepts numbers from 1-31. If you, however, enter something like 45 you will get a date corresponding to the middle of next month.

      Hope I explained this well enough 🙂

  • Giordano

    I might have found a solution that only uses CalendarUtil.
    This should handle all possible special cases (Google is doing it for you ^^)

    Date d = getUserSelectedDate();
    CalendarUtil.setToFirstDayOfMonth(d);
    CalendarUtil.addMonthsToDate(d, 1);
    CalendarUtil.addDaysToDate(d, -1);

    • Ethan Hunt

      Yup, that should do the trick. CalendarUtil.addMonthsToDate(d, 1); should do the same as date.setMonth(selectedMonth+1); essentially replicating the behavior.

      Thanks for sharing! 🙂

Leave a Reply