java获取当前时间和前一天日期(实现代码)
时间:2023-09-17 07:48:35
废话不多说,上代码
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);
标签:java,日期
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现上传网络图片到微信临时素材
2022-09-21 08:43:23
一文搞懂Java中的序列化与反序列化
2021-11-22 00:26:45
![](https://img.aspxhome.com/file/2023/3/73063_0s.jpg)
eclipse的web项目实现Javaweb购物车的方法
2023-07-28 15:55:53
![](https://img.aspxhome.com/file/2023/2/64152_0s.png)
Winform控件Picture实现图片拖拽显示效果
2022-04-01 05:49:54
![](https://img.aspxhome.com/file/2023/3/119543_0s.png)
详解Java变量与常量
2023-11-09 21:00:14
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
C#设计模式之Builder生成器模式解决带老婆配置电脑问题实例
2021-07-09 03:22:59
![](https://img.aspxhome.com/file/2023/2/119402_0s.png)
Java 深入探讨设计模式之原型模式篇
2023-11-16 17:37:59
![](https://img.aspxhome.com/file/2023/3/64553_0s.png)
Android UI控件ExpandableListView基本用法详解
2021-12-26 22:20:50
![](https://img.aspxhome.com/file/2023/8/137548_0s.jpg)
深入Android HandlerThread 使用及其源码完全解析
2023-11-30 19:16:56
![](https://img.aspxhome.com/file/2023/4/138614_0s.gif)
基于java math API 的详细解释说明
2021-10-04 06:51:44
如何关闭 IDEA 自动更新
2021-11-04 17:17:30
![](https://img.aspxhome.com/file/2023/7/62107_0s.png)
Java读取.properties配置文件的几种方式
2021-07-07 05:56:03
Spring Cache框架应用介绍
2023-06-15 22:32:59
![](https://img.aspxhome.com/file/2023/2/57492_0s.png)
C#中实现插入、删除Excel分页符的方法
2022-05-27 02:51:16
![](https://img.aspxhome.com/file/2023/9/100129_0s.png)
C#实现获取鼠标句柄的方法
2022-05-06 21:13:41
![](https://img.aspxhome.com/file/2023/0/108750_0s.jpg)
C#书写规范
2023-07-09 09:15:57
Flutter web bridge 通信总结分析详解
2022-05-23 05:15:42
![](https://img.aspxhome.com/file/2023/3/138493_0s.png)
Mybatis日志参数快速替换占位符工具的详细步骤
2023-09-30 21:29:36
SpringMVC上传图片与访问
2023-12-06 17:54:31
c++ 虚继承,多继承相关总结
2022-11-19 12:09:35
![](https://img.aspxhome.com/file/2023/0/120810_0s.png)