java 如何获取当前年月并转换为字符串格式
时间: 2023-08-07 10:01:59 浏览: 65
可以使用Java中的SimpleDateFormat类来获取当前年月并转换为字符串格式,具体代码如下:
```
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetCurrentYearMonth {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String currentYearMonth = sdf.format(date);
System.out.println("当前年月:" + currentYearMonth);
}
}
```
通过Date类获取当前时间,然后通过SimpleDateFormat类将时间对象格式化为指定格式的字符串。其中,"yyyy-MM"表示年月的格式,"-"为分隔符。
相关问题
java获取当前时间年月日然后转为字符串
可以使用Java的日期时间类库来获取当前时间的年月日,并将其转换为字符串,示例代码如下:
```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = currentDate.format(formatter);
System.out.println("当前时间的年月日为:" + formattedDate);
}
}
```
解释一下代码:
1. `LocalDate.now()`获取当前时间的日期。
2. `DateTimeFormatter.ofPattern("yyyy-MM-dd")`创建一个日期格式化对象,指定日期格式为“年-月-日”。
3. `currentDate.format(formatter)`将日期对象格式化为指定格式的字符串。
4. 最后将格式化后的字符串打印出来。
使用Java将英文字符串日期转换为中文字符串年月日
可以使用Java中的SimpleDateFormat类和Calendar类来实现英文字符串日期转换为中文字符串年月日。具体代码如下:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class EnglishToChineseDateConverter {
public static void main(String[] args) throws Exception {
String dateString = "2022-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = sdf.parse(dateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
String chineseDate = year + "年" + month + "月" + day + "日";
System.out.println("中文日期:" + chineseDate);
}
}
```
在这个例子中,我们首先定义了一个字符串类型的日期`dateString`,它表示英文格式的日期。然后,我们使用`SimpleDateFormat`类将其解析为`Date`类型的日期对象。
接着,我们创建了一个`Calendar`对象,并将其设置为解析后的日期对象。通过`Calendar`对象,我们可以获取年、月、日等日期信息,并将其转换为字符串类型的中文日期。
最后,我们将年、月、日拼接成中文日期字符串,并将其打印到控制台上。
需要注意的是,在`SimpleDateFormat`构造函数中,我们使用了`Locale.ENGLISH`参数来指定解析的日期格式为英文。如果想要解析其他语言的日期,可以将`Locale.ENGLISH`替换为相应的语言代码。
相关推荐
![js](https://img-home.csdnimg.cn/images/20210720083646.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)