Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java get last day in next month

Tags:

java

I have variable nowDate type of Date and I want set variable nextDate contains last day in next month.

For example: nowDate = 2013-04-16

So nextDate will contains 2013-05-31

How can I do that?

like image 550
faszynski Avatar asked Apr 04 '13 09:04

faszynski


1 Answers

Try

private static Date getNextDate(Date nowDate) {
    Calendar c = Calendar.getInstance();
    c.setTime(nowDate);
    c.add(Calendar.MONTH, 1);
    c.set(Calendar.DATE, c.getMaximum(Calendar.DATE));
    Date nextDate = c.getTime();
    return nextDate;
}

Usage:

    Date nowDate = new Date();
    Date nextDate = getNextDate(nowDate);
like image 114
Arun P Johny Avatar answered Sep 26 '22 14:09

Arun P Johny