java 输出 时间 年月日
时间: 2023-09-26 09:09:28 浏览: 52
您可以使用Java中的SimpleDateFormat类来输出时间中的年月日。以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 创建SimpleDateFormat对象,定义输出格式为年月日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
// 创建Date对象,获取当前时间
Date date = new Date();
// 使用SimpleDateFormat对象将时间格式化为字符串
String formatDate = sdf.format(date);
// 输出时间字符串
System.out.println(formatDate);
}
}
```
执行以上代码,将会输出类似于 "2021年07月29日" 的时间字符串。
相关问题
java输出不同格式年月日
好的,您想了解Java中不同格式的日期输出方法。Java中可以使用SimpleDateFormat类来格式化日期。下面是几个常用的格式化字符串:
1. "yyyy-MM-dd":输出格式为"年-月-日"
2. "yyyy/MM/dd":输出格式为"年/月/日"
3. "yyyy年MM月dd日":输出格式为"年月日"
以下是Java代码示例:
```
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = new Date(); //获取当前时间
//将日期格式化为"年-月-日"格式
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf1.format(date));
//将日期格式化为"年/月/日"格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(date));
//将日期格式化为"年月日"格式
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sdf3.format(date));
}
}
```
输出结果:
```
2022-08-08
2022/08/08
2022年08月08日
```
希望能解决您的问题!
java 获取当前时间年月日
以下是两种Java获取当前时间年月日的例子:
1.使用SimpleDateFormat类获取当前时间年月日
```java
import java.text.SimpleDateFormat;
import java.util.Date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());System.out.println(currentDate); // 输出:2022-08-01
```
2.使用Calendar类获取当前时间年月日
```java
import java.util.Calendar;
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "-" + month + "-" + day); // 输出:2022-8-1
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)