LocalDate 转为移除-的字符串
时间: 2023-11-16 13:32:39 浏览: 41
你可以使用 LocalDate 类的 format 方法结合 String 类的 replaceAll 方法将其转换为不带 `-` 的字符串,示例代码如下:
```java
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = date.format(formatter);
String removedHyphens = formattedDate.replaceAll("-", "");
System.out.println(removedHyphens); // 输出类似于 20221111 的字符串
```
其中,`replaceAll` 方法将字符串中所有匹配的字符替换为指定的字符串,这里使用空字符串 `""` 代表删除。
相关问题
LocalDate转为指定格式字符串
以下是将LocalDate转为指定格式字符串的例子:
```java
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.now();
String str = ld.format(fmt);
System.out.println(str); // 输出:2022-08-31
```
localdate转为yyyy-MM-dd
可以使用Java中的SimpleDateFormat类将LocalDate对象转换为指定格式的字符串,具体代码如下:
```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Example {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = localDate.format(formatter);
System.out.println(formattedDate);
}
}
```
输出结果为当前日期的yyyy-MM-dd格式字符串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)