当前位置:网站首页>Get the first and last days of the current month, and the first and last days of the previous month

Get the first and last days of the current month, and the first and last days of the previous month

2022-06-26 06:24:00 It's very quiet

The first and last day of last month
 

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

public class TestController {
    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // Get the first day of the current month 
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH,-1);
        c.set(Calendar.DAY_OF_MONTH,1);
        String dataStart = df.format(c.getTime());
        // Get the last day of the month 
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.MONTH,-1);
        c1.set(Calendar.DAY_OF_MONTH, c1.getActualMaximum(Calendar.DAY_OF_MONTH));
        String dataEnd = df.format(c1.getTime());
    }
}

First and last day of the month :
 

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

public class TestController {
    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // Get the first day of the current month 
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH,1);
        String dataStart = df.format(c.getTime());
        // Get the last day of the month 
        Calendar c1 = Calendar.getInstance();
        c1.set(Calendar.DAY_OF_MONTH, c1.getActualMaximum(Calendar.DAY_OF_MONTH));
        String dataEnd = df.format(c1.getTime());
    }
}

原网站

版权声明
本文为[It's very quiet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260605279278.html