当我将 SimpleDateFormat class 与 "YYYY" 一起使用时应用程序崩溃
App crashes when I use SimpleDateFormat class with "YYYY"
我正在使用 SimpleDateFormat class 像这样获取当前年份
SimpleDateFormat currentYear = new SimpleDateFormat("YYYY");
Date thisYear = new Date();
String stringThisYear = currentYear .format(thisYear );
它在这里(印度)工作正常,但是当它在马来西亚 运行 被其他 people.After 大量调试和搜索时它崩溃了,我发现它正在发生,因为没有提及locale.So 我将我的代码更改为这个
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.ENGLISH);
它仍然崩溃,所以我使用日历实例来获取当前年份
int thisYear=Calendar.getInstance().get(Calendar.YEAR);
但是我想知道 simpleDateFormat 有什么问题,因为我的大部分代码已经有 simpleDateFormat class.So 我想知道崩溃的原因,如果有人有解决方案,请帮助 me.Thank你
像这样尝试为设备使用默认语言环境
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.getDefault());
我在使用它时遇到了同样的问题,它工作正常。
SimpleDateFormat formatter = new SimpleDateFormat("YYYY",Locale.US);
这是因为你必须使用 "yyyy" 而不是 "YYYY"
在旧版本的 Android 上,使用 "YYYY" 会使您的应用程序崩溃。与 country/Locale
无关
"Y" 代表周年而不是年,如指定的那样 in the Javadoc
编辑 2
yyyy is the pattern string to identify the year in the
SimpleDateFormat class.
Java 7 introduced YYYY as a new date pattern to identify the date week
year.
An average year is exactly 52.1775 weeks long, which means that
eventually a year might have either 52 or 53 weeks considering
indivisible weeks.
Using YYYY unintendedly while formatting a date can cause severe
issues in your Java application.
如@OleV.V所述,格式 Y
仅在 Android API 24+ 上受支持。使用低于 API 24 的任何版本都会使应用程序崩溃。
我正在使用 SimpleDateFormat class 像这样获取当前年份
SimpleDateFormat currentYear = new SimpleDateFormat("YYYY");
Date thisYear = new Date();
String stringThisYear = currentYear .format(thisYear );
它在这里(印度)工作正常,但是当它在马来西亚 运行 被其他 people.After 大量调试和搜索时它崩溃了,我发现它正在发生,因为没有提及locale.So 我将我的代码更改为这个
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.ENGLISH);
它仍然崩溃,所以我使用日历实例来获取当前年份
int thisYear=Calendar.getInstance().get(Calendar.YEAR);
但是我想知道 simpleDateFormat 有什么问题,因为我的大部分代码已经有 simpleDateFormat class.So 我想知道崩溃的原因,如果有人有解决方案,请帮助 me.Thank你
像这样尝试为设备使用默认语言环境
SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.getDefault());
我在使用它时遇到了同样的问题,它工作正常。
SimpleDateFormat formatter = new SimpleDateFormat("YYYY",Locale.US);
这是因为你必须使用 "yyyy" 而不是 "YYYY"
在旧版本的 Android 上,使用 "YYYY" 会使您的应用程序崩溃。与 country/Locale
无关"Y" 代表周年而不是年,如指定的那样 in the Javadoc
编辑 2
yyyy is the pattern string to identify the year in the SimpleDateFormat class.
Java 7 introduced YYYY as a new date pattern to identify the date week year.
An average year is exactly 52.1775 weeks long, which means that eventually a year might have either 52 or 53 weeks considering indivisible weeks.
Using YYYY unintendedly while formatting a date can cause severe issues in your Java application.
如@OleV.V所述,格式 Y
仅在 Android API 24+ 上受支持。使用低于 API 24 的任何版本都会使应用程序崩溃。