Not much to say, look at the example:
Package test;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
public class Testcalendar {
public static void Main (string[] args) {
Testcalendar TC = new Testcalendar ();
Date date = new Date ();
Date = Tc.lastdayofmonth3 (date);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
System.out.println (Sdf.format (date));
}
The first of these methods
Public date lastdayofmonth (date date) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Calendar.set (Calendar.day_of_month, 1);/Set the current time to a monthly number
The number of days on the current calendar-1 becomes the maximum, and this method does not change fields other than the specified field
Calendar.roll (Calendar.day_of_month,-1);
return Calendar.gettime ();
}
The second method
Public date lastDayOfMonth2 (date date) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Returns the maximum value that a specified calendar field might poke
int value = Calendar.getactualmaximum (Calendar.day_of_month);
Calendar.set (calendar.day_of_month, value);
return Calendar.gettime ();
}
The third method
Public date lastDayOfMonth3 (date date) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Calendar.set (Calendar.day_of_month, 1);//Set as the first day of the month
Calendar.add (Calendar.month, 1);//month plus one
Calendar.add (Calendar.date,-1);/days plus-1 = last day of one months
return Calendar.gettime ();
}
Fourth method
Public date LastDayOfMonth4 (date date) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Cycle to the current time plus one day until the current number is added to the first day of the next month
do {
Calendar.add (calendar.date, 1);
while (Calendar.get (calendar.date)!= 1);
Minus a day to get the last day of last month
Calendar.add (Calendar.date,-1);
return Calendar.gettime ();
}
This article refers to: http://kin111.blog.51cto.com/738881/221672