Java Month Day lastDayOfMonth(String src)

Here you can find the source of lastDayOfMonth(String src)

Description

last Day Of Month

License

Open Source License

Parameter

Parameter Description
src a parameter

Exception

Parameter Description
ParseException an exception

Declaration

public static String lastDayOfMonth(String src) throws ParseException 

Method Source Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.text.DecimalFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    /**/* www. j  a v a  2 s .c o  m*/
     *
     * @param src
     * @return
     * @throws ParseException
     */
    public static String lastDayOfMonth(String src) throws ParseException {
        return lastDayOfMonth(src, "yyyyMMdd");
    }

    public static String lastDayOfMonth(String src, String format) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = check(src, format);

        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.KOREA);
        SimpleDateFormat monthFormat = new SimpleDateFormat("MM", Locale.KOREA);

        int year = Integer.parseInt(yearFormat.format(date));
        int month = Integer.parseInt(monthFormat.format(date));
        int day = lastDay(year, month);

        DecimalFormat fourDf = new DecimalFormat("0000");
        DecimalFormat twoDf = new DecimalFormat("00");
        String tempDate = String.valueOf(fourDf.format(year)) + String.valueOf(twoDf.format(month))
                + String.valueOf(twoDf.format(day));
        date = check(tempDate, format);

        return formatter.format(date);
    }

    /**
     *
     * @param s   date string you want to check.
     * @param format string representation of the date format. For example, "yyyy-MM-dd".
     * @return  date Date
     * @throws ParseException error info
     */
    public static Date check(String s, String format) throws ParseException {
        if (s == null)
            throw new ParseException("date string to check is null", 0);
        if (format == null)
            throw new ParseException("format string to check date is null", 0);

        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = null;
        try {
            date = formatter.parse(s);
        } catch (ParseException e) {
            throw new ParseException(" wrong date:\"" + s + "\" with format \"" + format + "\"", 0);
        }

        if (!formatter.format(date).equals(s))
            throw new ParseException("Out of bound date:\"" + s + "\" with format \"" + format + "\"", 0);
        return date;
    }

    /**
     *
     * @param year
     * @param month
     * @return int
     */
    public static int lastDay(int year, int month) {
        int day = 0;
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day = 31;
            break;
        case 2:
            if ((year % 4) == 0) {
                if ((year % 100) == 0 && (year % 400) != 0) {
                    day = 28;
                } else {
                    day = 29;
                }
            } else {
                day = 28;
            }
            break;
        default:
            day = 30;
        }
        return day;
    }
}

Related

  1. isLastDayOfMonth()
  2. isLastDayOfMonth(Calendar cal)
  3. lastDayOfLastMonth()
  4. lastdayofmonth()
  5. lastDayOfMonth(String batdate)
  6. previousMonthFirstDay()