用java输出昨天的年月日时分秒
时间: 2024-02-01 12:13:06 浏览: 24
可以使用Java 8中的`java.time`包来实现。
以下是一个示例代码:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class YesterdayDateTime {
public static void main(String[] args) {
// 获取当前时间
LocalDateTime now = LocalDateTime.now();
// 减去一天
LocalDateTime yesterday = now.minusDays(1);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String formattedDateTime = yesterday.format(formatter);
System.out.println("昨天的日期时间为:" + formattedDateTime);
}
}
```
输出结果类似于:
```
昨天的日期时间为:2022年01月17日 11:22:33
```
相关问题
java 分别以年月日时分秒毫秒输出时间
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
int year = date.getYear() + 1900; // 获取年份并加上1900
int month = date.getMonth() + 1; // 获取月份并加上1
int day = date.getDate(); // 获取日
int hour = date.getHours(); // 获取小时
int minute = date.getMinutes(); // 获取分钟
int second = date.getSeconds(); // 获取秒
long millisecond = date.getTime() % 1000; // 获取毫秒
System.out.printf("%d年%d月%d日 %d时%d分%d秒%d毫秒", year, month, day, hour, minute, second, millisecond);
}
}
输出结果:2021年10月10日 15时24分37秒419毫秒
用JAVA判断年月日时分秒格式的正则
表达式是什么?
你好,以下是用JAVA判断年月日时分秒格式的正则表达式:
String regex = "^(\\d{4})-(\\d{2})-(\\d{2})\\s(\\d{2}):(\\d{2}):(\\d{2})$";
这个正则表达式可以匹配形如“2021-06-29 14:30:40”的日期时间格式。如果你还有其他问题,欢迎继续提出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)