前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站 点击跳转浏览。
今天做一个需求是
实现当前事件减去100天并且格式化为下图所示
实现代码如下:
String time ="";if(StringUtils.isNotEmpty(vo.getStartTime())) {String startTime = vo.getStartTime();time =startTime;}else{Date date = new Date();SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DAY_OF_MONTH, -100);time =dateFormat.format(calendar.getTime());}
故而在此总结一下
代码如下:
void contextLoads() {Date date = new Date();//获取当前时间System.out.println(date);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天,即一天前的时间System.out.println(simpleDateFormat.format(calendar.getTime()));Calendar calendar2 = Calendar.getInstance();calendar2.add(Calendar.MONTH, -1);//当前时间减去一个月,即一个月前的时间System.out.println(simpleDateFormat.format(calendar2.getTime()));Calendar calendar3 = Calendar.getInstance();calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间System.out.println(simpleDateFormat.format(calendar3.getTime()));System.out.println(calendar.getTimeInMillis());//返回当前时间的毫秒数}
显示如下