Here you can find the source of getLastDayOfPreviousMonth(Date date, boolean isFormatDate)
public static Object getLastDayOfPreviousMonth(Date date, boolean isFormatDate)
//package com.java2s; //License from project: Open Source License import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class Main { static public final String FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss"; public static Object getLastDayOfPreviousMonth(Date date, boolean isFormatDate) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date);/*w w w .j av a 2 s . c o m*/ calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONTH) - 1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); if (isFormatDate) return formatDate(getLastDayOfMonth(calendar.getTime())); return getLastDayOfMonth(calendar.getTime()); } public static String formatDate(Date date) { return format(date, FORMAT_NORMAL); } public static Date getLastDayOfMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.DATE, 1); c.roll(Calendar.DAY_OF_MONTH, -1); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); return c.getTime(); } public static String format(Date date, String format) { SimpleDateFormat sf = new SimpleDateFormat(format); sf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return sf.format(date); } public static String format(Timestamp date, String format) { SimpleDateFormat sf = new SimpleDateFormat(format); sf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return sf.format(date); } public static Date format(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); return c.getTime(); } }