Android应用上的阿拉伯语日期 [英] Arabic Date on Android App

查看:160
本文介绍了Android应用上的阿拉伯语日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发android应用,下一个版本是在应用上添加阿拉伯语,但是我遇到了问题。

I'm working on android app , and the next release to add Arabic language on the app , but i have a problem .

这个问题是:Android操作系统将日期动态转换为阿拉伯格式,并且我在URL参数中使用了日期,服务器无法读取它。

this problem is : the Android OS convert the date dynamically to Arabic format , and i used it in URL parameter, and the server can't read it .

如何我将阿拉伯日期转换为英文日期吗?!?

How can i convert any Arabic date to English date ?!?

Android操作系统向我显示的内容:١٤-٠٥-٢٠١٤

我需要的是:2014-05-14

我厌倦了像这样的Java行:

I tired some Java lines like this :

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);

Android没有Locale.Arabic格式

And Android doesn't has the Locale.Arabic format

将手机转换为阿拉伯语时出现问题

推荐答案

tl; dr



您想要的格式恰好在ISO 8601标准中定义。 java.time 类在解析/生成文本时默认使用标准格式。

tl;dr

Your desired format happens to be defined in the ISO 8601 standard. The java.time classes use the standard formats by default when parsing/generating text.

LocalDate.now().toString()




2019-01-23

2019-01-23

要本地化为美国英语

LocalDate
.now(
    ZoneId.of( "America/Chicago" ) 
)
.format(
    DateTimeFormatter.ofLocalizedDate(
        FormatStyle.FULL   // or LONG or MEDIUM or SHORT
    )
    .withLocale(  
        new Locale( "en" , "US" )   // English language, with cultural norms of United States. 
    )
)

对于突尼斯的阿拉伯语,使用:

new Locale( "ar" , "TN" )



避免遗留日期时间类



您正在使用与最早的Java版本捆绑在一起的可怕的日期时间类。通过采用JSR 310,它们被 java.time 类完全取代。有关早期的Android,请参见下面的项目符号。

Avoid legacy date-time classes

You are using the terrible date-time classes bundled with the earliest versions of Java. They were supplanted entirely by the java.time classes with the adoption of JSR 310. For earlier Android, see bullets at bottom below.


而Android没有Locale.Arabic格式

And Android doesn't has the Locale.Arabic format

许多语言环境中只有少数具有命名常量。学会通过标准语言代码和国家(地区)代码指定语言环境,如 Locale 类。有关更多信息,请参见此相关问题

Only a few of the many locales have a named constant. Learn to specify a Locale by the standard language code and country (culture) code, as explained in the Locale class. For more info, see this related Question.

例如,对于具有沙特阿拉伯文化规范的阿拉伯语言,请使用:

Locale locale = new Locale( "ar" , "SA" ) ;  // Arabic language. Saudi Arabia cultural norms.

我们可以使用该语言环境来本地化表示某个时刻的文本。

We can use that locale to localize the text representing a moment.

ZoneId z = ZoneId.of( "Pacific/Auckland" );  // By the way, time zone has *nothing* to do with locale, orthogonal issues.
ZonedDateTime zdt = ZonedDateTime.now( z );
Locale locale = new Locale( "ar" , "SA" );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );

运行时。


الجمعة,18يناير2019 12:14:00متوقيتنيوزيلنداالصيفي

الجمعة، 18 يناير 2019 12:14:00 م توقيت نيوزيلندا الصيفي

要查看您的所有可用语言环境定义系统,运行此代码。

To see all the locale definitions available on your system, run this code.

    for ( Locale locale : Locale.getAvailableLocales() ) {
        System.out.println( locale.toString() + "  Name: " + locale.getDisplayName( Locale.US ) );
    }



今天的英文日期



LocalDate 类表示没有日期且没有时区 UTC偏移量

时区对于确定日期至关重要。在任何给定时刻,日期都会在全球范围内变化。例如,在法国巴黎午夜过后的几分钟,虽然仍然是昨天中的魁北克蒙特利尔

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still "yesterday" in Montréal Québec.

如果未指定时区,则JVM隐式应用其当前的默认时区。该默认值可能随时更改在运行时(!),因此您的结果可能会有所不同。最好将您的期望/期望时区明确指定为参数。

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

指定正确的时区名称,格式为大陆/地区,例如美国/蒙特利尔非洲/卡萨布兰卡太平洋/奥克兰。请勿使用2-4个字母的缩写,例如 EST IST ,因为它们不是 真实时区,不是标准化的,甚至不是唯一的(!)。

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果要使用JVM的当前默认时区,请提出要求并作为参数传递。如果省略该代码,则该代码将变得难以理解,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样不知道该问题。

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

或指定日期。您可以用数字设置月份,一月至十二月的理智编号为1-12。

Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

或者更好的方法是使用 Month 枚举对象已预先定义,每个对象一年中的月份。提示:在整个代码库中使用这些 Month 对象,而不是仅使用整数,可以使您的代码更具自记录性,确保有效值并提供类型安全。同上 年份 & YearMonth

Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year & YearMonth.

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

要生成本地化为英文的文本,请使用 DateTimeFormatter.ofLocalizedDate

To generate text localized to English, use DateTimeFormatter.ofLocalizedDate.

FormatStyle formatStyle = FormatStyle.FULL ;
Locale locale = new Locale( "en" , "US" ) ; // English language, United States cultural norms.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( formatStyle ).withLocale( locale );
String output = localDate.format( f ) ;






关于 java.time



java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date Calendar ,& SimpleDateFormat


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time 项目,现在处于维护模式 ,建议迁移到 java.time 类。

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参见 Oracle教程 。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

您可以直接与数据库交换 java.time 对象。使用符合 JDBC驱动程序 / jeps / 170 rel = nofollow noreferrer> JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql。* 类。

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

在哪里获取java.time类?

Where to obtain the java.time classes?


  • Java SE 8 Java SE 9 Java SE 10 Java SE 11 ,以及更高版本-具有捆绑实现的标准Java API。


    • Java 9添加了一些次要功能和修复。

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.

      • 大多数 java.time 功能都反向移植到Java 6& nofollow noreferrer> ThreeTen-Backport

      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

      ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如 时间间隔 YearWeek YearQuarter 更多

      这篇关于Android应用上的阿拉伯语日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