java获取当月第一天时间、当月最后一天时间、上月第一天时间、上月最后一天时间
import java.text.SimpleDateFormat; import java.util.Calendar; public class test { public static void main(String[] args) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 本月起始 Calendar thisMonthFirstDateCal = Calendar.getInstance(); thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00"; System.out.println("本月起始:" + thisMonthFirstTime); // 本月末尾 Calendar thisMonthEndDateCal = Calendar.getInstance(); thisMonthEndDateCal.set(Calendar.DAY_OF_MONTH, thisMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); String thisMonthEndTime = format.format(thisMonthEndDateCal.getTime()) + " 23:59:59"; System.out.println("本月末尾:" + thisMonthEndTime); // 上月起始 Calendar lastMonthFirstDateCal = Calendar.getInstance(); lastMonthFirstDateCal.add(Calendar.MONTH,-1); lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); String lastMonthFirstTime = format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00"; System.out.println("上月起始:" + lastMonthFirstTime); // 上月末尾 Calendar lastMonthEndDateCal = Calendar.getInstance(); lastMonthEndDateCal.add(Calendar.MONTH,-1); lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); String lastMonthEndTime = format.format(lastMonthEndDateCal.getTime()) + " 23:59:59"; System.out.println("上月末尾:" + lastMonthEndTime); } }