Java Month Convert monthsBetween(String from, String to)

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

Description

months Between method

License

Open Source License

Parameter

Parameter Description
from start date
to end date

Exception

Parameter Description
ParseException error info

Return

monthsBetween date value

Declaration

public static int monthsBetween(String from, String to) throws ParseException 

Method Source Code


//package com.java2s;

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

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

public class Main {
    /**//from   w  w w  .  j  av  a2s . c  o  m
     * months Between method
     * @param from start date
     * @param to end date
     * @return monthsBetween date value
     * @throws ParseException error info
     */
    public static int monthsBetween(String from, String to) throws ParseException {
        return monthsBetween(from, to, "yyyyMMdd");
    }

    /**
     * months Between method
     * @param from start date
     * @param to end date
     * @param format from/to date format
     * @return monthsBetween date value
     * @throws ParseException error info
     */
    public static int monthsBetween(String from, String to, String format) throws ParseException {
        Date fromDate = check(from, format);
        Date toDate = check(to, format);

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

        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.KOREA);
        SimpleDateFormat monthFormat = new SimpleDateFormat("MM", 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 + 1;
    }

    /**
     *
     * @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;
    }
}

Related

  1. monthConvertToNumber(String str)
  2. monthformMatDate(Date date)
  3. monthIndexAsString(Integer index, Integer offset)
  4. monthNumber(String s)
  5. monthOfDate(Date s)
  6. monthStringToInt(String month)
  7. nextMonth()
  8. nextMonth(Date date)
  9. nextMonth(Date date)