本文介绍了如何以独立于语言的方式格式化日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以这种格式显示日期和时间,日期:12月9日,时间:19:20

I need to show date and time in this format, Date: 9th Dec, Time: 19:20

尽管我的以下代码在英语下工作正常,但是不知道这是我的应用程序支持的其他语言(例如中文,泰语等)中的正确方法。谢谢

Although my following code is working fine in English, have no idea this is correct way in other languages which my application is supporting like Chinese, Thai, etc. Any idea would be appreciated? Thanks

这是我的代码:

private String getFormattedTime(Calendar calendar)
{
   int hour = calendar.get(Calendar.HOUR_OF_DAY);
   int minute = calendar.get(Calendar.MINUTE);

   StringBuilder sBuilder = new StringBuilder(4);
   sBuilder.append(hour);
   sBuilder.append(":");

   // We want to display the hour like this: 12:09,
   // but by not using following code it becomes 12:9
   if (minute < 10)
   {
      sBuilder.append("0");
   }
   sBuilder.append(minute);

   return sBuilder.toString();
}

private String getDateSuffix( int day)
{
   if (day < 1 || day > 31)
   {
      throw new IllegalArgumentException("Illegal day of month");
   }

   switch (day)
   {
      case 1:
      case 21:
      case 31:
      return ("st");

      case 2:
      case 22:
      return ("nd");

      case 3:
      case 23:
      return ("rd");

      default:
      return ("th");
    }
}


推荐答案

I建议您使用Joda Time。

I would recommend that you use Joda Time.

以下是他们的。

  DateTime dt = new DateTime();
  String monthName = dt.monthOfYear().getAsText();
  String frenchShortName = dt.monthOfYear().getAsShortText(Locale.FRENCH);

Dec可能表示12月或décembre,Joda将使用正确的值,具体取决于语言环境

While "Dec" could mean December or décembre, Joda will use the correct one, depending on Locale.

因为 Locale.CHINESE Locale.THAI 是可用,它将为您翻译。

Since Locale.CHINESE and Locale.THAI are available, it will do the translation for you.

如果您使用Java 8,则。

If you use Java 8, Joda comes with the package.

如果使用此软件包,您将节省大量的工作。

You will save yourself a mountain of work if you use this package.

这篇关于如何以独立于语言的方式格式化日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 21:31