Can';t将int month转换为月份名称android java
我正在尝试将int month转换为month name(字符串)。我使用的是简单的日期格式,但所有月份我只得到“一月”。为什么会这样Can';t将int month转换为月份名称android java,java,android,datepicker,simpledateformat,Java,Android,Datepicker,Simpledateformat,我正在尝试将int month转换为month name(字符串)。我使用的是简单的日期格式,但所有月份我只得到“一月”。为什么会这样 public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { String sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(monthOfYear)
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);
simpleDataFormat#format()
方法有一个重载,它接受一个对象
,这就是为什么您能够向它传递一个int
。但是,这不是您想要的方法。您需要一个带有日期
对象的对象,我们可以从日历
中获得该对象,并设置相应的月份:
private String getMonthAbbr(int monthOfYear) {
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, monthOfYear);
return new SimpleDateFormat("LLL").format(c.getTime());
}
在这种情况下,传递Locale.getDefault()
是多余的,因为如果没有另外指定,这就是SimpleDateFormat
将使用的。SimpleDateFormat#format()方法有一个重载,它接受一个对象,这就是为什么您能够向它传递一个int
。但是,这不是您想要的方法。您需要一个带有日期
对象的对象,我们可以从日历
中获得该对象,并设置相应的月份:
private String getMonthAbbr(int monthOfYear) {
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, monthOfYear);
return new SimpleDateFormat("LLL").format(c.getTime());
}
在这种情况下,传递Locale.getDefault()
是多余的,因为如果没有另外指定,这就是SimpleDataFormat
将使用的
我每个月都只得到“简”
因为String sdf=newsimpledateformat(“LLL”,Locale.getDefault()).format(monthOfYear)
这里,monthOfYear
是数字对象,因此,DateFormate
类将它从该数字转换为Date
对象,该数字将介于1970年1月1日
到1970年1月12日之间,因此您总是得到所有月份的一月
试试看
DateFormat代码段:
/**
* 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());
}
/**
*使用此日期的模式将指定对象格式化为字符串
*格式化字符串并将其附加到指定的字符串缓冲区。
*
*如果{@code field}的{@code field}成员包含指定
*格式字段,然后是其{@code beginIndex}和{@code endIndex}成员
*将使用此字段第一次出现的位置进行更新
*在格式化文本中。
*
*@param对象
*要格式化的源对象必须是{@code Date}或
*{@code Number}。如果{@code object}是一个数字,那么日期就是一个数字
*使用数字的{@code longValue()}构造。
*@param缓冲区
*要将格式化日期/时间附加到的目标字符串缓冲区。
*@param字段
*输入时:可选的对齐字段;关于输出:偏移量
*格式化文本中对齐字段的名称。
*@返回字符串缓冲区。
*@galargumentException
*如果{@code object}既不是{@code Date}也不是
*{@code Number}实例。
*/
@凌驾
公共最终StringBuffer格式(对象对象、StringBuffer缓冲区、FieldPosition字段){
if(对象实例of Date){
返回格式((日期)对象、缓冲区、字段);
}
if(对象实例的编号){
返回格式(新日期((数字)对象).longValue()),缓冲区,字段);
}
抛出新的IllegalArgumentException(“坏类:+object.getClass());
}
我每个月都只得到“简”
因为String sdf=newsimpledateformat(“LLL”,Locale.getDefault()).format(monthOfYear)
这里,monthOfYear
是数字对象,因此,DateFormate
类将它从该数字转换为Date
对象,该数字将介于1970年1月1日到1970年1月12日之间,因此您总是得到所有月份的一月
试试看
DateFormat代码段:
/**
* 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());
}
/**
*使用此日期的模式将指定对象格式化为字符串
*格式化字符串并将其附加到指定的字符串缓冲区。
*
*如果{@code field}的{@code field}成员包含指定
*格式字段,然后是其{@code beginIndex}和{@code endIndex}成员
*将使用此字段第一次出现的位置进行更新
*在格式化文本中。
*
*@param对象
*要格式化的源对象必须是{@code Date}或
*{@code Number}。如果{@code object}是一个数字,那么日期就是一个数字
*使用数字的{@code longValue()}构造。
*@param缓冲区
*要将格式化日期/时间附加到的目标字符串缓冲区。
*@param字段
*输入时:可选的对齐字段;关于输出:偏移量
*格式化文本中对齐字段的名称。
*@返回字符串缓冲区。
*@galargumentException
*如果{@code object}既不是{@code Date}也不是
*{@code Number}实例。
*/
@凌驾
公共最终StringBuffer格式(对象对象、StringBuffer缓冲区、FieldPosition字段){
if(对象实例of Date){
返回格式((日期)对象、缓冲区、字段);
}
if(对象实例的编号){
返回格式(新日期((数字)对象).longValue()),缓冲区,字段);
}
抛出新的IllegalArgumentException(“坏类:+object.getClass());
}
tl;博士
java.time.Month
现在在java.time类中更容易实现,它取代了这些麻烦的旧遗留日期时间类
枚举定义了十几个对象,每个月一个
一月至十二月的月份编号为1-12
Month month = Month.of( 2 ); // 2 → February.
要求对象生成一个字符串。调整以指定名称的长度或缩写。具体说明翻译中应使用哪种人类语言,以及哪些文化规范应决定缩写、标点和大写等问题
String output = Month.FEBRUARY.getDisplayName( TextStyle.SHORT_STANDALONE , Locale.CANADA_FRENCH );
关于java.time
该框架内置于Java8及更高版本中。这些类取代了tr