Java uses calendar to get the last day of the month

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.