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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部