Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-显示不同日期格式的月份名称_Java_Android_Datetime_Localization_Simpledateformat - Fatal编程技术网

Java Android-显示不同日期格式的月份名称

Java Android-显示不同日期格式的月份名称,java,android,datetime,localization,simpledateformat,Java,Android,Datetime,Localization,Simpledateformat,我有一个应用程序,它以本地化格式显示不同区域的日期和时间,也就是说,它根据用户的偏好显示日期和时间。但我的问题是它总是显示数字,即yyyy.mm.dd,或mm/dd/yyyy,或dd,mm,yyy。我想要的是:显示日期,使日期成为数字,年份成为数字,但月份显示为字符串,即Jan/Xin/Gen/Jān/Yan。。。或2018年1月1日/2018年1月1日/2018年1月1日,依此类推,但仍保留当前的本地格式。我知道,如果我像MMM一样硬编码,我可以做到这一点,但我希望根据本地化格式进行更改 因此

我有一个应用程序,它以本地化格式显示不同区域的日期和时间,也就是说,它根据用户的偏好显示日期和时间。但我的问题是它总是显示数字,即yyyy.mm.dd,或mm/dd/yyyy,或dd,mm,yyy。我想要的是:显示日期,使日期成为数字,年份成为数字,但月份显示为字符串,即Jan/Xin/Gen/Jān/Yan。。。或2018年1月1日/2018年1月1日/2018年1月1日,依此类推,但仍保留当前的本地格式。我知道,如果我像MMM一样硬编码,我可以做到这一点,但我希望根据本地化格式进行更改

因此,基本上这就是我的问题:如何根据从服务器获得的值(yyyy-MM-dd HH:MM:ss)显示本地化时间,并将月份显示为三个字母的单词,而不管它使用的是什么语言环境

我知道这个问题听起来很熟悉,但到目前为止,我已经尝试了很多答案,包括堆栈溢出和其他来源,但都没有成功。我尝试过的一些事情包括:///。。。但在我的例子中,我无法使它起作用

我的适配器:

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);
}
请尝试使用 getDisplayName(…)方法

为了得到更多的信息,你最好查阅文档


编辑 更充分地说,您可以尝试以下方法:

int monthOfYear = Calendar.JULY; // 6
String monthName = new 
DateFormatSymbols(Locale.getDefault()).getShortMonths()[monthOfYear];
请尝试使用 getDisplayName(…)方法

为了得到更多的信息,你最好查阅文档


编辑 更充分地说,您可以尝试以下方法:

int monthOfYear = Calendar.JULY; // 6
String monthName = new 
DateFormatSymbols(Locale.getDefault()).getShortMonths()[monthOfYear];

我认为以下方法将为您提供所需的日期时间格式化程序

public static DateTimeFormatter getLocalizedDateFormatter(Locale requestedLocale) {
    String formatPatternString = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
            FormatStyle.SHORT, null, 
            Chronology.ofLocale(requestedLocale), requestedLocale);
    // if not already showing month name, modify so it shows abbreviated month name
    if (! formatPatternString.contains("MMM")) {
        formatPatternString = formatPatternString.replaceAll("M+", "MMM");
    }
    return DateTimeFormatter.ofPattern(formatPatternString, requestedLocale);
}
测试:

输出:

ENGLISH (UNITED STATES)          Dec/24/17
FRENCH (FRANCE)                  24/déc./17
GERMAN (GERMANY)                 24.Dez.17
DANSK (DANMARK)                  24-dec.-17
SERBIAN (BOSNIA AND HERZEGOVINA) 17-дец-24
JSR-310也称为java.time 您试图使用过时且臭名昭著的类
DateFormat
SimpleDateFormat
。相反,我建议您养成使用JSR-310(现代Java日期和时间API)的习惯。所以我用上面的方法

