Gets the first day of the last day of this month the first day of the last day of the previous month

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Test3 {
    //测试
    public static void main(String[] args) {
        Test3 test3=new Test3();
        test3.preMonth();
        test3.thisMonth();
    }

    //代码

    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    /**
     * calendar输出结果如下:
     [id="Asia/Shanghai",
     offset=28800000,
     YEAR=2019,
     MONTH=8,
     WEEK_OF_YEAR=38,
     WEEK_OF_MONTH=3,
     DAY_OF_MONTH=19,
     DAY_OF_YEAR=262,
     =. 5 DAY_OF_WEEK, 
        // convert their desired date format
     =. 3 DAY_OF_WEEK_IN_MONTH, 
     the HOUR_OF_DAY =. 9, 14 = MINUTE, SECOND, = 42 is, for MILLISECOND = 308, ZONE_OFFSET = 28.8 million] 
     * / 
    public void preMonth () { 
        // Get the first day of the previous month 
        calendar.add (Calendar.MONTH, -1); 
        calendar.set (Calendar.DAY_OF_MONTH,. 1); 
        String fistDay = formatter.format (calendar.getTime ()); 
        System.out.println ( "the first day of the month:" + fistDay); 

        / / get one month before the last day 
        // get the current date: May day 
        Calendar Calendar1 = Calendar.getInstance (); 
        // get the month the day before, that is the last day of last month. 
        calendar1.set (Calendar.DAY_OF_MONTH, 0 ); // set to 1, the current date is the first day of the month both
        Formatter.format lastDay = String (calendar1.getTime ()); 
        System.out.println ( "the last day of the month:" + lastDay); 
    } 

    public void thisMonth () { 
        // Get the first day of the month 
        Calendar = Calendar.getInstance calendar0 (); 
        calendar0.set (Calendar.DAY_OF_MONTH,. 1); 
        String firstDay = formatter.format (calendar0.getTime ()); 
        System.out.println ( "the first day of the month:" + firstDay ); 

        // this month, the last day of a method: 
        Calendar Calendar1 = Calendar.getInstance (); 
        calendar1.set (Calendar.DAY_OF_MONTH, calendar1.getActualMaximum (Calendar.DAY_OF_MONTH)); 
        String lastDay1 = formatter.format (calendar1.getTime ( )); 
        System.out.println ( "the last day of the current month" + lastDay1);

        // the last day of the month Method two:
        Calendar2 = Calendar.getInstance Calendar (); 
        calendar2.add (the Calendar.MONTH, +. 1); 
        calendar2.set (Calendar.DAY_OF_MONTH, 0); 
        String lastDay2 = formatter.format (calendar2.getTime ()); 
        the System.out. println ( "the last day of the current month" + lastDay2); 
    } 
    
}

  

Guess you like

Origin www.cnblogs.com/taoist123/p/11547163.html