gpt4 book ai didi

java - 无法将 int 月份转换为月份名称 android java

转载 作者:行者123 更新时间:2023-12-01 07:53:17 25 4
gpt4 key购买 nike

我正在尝试将 int 月份转换为月份名称(字符串)。我使用简单的日期格式,但所有月份我只得到“Jan”。为什么会发生这种情况?

 public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

String sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(monthOfYear);

String date = +dayOfMonth+" "+(sdf);
dateTextView.setText(date);

最佳答案

I get only "Jan" for all months

因为 String sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(monthOfYear); 这里,monthOfYear 是 Number 对象,所以,DateFormate 类会将其转换为该数字的 Date 对象,该数字介于 1970 年 1 月 1 日1970 年 1 月 12 日 之间> 所以整个月你总是得到一月。

尝试,

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, monthOfYear);
sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(calendar.getTime());

or

sdf = new DateFormatSymbols().getShortMonths()[monthOfYear];

日期格式代码 fragment :

     /**
* Formats the specified object as a string using the pattern of this date
* format and appends the string to the specified string buffer.
* <p>
* If the {@code field} member of {@code field} contains a value specifying
* a format field, then its {@code beginIndex} and {@code endIndex} members
* will be updated with the position of the first occurrence of this field
* in the formatted text.
*
* @param object
* the source object to format, must be a {@code Date} or a
* {@code Number}. If {@code object} is a number then a date is
* constructed using the {@code longValue()} of the number.
* @param buffer
* the target string buffer to append the formatted date/time to.
* @param field
* on input: an optional alignment field; on output: the offsets
* of the alignment field in the formatted text.
* @return the string buffer.
* @throws IllegalArgumentException
* if {@code object} is neither a {@code Date} nor a
* {@code Number} instance.
*/
@Override
public final StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) {
if (object instanceof Date) {
return format((Date) object, buffer, field);
}
if (object instanceof Number) {
return format(new Date(((Number) object).longValue()), buffer, field);
}
throw new IllegalArgumentException("Bad class: " + object.getClass());
}

关于java - 无法将 int 月份转换为月份名称 android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com