JAVA中Calendar类,计算当前日期的前一天与上一月份的第一天

		//设置日期格式及获取当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		//获得日历   并把当前时间放入日历中
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//返回日期的前一天   从日历中获取出来    按格式输出
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		System.out.println(sdf.format(date));
-----------------------------------当前日期的前一天---------------------------------------
        //设置日期格式及获取当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		//获得日历   并把当前时间放入日历中
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//计算当前月份-1
		calendar.add(Calendar.MONTH, -1);
		//设时间为当前月份的第一天
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		date = calendar.getTime();
		System.out.println(sdf.format(date));
		String fy = sdf.format(date);
------------------------------上一月份的第一天---------------------------------------

 


版权声明:本文为qq_35714535原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。