【发布时间】:2018-06-06 08:16:04
【问题描述】:
我有一个应用程序以本地化格式显示不同区域的日期和时间,也就是说 - 它根据用户的偏好显示日期和时间。但我的问题是它总是显示数字,即 yyyy.mm.dd,或 mm/dd/yyy,或 dd,mm,yyyy。我想要的是:显示日期以便日期是数字,年份是数字,但月份显示为字符串,即。 Jan / Xin / Gen / Jān / Yan... or 01 Jan 2018 / Jan 01 2018 / 2018 Jan 01,以此类推,但仍保持当前本地格式。我知道如果我像 MMM 一样对其进行硬编码,我可以做到这一点,但我希望它根据本地化格式进行更改。
所以基本上这是我的问题:如何根据从服务器获得的值(yyyy-MM-dd HH: mm: ss)显示本地化时间,月份显示为三字母词,无论它使用什么语言环境?
我知道这个问题听起来很熟悉,但到目前为止,我已经尝试了很多答案,包括堆栈溢出和其他来源,但没有任何成功。我尝试过的一些事情包括:1/2/3/4/5/6 ...但我无法在我的示例中使用它。
我的适配器:
public void onBindViewHolder(RestaurantsAdapter.RestaurantsViewHolder holder, int position) {
// getting restaurant data for the row
Restaurant restaurant = restaurant Items.get(position);
holder.userName.setText(restaurant .getUserName());
holder.date.setText(convertDate(restaurant .getDateTime())); //string dobiti u formatu, pretvoriti ga u localized i podijeliti na dva dijela
holder.time.setText(convertTime(restaurant .getDateTime()));
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.withBorder(0)
.toUpperCase()
.endConfig()
.roundRect(10);
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getColor(restaurant.getUserId());
TextDrawable textDrawable = builder.build(restaurant Items.get(position).getUserName().substring(0, 1), color);
holder.thumbNail.setImageDrawable(textDrawable);
Picasso.with(context)
.load(AppConfig.URL_PROFILE_PHOTO + restaurant.getThumbnailUrl())
.placeholder(textDrawable)
.error(textDrawable)
.transform(new RoundedTransform(12, 0))
.fit()
.centerCrop()
.into(holder.thumbNail);
}
private String convertDate(String time) {
final DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
SimpleDateFormat dateFormatReceived = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
SimpleDateFormat dateFormatConverted = new SimpleDateFormat(((SimpleDateFormat) shortDateFormat).toPattern(), Locale.getDefault());
java.util.Date date = null;
try {
date = dateFormatReceived.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormatConverted.format(date);
}
private String convertTime(String time) {
final DateFormat shortTimeFormat = android.text.format.DateFormat.getTimeFormat(context.getApplicationContext());
SimpleDateFormat timeFormatReceived = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
SimpleDateFormat timeFormatConverted = new SimpleDateFormat(((SimpleDateFormat) shortTimeFormat).toPattern(), Locale.getDefault());
java.util.Date date = null;
try {
date = timeFormatReceived.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return timeFormatConverted.format(date);
}
更新:
我尝试将此添加到我的解决方案中:
private String convertDate(String time) {
final DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
Calendar Now = Calendar.getInstance();
Now.getDisplayName(Calendar.HOUR, Calendar.SHORT, Locale.getDefault());
SimpleDateFormat dateFormatReceived = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
SimpleDateFormat dateFormatConverted = new SimpleDateFormat(((SimpleDateFormat) shortDateFormat).toPattern(), Locale.getDefault());
dateFormatConverted.getDisplayName(Calendar.HOUR, Calendar.SHORT, Locale.getDefault());
java.util.Date date = null;
try {
date = dateFormatReceived.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormatConverted.format(date);
}
【问题讨论】:
-
您能否添加您获得日期/时间的格式以及您获得的输出?还有 MMM 格式不起作用的特定语言环境吗?另一方面,当您使用像 1970 年 1 月 1 日这样的日期格式时,我认为区域设置并不重要。如果日期是 11/12/17 会很重要,在这种情况下会造成混淆。
-
@LeoNeo 嘿 LeoNeo,这是我从服务器获得的格式:
yyyy-MM-dd HH:mm:ss。我无法让它在几种不同的格式、3 个设备和 1 个模拟器上显示 :) 另外,我最初按你说的那样显示它,硬编码 2018 年 1 月 1 日,但我想让它更漂亮一点。 -
请为了您自己的利益,停止使用早已过时且臭名昭著的麻烦
SimpleDateFormat和朋友。即使在 Android 上,我也建议改用 JSR-310, AKAjava.time, the modern Java date and time API。使用起来感觉好多了。
标签: java android datetime localization simpledateformat