Java中的MonthDay format()方法
可以使用format()Java中MonthDay类中的方法,使用指定的格式化程序对MonthDay进行格式化。此方法需要单个参数,即要格式化的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));热门推荐
10 亲戚送行祝福语简短精辟
11 春节送祝福语简短的
12 孩子满月随礼简短祝福语
13 高中升学宴祝福语简短
14 年底拜年祝福语大全简短
15 老板生日祝福语简短语
16 给儿子祝福语简短霸气
17 毕业结婚祝福语简短精辟
18 2026新年祝福语简短语