2

I am trying to do something very simple, take a Date and get the first day of the next month from it. I have tried using Calendar. Here date is of type Calendar.

Calendar nextMonthFirst = (Calendar)date.clone();

     int year = date.get(Calendar.YEAR);
     int month =  date.get(Calendar.MONTH);

     nextMonthFirst.clear();

     nextMonthFirst.set(year, month+1,1);
     nextMonthFirst.add(Calendar.DAY_OF_MONTH, 0);
     return nextMonthFirst 

When I use date as cal.set(2016,11,30); I get the output as 2017-01-01 instead of 2016-12-01. Any idea what I am doing wrong?

Mary
  • 1,505
  • 5
  • 27
  • 44
  • Month value is zero-based...so 11 means December when you do cal.set(2016,11,30) – dev8080 Mar 24 '17 at 12:28
  • 2
    IMO, this zero-base month is the worst thing in this date API. I recommend you to use another API: http://stackoverflow.com/a/589940/7605325 –  Mar 24 '17 at 12:33
  • If you can use Java SE 8, use the new LocalDate API. – dev8080 Mar 24 '17 at 12:34
  • Even then, it's working fine, except that where you set values, you must remember the zero-based rule. – dev8080 Mar 24 '17 at 12:46
  • Please search Stack Overflow before posting. Virtually every basic date-time question has already been asked and answered, often many times over. On the duplicate, read the answers about `TemporalAduster` interface, and use [`TemporalAdjusters.firstDayOfNextMonth`](https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html#firstDayOfNextMonth--) – Basil Bourque Mar 24 '17 at 20:23
  • Another duplicate: http://stackoverflow.com/q/4786169/642706 Pay attention to the java.time Answers rather than Joda-Time as java.time is the successor to Joda-Time. – Basil Bourque Mar 24 '17 at 20:29
  • Another duplicate: http://stackoverflow.com/q/10828398/642706 – Basil Bourque Mar 24 '17 at 20:32
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 07 '18 at 20:41

1 Answers1

5

Try the following, but use Java 8 and the java.time API instead!

import java.util.Calendar;

import static java.util.Calendar.*;
import static java.util.Calendar.DAY_OF_MONTH;

/**
 * Created by kedar on 3/24/17.
 */
public class FirstDayOfNext {
    public static void main(String[] args) {
        Calendar today = getInstance();
        today.clear();
        today.set(2016, NOVEMBER, 30);
        System.out.println("today date: " + today.getTime());
        Calendar next = getInstance();
        next.clear();
        next.set(YEAR, today.get(YEAR));
        next.set(MONTH, today.get(MONTH) + 1);
        next.set(DAY_OF_MONTH, 1); // optional, default: 1, our need
        System.out.println("next  date: " + next.getTime());
    }
}

It prints:

today date: Wed Nov 30 00:00:00 PST 2016
next  date: Thu Dec 01 00:00:00 PST 2016

as expected.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34