共計 544 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 DateTimeFormatter 類來將 LocalDateTime 對象轉換為字符串。下面是一個示例代碼:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {public static void main(String[] args) {LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println("Formatted LocalDateTime: " + formattedDateTime);
}
}
在上面的代碼中,我們首先創建了一個 LocalDateTime 對象表示當前日期和時間。然后使用 DateTimeFormatter 類創建一個格式化模板,最后調用 LocalDateTime 對象的 format 方法將其轉換為字符串并打印輸出。
丸趣 TV 網 – 提供最優質的資源集合!
正文完
發表至: Java
2024-02-27