Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the dates in a month using calender class?

Here I want to display dates like

2013-01-01,
2013-01-02,
2013-01-03,
.
.
...etc

I can get total days in a month

private int getDaysInMonth(int month, int year) {
  Calendar cal = Calendar.getInstance();  // or pick another time zone if necessary
  cal.set(Calendar.MONTH, month);
  cal.set(Calendar.DAY_OF_MONTH, 1);      // 1st day of month
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.HOUR, 0);
  cal.set(Calendar.MINUTE, 0);
  Date startDate = cal.getTime();

  int nextMonth = (month == Calendar.DECEMBER) ? Calendar.JANUARY : month + 1;
  cal.set(Calendar.MONTH, nextMonth);
  if (month == Calendar.DECEMBER) {
     cal.set(Calendar.YEAR, year + 1);
  }
  Date endDate = cal.getTime();

  // get the number of days by measuring the time between the first of this
  //   month, and the first of next month
  return (int)((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
}

Does anyone have an idea to help me?

like image 823
Android_dev Avatar asked Nov 20 '13 07:11

Android_dev


People also ask

How to get a list of all dates in a month?

You should set both the day and the month. In your code above, you're already doing it: mStart.set (Calendar.DAY_OF_MONTH, 1); - have you removed this? To get all the dates of a particular month, set the Calendar to a date in that month, e.g. the 1th, ask the Calendar for the number of dates in that month, then get the dates.

What is calendar class in Java?

Last Updated : 28 Aug, 2018 Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces.

How do I add months to the calendar?

you are starting with today's date, so assuming you want months starting from there simply use: cal.add (Calendar.MONTH, -1) to move to last month.

How do I add/subtract values in the calendar class?

:) The Calendar class uses the add () method to add and subtract values. You pass in the field you want to change, and a number (exactly how much you want to add/subtract from the current value). For example, let's get a date that is 2 months before the date we created:


2 Answers

If you only want to get the max number of days in a month you can do the following.

// Set day to one, add 1 month and subtract a day
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1); 
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.get(Calendar.DAY_OF_MONTH);

If you actually want to print every day then you can just set the day of month to 1 and keep adding a day in a loop until the month changes.

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1); 
int myMonth=cal.get(Calendar.MONTH);

while (myMonth==cal.get(Calendar.MONTH)) {
  System.out.print(cal.getTime());
  cal.add(Calendar.DAY_OF_MONTH, 1);
}
like image 155
Jean B Avatar answered Nov 03 '22 21:11

Jean B


Modern answer: Don’t use Calendar. Use java.time, the modern Java date and time API.

YearMonth ym = YearMonth.of(2013, Month.JANUARY);
LocalDate firstOfMonth = ym.atDay(1);
LocalDate firstOfFollowingMonth = ym.plusMonths(1).atDay(1);
firstOfMonth.datesUntil(firstOfFollowingMonth).forEach(System.out::println);

Output (abbreviated):

2013-01-01
2013-01-02
2013-01-03
…
2013-01-30
2013-01-31

datesUntil gives us a stream of dates until the specified end date exclusive, so when we give it the 1st of the following month, we get exactly all the dates of the month in question. In this example case up to and including January 31.

Link: Oracle tutorial: Date Time explaining how to use java.time.

like image 39
Ole V.V. Avatar answered Nov 03 '22 19:11

Ole V.V.