Java date time in arabic

前端 未结 2 619
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 01:54

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

相关标签:
2条回答
  • 2020-12-07 02:27

    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();
        }
    
    0 讨论(0)
  • 2020-12-07 02:31

    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 م
    
    0 讨论(0)
提交回复
热议问题