Java Month Get getEndDateOfMonth(String yyyymmdd)

Here you can find the source of getEndDateOfMonth(String yyyymmdd)

Description

get End Date Of Month

License

Open Source License

Declaration

public static Date getEndDateOfMonth(String yyyymmdd) 

Method Source Code

//package com.java2s;
/**/*from   w  ww .j  a  v a 2s  . c om*/
 * Copyright (c) 2008 OpenSprout Team.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 */

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static Date getEndDateOfMonth(String yyyymmdd) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");

        String yyyymm = yyyymmdd.substring(0, 7);
        int year = Integer.parseInt(yyyymm.substring(0, 4));
        int month = Integer.parseInt(yyyymm.substring(5));

        String endDate = "31";
        if (month == 2)
            if (year % 4 == 0)
                endDate = "29";
            else
                endDate = "28";
        else if (month % 2 == 0)
            endDate = "30";

        Date date = null;
        try {
            date = sformat.parse(yyyymm + "-" + endDate);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    public static Date getEndDateOfMonth(Date date) {
        return getEndDateOfMonth(makeYYYYMMWithHypon(date));
    }

    public static String makeYYYYMMWithHypon(Date date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }
}

Related

  1. getAllMonths(String date1, String date2)
  2. getBeforeMonth(int amount)
  3. getBeforeOrAfterDate(String strDate, int months)
  4. getCurMonth2Long()
  5. getCurrMonthLastDay()
  6. getEngMonth()
  7. getIntervalMonths(String startDate, String endDate)
  8. getIntevalMonth(String strDate1, String strDate2)
  9. getMonth()