本文主要是介绍获取当前时间的前一天和后一天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package jp.co.hitachi_solutions.regasas.common;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** DateUtil*/
public class DateUtil {/*** 当前日期的前一天* @param specifiedDay* @return* @throws Exception*/public static String getSpecifiedDayBefore(String specifiedDay){//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");Calendar c = Calendar.getInstance();Date date=null;try {date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);} catch (ParseException e) {e.printStackTrace();}c.setTime(date);int day=c.get(Calendar.DATE);c.set(Calendar.DATE,day-1);String dayBefore=new SimpleDateFormat("yyyyMMdd").format(c.getTime());return dayBefore;}/*** 当前日期的后一天* @param specifiedDay* @return*/public static String getSpecifiedDayAfter(String specifiedDay){Calendar c = Calendar.getInstance();Date date=null;try {date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);} catch (ParseException e) {e.printStackTrace();}c.setTime(date);int day=c.get(Calendar.DATE);c.set(Calendar.DATE,day+1);String dayAfter=new SimpleDateFormat("yyyyMMdd").format(c.getTime());return dayAfter;}
}
这篇关于获取当前时间的前一天和后一天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!