Reputation: 385
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
Upvotes: 1
Views: 524
Reputation: 159270
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.
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
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
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
Upvotes: 4
Reputation: 40076
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);
Upvotes: 1
Reputation: 9806
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();
Upvotes: 1
Reputation: 11
Calendar calendar =Calendar.getInstance();
calendar.set(Calendar.DATE, 1);
calendar.roll(Calendar.DATE, -1);
int lastDate=calendar.get(Calendar.DATE);
Upvotes: 1
Reputation: 240996
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);
Upvotes: 3