Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java date time in arabic

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).

like image 800
Muthu Avatar asked Nov 12 '13 07:11

Muthu


2 Answers

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();
    }
like image 103
Masudul Avatar answered Nov 19 '22 09:11

Masudul


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 م
like image 1
Basil Bourque Avatar answered Nov 19 '22 08:11

Basil Bourque