Java Month Get getMonthCount(String from, String to)

Here you can find the source of getMonthCount(String from, String to)

Description

get Month Count

License

Open Source License

Declaration

public static int getMonthCount(String from, String to) throws Exception 

Method Source Code


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

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

import java.util.Date;

import java.util.Locale;

public class Main {
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static int getMonthCount(String from, String to) throws Exception {
        return getMonthCount(from, to, DATE_FORMAT.toPattern());
    }/*w  ww . j  a va2 s . c o m*/

    public static int getMonthCount(String from, String to, String format) throws Exception {
        Date fromDate = dateFormatCheck(from, format);
        Date toDate = dateFormatCheck(to, format);

        // if two date are same, return 0.
        if (fromDate.compareTo(toDate) == 0)
            return 0;

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

        int fromYear = Integer.parseInt(yearFormat.format(fromDate));
        int toYear = Integer.parseInt(yearFormat.format(toDate));
        int fromMonth = Integer.parseInt(monthFormat.format(fromDate));
        int toMonth = Integer.parseInt(monthFormat.format(toDate));

        int result = 0;
        result += ((toYear - fromYear) * 12);
        result += (toMonth - fromMonth);

        return result;
    }

    public static Date dateFormatCheck(String source, String format) throws ParseException {

        if (source == 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(source);
        } catch (ParseException e) {
            throw new ParseException(" wrong date:\"" + source + "\" with format \"" + format + "\"", 0);
        }

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

        return date;
    }

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

    public static Date parse(String datetime) {
        try {
            return FORMAT.parse(datetime);
        } catch (ParseException e) {
            return new Date();
        }
    }
}

Related

  1. getMonthAndDate()
  2. getMonthBefore(Date d, int number)
  3. getMonthBegin(String strdate)
  4. getMonthBegin(String strdate)
  5. getMonthByNow(int month)
  6. getMonthDesc(Date month)
  7. getMonthEnd(Date date)
  8. getMonthEnd(String strdate)
  9. getMonthEnd(String strdate)