public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
String time1 = df.format(time);
System.out.println("获取昨日日期------"+time1);
Date date = new Date();
Date da = new Date(date.getYear(),date.getMonth(),01);
String startTime=String.valueOf(df.format(da));
System.out.println("获取当前月第一天------"+startTime);
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.roll(Calendar.DAY_OF_MONTH, -1);
String time3 = df.format(cal.getTime());
System.out.println("获取本月最后一天-----"+time3);
cal.setTime(date);
cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3);
cal.set(Calendar.DAY_OF_MONTH, 1);
String time4 = df.format(cal.getTime());
System.out.println("获取本季度第一天----"+time4);
cal.setTime(date);
//计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第三个月份只需 月份 / 3 * 3 + 2
cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3 + 2);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String time5 = df.format(cal.getTime());
System.out.println("获取本季度的最后一天----"+time5);
cal.setTime(date);
cal.set(Calendar.DAY_OF_YEAR, 1);
String time6 = df.format(cal.getTime());
System.out.println("获取本年的第一天----"+time6);
cal.setTime(date);
cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
String time7 = df.format(cal.getTime());
System.out.println("获取本年最后一天-----"+time7);
}
java获取当前日,当前月的开始时间和结束时间,当前季度的开始时间和结束时间
最新推荐文章于 2024-07-13 03:21:10 发布