Calendar date method

Calendar date method

The interview actually allowed me to get the first and last day of the current month, mainly because of embarrassing answers.

Don't talk nonsense, post the code directly, the work should be enough

public class TestCalendar {
   //This is the date
    public static void main(String[] args) {
       //Get the current year, month, and date
        Calendar cale = null;
        cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        int day = cale.get(Calendar.DATE);
        int hour = cale.get(Calendar.HOUR_OF_DAY);
        int minute = cale.get(Calendar.MINUTE);
        int second = cale.get(Calendar.SECOND);
        int dow = cale.get(Calendar.DAY_OF_WEEK);
        int dom = cale.get(Calendar.DAY_OF_MONTH);
        int doy = cale.get(Calendar.DAY_OF_YEAR);

        System.out.println("Current Date: "+ cale.getTime());
        System.out.println("Year: "+ year);
        System.out.println("Month: "+ month);
        System.out.println("Day: "+ day);
        System.out.println("Hour: "+ hour);
        System.out.println("Minute: "+ minute);
        System.out.println("Second: "+ second);
        System.out.println("Day of Week: "+ dow);
        System.out.println("Day of Month: "+ dom);
        System.out.println("Day of Year: "+ doy);

       //Get the first and last day of the month
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday, lastday;
       //Get the first day of the previous month
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cale.getTime();
        firstday = format.format(cale.getTime());
       //Get the last day of the previous month
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());
        Date time2 = cale.getTime();
        System.out.println(time);
        System.out.println(time2);
        System.out.println("The first and last days of the month are: "+ firstday +" and "+ lastday);

       //Get the current date string
        Date d = new Date();
        System.out.println("Current date string 1:" + format.format(d));
        System.out.println("Current date string 2:" + year + "/" + month + "/" + day + "" + hour + ":" + minute + ":" + second);
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(format2.format(d));
    }
}

In place! ! ! All belong to the basics, just remember it.

Reference: https://cloud.tencent.com/developer/article/1335831 Calendar Date Method-Cloud + Community-Tencent Cloud