month Range - Java java.util

Java examples for java.util:Month

Description

month Range

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

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

    public static Integer monthRange(String begin, String end) {
        Date start = null;//from w  w  w  .ja v  a 2 s . c  o m
        Date stop = null;
        try {
            start = sdf.parse(begin);
            stop = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        int rangeyear = stop.getYear() - start.getYear();
        int rangemonth = stop.getMonth() - start.getMonth();

        Integer result = null;
        if (rangeyear > 0) {
            result = rangeyear * 12 + rangemonth;
        } else {
            result = rangemonth;
        }
        return result;
    }
}

Related Tutorials