我不认为这是问题的一部分,但为了完整起见,请按如下方式解析日期时间字符串。从my
getLocalizedDateFormatter
获得的格式化程序可用于格式化
LocalDateTime
以及除
LocalDate
之外的许多其他日期时间类型

    LocalDateTime ldt = LocalDateTime.parse("2017-12-24 22:51:34",
            DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"));
    String formattedDate = ldt.format(getLocalizedDateFormatter(Locale.UK));
问题:我可以在Android上使用JSR-310吗? 是的,你可以。它至少需要Java6

  • 在Java8和更高版本中,新的API是内置的
  • 在Java6和Java7中,获取三个后端口,即新类的后端口(三个用于JSR310)
  • 在Android上,使用Android版本的ThreeTen Backport。它叫ThreeTenABP
在后两种情况下,请确保将相应版本的backport库添加到项目中,使用下面的链接,并从
org.threeten.bp
org.threeten.bp.format
导入我使用的类

链接
  • ,解释如何使用
    java.time
  • ,Android版Three Ten Backport
  • ,解释得非常透彻
  • ,其中首次描述了现代日期和时间API

我认为以下方法将为您提供所需的日期时间格式化程序

public static DateTimeFormatter getLocalizedDateFormatter(Locale requestedLocale) {
    String formatPatternString = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
            FormatStyle.SHORT, null, 
            Chronology.ofLocale(requestedLocale), requestedLocale);
    // if not already showing month name, modify so it shows abbreviated month name
    if (! formatPatternString.contains("MMM")) {
        formatPatternString = formatPatternString.replaceAll("M+", "MMM");
    }
    return DateTimeFormatter.ofPattern(formatPatternString, requestedLocale);
}
测试:

输出:

ENGLISH (UNITED STATES)          Dec/24/17
FRENCH (FRANCE)                  24/déc./17
GERMAN (GERMANY)                 24.Dez.17
DANSK (DANMARK)                  24-dec.-17
SERBIAN (BOSNIA AND HERZEGOVINA) 17-дец-24
JSR-310也称为java.time 您试图使用过时且臭名昭著的类
DateFormat
SimpleDateFormat
。相反,我建议您养成使用JSR-310(现代Java日期和时间API)的习惯。所以我用上面的方法

我不认为这是问题的一部分,但为了完整起见,请按如下方式解析日期时间字符串。从my
getLocalizedDateFormatter
获得的格式化程序可用于格式化
LocalDateTime
以及除
LocalDate
之外的许多其他日期时间类型

    LocalDateTime ldt = LocalDateTime.parse("2017-12-24 22:51:34",
            DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"));
    String formattedDate = ldt.format(getLocalizedDateFormatter(Locale.UK));
问题:我可以在Android上使用JSR-310吗? 是的,你可以。它至少需要Java6

  • 在Java8和更高版本中,新的API是内置的
  • 在Java6和Java7中,获取三个后端口,即新类的后端口(三个用于JSR310)
  • 在Android上,使用Android版本的ThreeTen Backport。它叫ThreeTenABP
在后两种情况下,请确保将相应版本的backport库添加到项目中,使用下面的链接,并从
org.threeten.bp
org.threeten.bp.format
导入我使用的类

链接
  • ,解释如何使用
    java.time
  • ,Android版Three Ten Backport
  • ,解释得非常透彻
  • ,其中首次描述了现代日期和时间API

能否添加日期/时间的格式以及输出内容?还有MMM格式不适用的特定地区吗?另一方面,我认为当您使用1970年1月1日这样的日期格式时,区域设置并不重要。如果日期是2017年11月12日,那就很重要了。在这种情况下,它会引起混淆。@LeoNeo嘿,LeoNeo,这是我从服务器获得的格式:
yyyy-MM-dd HH:MM:ss
。我无法在几种不同的格式、3台设备和1台模拟器上显示它:)另外,我最初是按照您所说的那样显示的,使用了2018年1月1日的硬编码,但我想让它更漂亮一些。为了您自己的利益,请停止使用过时且臭名昭著的
SimpleDataFormat
和朋友。即使在安卓系统上,我也建议改用。你能加上你得到日期/时间的格式和你得到的输出吗?还有MMM格式不适用的特定地区吗?另一方面,我认为当你有约会的时候,地点并不重要