Java Month Get getThisMonthLastDate()

Here you can find the source of getThisMonthLastDate()

Description

get This Month Last Date

License

Open Source License

Declaration

public static int getThisMonthLastDate() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    private static SimpleDateFormat intSDF = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat defaultFormater = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat customizedFormater = new SimpleDateFormat("yyyyMMdd");

    public static int getThisMonthLastDate() {
        Calendar calendar = Calendar.getInstance();
        Calendar cpcalendar = (Calendar) calendar.clone();
        cpcalendar.set(Calendar.DAY_OF_MONTH, 1);
        cpcalendar.add(Calendar.MONTH, 1);
        cpcalendar.add(Calendar.DATE, -1);
        return Integer.parseInt(intSDF.format(new Date(cpcalendar.getTimeInMillis())));
    }/*from  w  ww . ja va2  s . c  o m*/

    public static int getThisMonthLastDate(int date) {
        Calendar calendar = Calendar.getInstance();
        int year = Integer.parseInt(String.valueOf(date).substring(0, 4));
        int month = Integer.parseInt(String.valueOf(date).substring(4, 6));
        int day = Integer.parseInt(String.valueOf(date).substring(6, 8));
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -day);
        return Integer.parseInt(intSDF.format(new Date(calendar.getTimeInMillis())));
    }

    public static String format(Date date, String pattern) {
        synchronized (customizedFormater) {
            if (null != pattern && !"".equals(pattern.trim())) {
                customizedFormater.applyPattern(pattern);
            } else {
                throw new IllegalArgumentException("pattern can not be empty");
            }
            return customizedFormater.format(date);
        }
    }

    public static String format(Date date) {
        synchronized (defaultFormater) {
            return defaultFormater.format(date);
        }
    }
}

Related

  1. getThisMonth()
  2. getThisMonth()
  3. getThisMonth()
  4. getThisMonthAndCycle()
  5. getThisMonthDate()
  6. getTsOfMonth(Date date, boolean isEnd, boolean isPrevious)
  7. getWeekOfMonth(String datetime)
  8. getWorkMonthLastDay(Date date)
  9. getYesterdayOnLastMonth(String yesterday)