MonthDay 格式化()方法在 Java 中
MonthDay 可以在 Java 中 MonthDay 类的 format() 方法使用特定的格式化器进行格式化。此方法需要一个参数,即要格式化的 MonthDay 对象,它返回使用特定格式化器格式化的 MonthDay。
以下给出了演示此方法的程序
示例
import java.time.*;
import java.time.temporal.*;
import java.time.format.DateTimeFormatter;
public class Demo {
public static void main(String[] args) {
MonthDay md = MonthDay.parse("--02-22");
LocalDate ld = md.atYear(2019);
System.out.println("The MonthDay is: " + md);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
System.out.println("The formatted MonthDay is: " + dtf.format(ld));
}
}输出
The MonthDay is: --02-22 The formatted MonthDay is: 2019-02-22
现在让我们了解一下上述程序。
首先显示 MonthDay。然后使用 format() 方法使用特定的格式化器对 MonthDay 进行格式化,并显示格式化的 MonthDay。以下代码片段演示了此操作
MonthDay md = MonthDay.parse("--02-22");
LocalDate ld = md.atYear(2019);
System.out.println("The MonthDay is: " + md);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
System.out.println("The formatted MonthDay is: " + dtf.format(ld));
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP