1

I have specific date and i want to find last day number(integer) of month. I am using following code but always return current of date.

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Date date = (Date) sdf.parse(year+"-"+(month<10?("0"+month):month)+"-01");


    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    Date dt = (Date) calendar.getTime(); -> dt is return current date always

example: my date = 2018/04/30 and i want to find 30.

I couldnt find answer at site.

Thnx

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ibrahim Ates
  • 375
  • 2
  • 5
  • 15
  • Possible duplicate of [How to get day of the month?](https://stackoverflow.com/questions/7226156/how-to-get-day-of-the-month) – TheSprinter May 31 '18 at 07:24
  • I cannot reproduce your problem. I get Mon Apr 30 00:00:00 CEST 2018. And `calendar.get(Calendar.DAY_OF_MONTH)` gives me 30. – Ole V.V. May 31 '18 at 09:23

5 Answers5

4

If using Java 8 (or higher), don't use Calendar. If using Java 6 or 7, you might want to consider using the ThreeTen Backport. In either case, use the Java Time API.


Using Java Time

Since input is int year and int month, use YearMonth.

To find last day number of month, call lengthOfMonth().

To get the date at the end of month, call atEndOfMonth().

Demo

int year = 2020;
int month = 2;

int lastDay = YearMonth.of(year, month).lengthOfMonth();
System.out.println(lastDay);

LocalDate date = YearMonth.of(year, month).atEndOfMonth();
System.out.println(date);

Output

29
2020-02-29

Using Joda-Time

If you don't have Java 8, and already use Joda-Time, do it this way:

Demo

int year = 2020;
int month = 2;

int lastDay = new LocalDate(year, month, 1).dayOfMonth().getMaximumValue();
System.out.println(lastDay);

LocalDate date = new LocalDate(year, month, 1).dayOfMonth().withMaximumValue();
System.out.println(date);

Output

29
2020-02-29

Using Calendar

If you insist on using Calendar, call getActualMaximum(Calendar.DAY_OF_MONTH) as also mentioned in other answers.

Since input is int year and int month, don't build and parse a string, just set Calendar fields directly. Note that "month" in Calendar is zero-based.

Demo

int year = 2020;
int month = 2;

Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, month - 1, 1);
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, lastDay);
Date date = calendar.getTime();

System.out.println(lastDay);
System.out.printf("%tF%n", date);

Output

29
2020-02-29
Andreas
  • 154,647
  • 11
  • 152
  • 247
3

have specific date and i want to find last day number(integer) of month

getActualMaximum() is what you are looking for here.

Calendar cal = Calendar.getInstance();
cal.setTime(parsedDate);
cal.getActualMaximum(Calendar.DAY_OF_MONTH);
jmj
  • 237,923
  • 42
  • 401
  • 438
1

You can use calendar for that, like this:

calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

Or, if you have joda, which is usually better:

DateTime d = new DateTame(dt);
d.dayOfMonth().getMaximumValue();
NiVeR
  • 9,644
  • 4
  • 30
  • 35
1
Calendar calendar =Calendar.getInstance();
calendar.set(Calendar.DATE, 1); 
calendar.roll(Calendar.DATE, -1); 
int lastDate=calendar.get(Calendar.DATE);
J.cui
  • 11
  • 2
1

First, how are you getting the year to be used?

it should be simple by using Java Time LocalDate:

import java.time.*;
import static java.time.Month.*;
import static java.time.temporal.TemporalAdjusters.*;
import static java.time.temporal.ChronoField.*;

int lastDay = LocalDate.now()   // or whatever date you want
                  .with(Month.of(yourMonth))
                  .with(lastDayOfMonth())
                  .get(DAY_OF_MONTH);

 // or, if you have year and month, and want to find the corresponding last day
int lastDay = LocalDate.of(yourYear, yourMonth, 1)
                  .with(lastDayOfMonth())
                  .get(DAY_OF_MONTH);
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • You should use `YearMonth` class. See [my answer](https://stackoverflow.com/a/50619041/5221149). – Andreas May 31 '18 at 07:37
  • true, YearMonth is actually better in such case (I was writing the solution with parsing an input date in mind... :P ) – Adrian Shum May 31 '18 at 07:39
  • 1
    I would still prefer this way over using `Calendar` any time. `Calendar` is long outdated and poorly designed. Agree that `YearMonth` is a further (minor) improvement. +1 to both of you. – Ole V.V. May 31 '18 at 09:28
  • 1
    @OleV.V. Same here. For any project I have control, I only consider using Java Time (Java8+) or Joda/310-backport (Java7 or earlier). Date is probably the only legacy class I will use. There is simply no reason to use Calendar – Adrian Shum May 31 '18 at 10:11