get Next Month Start - Java java.util

Java examples for java.util:Month

Description

get Next Month Start

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        int year = 2;
        int month = 2;
        System.out.println(getNextMonthStart(year, month));
    }//from  ww  w .jav  a  2s  . c  o  m

    public static Date getNextMonthStart(int year, int month) {
        Calendar temp = getMonthEndCalendar(year, month);
        temp.add(Calendar.DAY_OF_MONTH, 1);
        return temp.getTime();
    }

    private static Calendar getMonthEndCalendar(int year, int month) {
        Calendar result = Calendar.getInstance();
        result.setTime(getMonthStart(year, month));
        result.set(Calendar.DAY_OF_MONTH,
                result.getActualMaximum(Calendar.DAY_OF_MONTH));
        return result;
    }

    public static Date getMonthStart(int year, int month) {
        return parseDate(String.valueOf(year) + month, "yyyyM");
    }

    public static Date parseDate(String dateString, String format) {
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern(format);
        formater.setLenient(false);
        try {
            return formater.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related Tutorials