如何“%tB”格式化程序工作? [英] How does "%tB" formatter work?
问题描述
System.out.format( %TB,12);
我应该得到一个十二月,但是我得到一个很好的例外
$ p $
线程中的异常mainjava.util.IllegalFormatConversionException:b!= java.lang.Integer
这意味着我使用的语法是错误的。
我无法找到解释%tB
格式命令的任何参考。
有人帮忙澄清这个问题吗?
在此先感谢。
从 Formatter
文档:
日期/时间 - 可以应用于能够编码
a日期或时间的Java类型:long
,Long
,日历
和日期
p>
您可以使用一个长整数(如 12L
)来排除异常。但请注意,格式化程序期待整数表示日期(例如精度为毫秒的Unix时间戳)。
为了得到你可以尝试在1970年中的一个月中手动建立一个近似的时间戳:
int month = 12;
int millisecondsInDay = 24 * 60 * 60 * 1000;
long date =((month - 1L)* 30 + 15)* millisecondsInDay;
System.out.format(%tB,date);
或者简单地使用 Date
对象:
System.out.format(%tB,new Date(0,12,0));
另请注意,不需要 Formatter
:
java.text.DateFormatSymbols.getInstance()。getMonths()[12 -1];
请参阅 DateFormatSymbols
了解更多信息。
System.out.format("%tB",12);
I should get a "December" out of it, however i get a nice exception
Exception in thread "main" java.util.IllegalFormatConversionException: b != java.lang.Integer
It means the syntax I used is wrong.
I cannot find any reference on line explaining the %tB
formatting command.
Is anybody help to clarify the matter?
Thanks in advance.
From the Formatter
documentation:
Date/Time - may be applied to Java types which are capable of encoding a date or time:
long
,Long
,Calendar
, andDate
.
You can get rid of the exception by using a long integer such as 12L
. But note that the formatter is expecting an integer representation of a date (i.e. a Unix timestamp with millisecond precision).
In order to get what you want, you can try to manually build an approximate timestamp in a middle of a month in 1970 :
int month = 12;
int millisecondsInDay = 24*60*60*1000;
long date = ((month - 1L)*30 + 15)*millisecondsInDay;
System.out.format("%tB", date);
Or simply use a Date
object :
System.out.format("%tB", new Date(0, 12, 0));
Also note that you can do the same thing without Formatter
:
java.text.DateFormatSymbols.getInstance().getMonths()[12-1];
See DateFormatSymbols
for more information.
这篇关于如何“%tB”格式化程序工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!