Java SQL Date Create getLastDayOfPreviousMonth(Date date, boolean isFormatDate)

Here you can find the source of getLastDayOfPreviousMonth(Date date, boolean isFormatDate)

Description

get Last Day Of Previous Month

License

Open Source License

Declaration

public static Object getLastDayOfPreviousMonth(Date date, boolean isFormatDate) 

Method Source Code


//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();
    }
}

Related

  1. getLastDay(Date date)
  2. getLastDay(Date dt)
  3. getLastDay(java.util.Date dt)
  4. getLastDayofMonth(Date date1)
  5. getLastDayOfMonth(java.sql.Date date)
  6. getLastSundayDate()
  7. getLongDate(String strDate)
  8. getSqlCurrentDate()
  9. getSqlCurrentDate()