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

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

编码文章call10242024-12-22 14:42:4624A+A-

//根据传过来的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);

}

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4