Java Month Get getMonthEnd(Date date)

Here you can find the source of getMonthEnd(Date date)

Description

get Month End

License

Open Source License

Declaration

public static Date getMonthEnd(Date date) 

Method Source Code


//package com.java2s;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {

    public static Date getMonthEnd(Date date) {
        int year = Integer.parseInt(FormatDate(date, "yyyy"));
        int month = Integer.parseInt(FormatDate(date, "MM"));
        int day = Integer.parseInt(FormatDate(date, "dd"));

        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, 0, 0, 0);
        int monthLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String newDateStr = FormatDate(date, "yyyy") + "-" + FormatDate(date, "MM") + "-";
        if (monthLength < 10)
            newDateStr += "0" + monthLength;
        else/*  w ww.j  av  a2 s .c o  m*/
            newDateStr += "" + monthLength;
        return stringToDateShort(newDateStr);
    }

    public static String FormatDate(Date date, String sf) {
        if (date == null)
            return "";
        SimpleDateFormat dateformat = new SimpleDateFormat(sf);
        return dateformat.format(date);
    }

    public static Date stringToDateShort(String dateString) {
        String sf = "yyyy-MM-dd";
        Date dt = stringToDate(dateString, sf);
        return dt;
    }

    public static Date stringToDate(String dateString) {
        String sf = "yyyy-MM-dd HH:mm:ss";
        Date dt = stringToDate(dateString, sf);
        return dt;
    }

    public static Date stringToDate(String dateString, String sf) {
        ParsePosition pos = new ParsePosition(0);
        SimpleDateFormat sdf = new SimpleDateFormat(sf);
        Date dt = sdf.parse(dateString, pos);
        return dt;
    }
}

Related

  1. getMonthBegin(String strdate)
  2. getMonthBegin(String strdate)
  3. getMonthByNow(int month)
  4. getMonthCount(String from, String to)
  5. getMonthDesc(Date month)
  6. getMonthEnd(String strdate)
  7. getMonthEnd(String strdate)
  8. getMonthEndDate(Date date)
  9. getMonthFirstDate(final Date date, final String format)