加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java获取当前时间和前一天日期(实现代码)

发布时间:2020-12-14 14:15:44 所属栏目:Java 来源:网络整理
导读:废话不多说,上代码 复制代码 代码如下: String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; Date dNow = new Date(); //当前时间 Date dBefore = new Date(); Calendar calendar = Calendar.getI

废话不多说,上代码

复制代码 代码如下:

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

Date dNow = new Date();   //当前时间
Date dBefore = new Date();

Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.DAY_OF_MONTH,-1);  //设置为前一天
dBefore = calendar.getTime();   //得到前一天的时间


SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
String defaultStartDate = sdf.format(dBefore);    //格式化前一天
String defaultEndDate = sdf.format(dNow); //格式化当前时间


System.out.println("前一天的时间是:" + defaultStartDate);

System.out.println("生成的时间是:" + defaultEndDate); 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读