Java Month Get getMonthLength(String countDate)

Here you can find the source of getMonthLength(String countDate)

Description

get Month Length

License

Apache License

Declaration

public static long getMonthLength(String countDate) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static long getMonthLength(String countDate) throws ParseException {
        String firstDay = countDate.substring(0, countDate.length() - 2) + "01";
        Date startDate = strToDate(firstDay);
        Date endDate = getNextMonth(startDate, new Long(1));
        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        long dayTime = 24 * 60 * 60 * 1000;
        long days = (endDateTime - startDateTime) / dayTime;
        return days;
    }/*from w ww.jav  a  2 s  . c om*/

    public static Date strToDate(String date) throws ParseException {

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");

        java.util.Date d = sdf.parse(date);

        return d;
    }

    public static Date getNextMonth(Date dt, Long n) {

        Calendar cal = new GregorianCalendar();
        cal.setTime(dt);

        Calendar firstCal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(),
                1);
        if (firstCal.getActualMaximum(Calendar.DAY_OF_MONTH) < cal.get(Calendar.DAY_OF_MONTH)) {
            return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(),
                    firstCal.getActualMaximum(Calendar.DAY_OF_MONTH)).getTime();
        } else {
            return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(),
                    cal.get(Calendar.DAY_OF_MONTH)).getTime();
        }

    }
}

Related

  1. getMonthFirstDate(final Date date, final String format)
  2. getMonthForDate(String dateToCheck, String pattern)
  3. getMonthForInt(int num)
  4. getMonthForString(String monthStr, Locale locale)
  5. getMonthInfo(int monthInfo)
  6. getMonthNo(Date date)
  7. getMonthNumber()
  8. getMonthOfSeason(Date date, boolean firstOrLast)
  9. getMonthPath(Date date)