Java date time in arabic

有些话、适合烂在心里 提交于 2020-01-27 08:35:31

问题


How to get arabic date when the user selected language is Arabic and date in english format when selected language is english in spring application ? I tried by setting the default locale to english and arabic based on the request but this doesn't help me in getting calendar api time in arabic for (9 hours 15 mins).


回答1:


Use Joda time or Java 8 Date time API. Following is the example of Java 8 API

    if(language.equals("English")){
      LocalDateTime now = LocalDateTime.now();
    }
    else{        
      HijrahDate hijrahDate=HijrahDate.now();
    }



回答2:


Confused Question

Your question is confusing, and your comment did not help to clarify.

Did you want an Islamic calendar? If so, the answer by Masud is correct.

Did you want to adjust the time zone of the date-time to an Arab location such as Riyadh?

Did you want to localize the words used in the textual representation of a date-time, such as month and day name?

Did you want to localize the format of textual representation of a date-time, such as the order of presentation of day, month, year?

Joda-Time

I'll just spin out a bit of example code using the Joda-Time 2.3 library, hoping it might help.

To create a java.util.Locale, you need:

  • Language code (either, see combined list)
    • ISO 639 alpha-2
    • ISO 639 alpha-3
  • Country Code (either)
    • ISO 3166 alpha-2 country code
    • UN M.49 numeric-3 area code

Example Code

DateTime dateTimeUtc = new DateTime( DateTimeZone.UTC );

DateTimeZone timeZone = DateTimeZone.forID( "Asia/Riyadh" );
DateTime dateTimeRiyadh = dateTimeUtc.withZone( timeZone );

java.util.Locale locale = new Locale( "ar", "SA" ); // ( language code, country code );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale ).withZone( timeZone );
String output = formatter.print( dateTimeUtc );

Dump to console…

System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeRiyadh: " + dateTimeRiyadh );
System.out.println( "output: " + output );

When run…

dateTimeUtc: 2014-02-16T09:52:33.901Z
dateTimeRiyadh: 2014-02-16T12:52:33.901+03:00
output: 16 فبراير, 2014 AST 12:52:33 م


来源:https://stackoverflow.com/questions/19923498/java-date-time-in-arabic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!