您的位置 首页 java

java中常用到的获取格式化时间字符串

//根据传过来的date参数得到制定格式的时间 字符串

public static String getCurrentDateTime3(Date date){

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

return sdf.format(date);

}

//通过date对象得到n天后的格式化时间字符串

public static String getCurrentDateTime4(Date date,int day){//日期加上天数得到后来的日期

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

long time=date.getTime();

day=day*24*60*60*1000;

time+=day;

Date newDate=new Date(time);

return sdf.format(newDate);

}

//通过date对象得到n天前的格式化时间字符串

public static String getCurrentDateTime5(Date date,int day){//日期减去天数得到后来的日期

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

long time=date.getTime();

day=day*24*60*60*1000;

time-=day;

Date newDate=new Date(time);

return sdf.format(newDate);

}

文章来源:智云一二三科技

文章标题:java中常用到的获取格式化时间字符串

文章地址:https://www.zhihuclub.com/200187.shtml

关于作者: 智云科技

热门文章

网站地图