Java Data Type How to - Get last time of a Month


We would like to know how to get last time of a Month.


import java.util.Calendar;
import java.util.Date;
//from   w w  w  .  j a v  a  2s .c o  m
public class Main {
  public static void main(String[] argv) {
    System.out.println(lastTimeOfMonth(new Date()));

  public static Date lastTimeOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
    return cal.getTime();


The code above generates the following result.