java format用法_Java YearMonth format()用法及代码示例
Java中的YearMonth类的format()方法用于根据作为参数传递给此方法的year-month的指定DateTimeFormatter来格式化YearMonth实例。
用法:
public String format(DateTimeFormatter formatter)
参数:此方法接受单个参数格式程序,该格式程序是DateTimeFormatter,根据该格式将格式化Yearyear实例。
返回值:根据指定的格式化程序格式化后,它将Yearyear的值作为字符串返回。
以下程序说明了Java中YearMonth的format()方法:
示例1::
// Program to illustrate the format() method
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GfG {
public static void main(String[] args)
{
// Create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2017, 8);
// Create a DateTimeFormatter string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yy/MM”);
// Format this year-month
System.out.println(thisYearMonth.format(formatter));
}
}
输出:
17/08
示例2::
// Program to illustrate the format() method
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GfG {
public static void main(String[] args)
{
// Create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2018, 5);
// Create a DateTimeFormatter string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“MM/yy”);
// Format this year-month
System.out.println(thisYearMonth.format(formatter));
}
}
输出:
05/18
还没有评论,来说两句吧...