私はアラビア語でウェブ監視プロジェクトに取り組んでいると私はこの1つのような文字列の日付を変換したい:アラビア文字列の日付をJava 8の日付オブジェクトに変換するにはどうすればよいですか?
الاثنين 24 أبريل 2017 - 15:00
のJava 8日付オブジェクトに。どうやってやるの?
私はアラビア語でウェブ監視プロジェクトに取り組んでいると私はこの1つのような文字列の日付を変換したい:アラビア文字列の日付をJava 8の日付オブジェクトに変換するにはどうすればよいですか?
الاثنين 24 أبريل 2017 - 15:00
のJava 8日付オブジェクトに。どうやってやるの?
一つの解決策は、英語に日付を変換し、それを解析することができます
private final static Map<String, Integer> monthMapping = new HashMap<>();
static {
// list of all month.
monthMapping.put("أبريل", "4");
}
public Date fromArabicToDate(String arabicInput) throws ParseException {
String[] parts = arabicInput.split(" ");
if (parts.length != 4)
throw new IllegalArgumentException();
String dateInput = parts[0] + "-" + monthMapping.get(parts[1]) + "-" + parts[2];
SimpleDateFormat parser = new SimpleDateFormat("YYYY-MM-DD");
return parser.parse(dateInput);
}
私は月にコピーしようとしたが、私はそれを正しく行っているとは思いません。構文解析時にput
の引数が切り替えられます。
またはJoda-Timeをご覧ください。彼らには解決策があるかもしれない。それはmentioned hereでした。
ああ...廃止された情報。あなたは学習を止めることはありません。ありがとう@ハルク。これは私の心のどこかにあった。私は同じことを言っているが、それは4歳以上であることが分かった:http://stackoverflow.com/a/14439397/2097191(私は私の答えを変更した) – Obenland
あなたが言っている答えは、 [SimpleDateFormat'の特定のコンストラクタ](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat-java.lang.String-java.util.Locale- ): "指定されたパターンと、デフォルトのFORMATロケールのデフォルトの日付書式記号を使用してSimpleDateFormatを構築します(注:このコンストラクタはすべてのロケールをサポートしているわけではありません。 – Hulk
アラビア語の書式設定された日付を理解するには十分なアラビア語がわかりません。しかし、このコード:
Locale arabicLocale = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build();
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(arabicLocale);
String formatted = date.format(formatter);
System.out.println(formatted);
System.out.println(formatter.parse(formatted));
は、この出力が得られます。
26 أبريل, 2017
{},ISO resolved to 2017-04-26
Locale
を作成するためのコードが答えからSetting Arabic numbering system locale doesn't show Arabic numbers
にあるあなたがFormatStyle
あなた自身を定義することによって、このフォーマットを微調整することができます。
編集:
String dateTimeString = "الاثنين 24 أبريل 2017 - 15:00";
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEEE d MMMM uuuu - HH:mm", new Locale("ar"));
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
この版画:日付したいと仮定して、文字列を解析するときには文字セットを指定する必要が
2017-04-24T15:00
nu-extensionなしでロケールを構築するだけです。それはあなたのニューエクステンションなしで動作し、失敗するはずです(しかしそうではありません)、私の答えを見てください。 –
ありがとう、@MenoHochschild。私のコンピュータでも 'new Locale(" ar ")'が動作するので、私は複雑な言語タグの代わりにそれを入れました。 –
インスピレーションのためのスリムでメノホックシールドのおかげで解析するには、常にこれが機能するように指定した形式になります。
public static Date getDate(String strDate) throws Exception{
strDate=new String(strDate.getBytes(),"UTF-8");
Map<String, Integer> months = new HashMap<>();
String JAN = new String("يناير".getBytes(), "UTF-8");
String FEB = new String("فبراير".getBytes(), "UTF-8");
String MAR = new String("مارس".getBytes(), "UTF-8");
String APR = new String("أبريل".getBytes(), "UTF-8");
String APR_bis = new String("ابريل".getBytes(), "UTF-8");
String MAY = new String("ماي".getBytes(), "UTF-8");
String JUN = new String("بونيو".getBytes(), "UTF-8");
String JUN_bis = new String("يونيه".getBytes(), "UTF-8");
String JUL = new String("يوليوز".getBytes(), "UTF-8");
String AUG = new String("غشت".getBytes(), "UTF-8");
String SEP = new String("شتنبر".getBytes(), "UTF-8");
String SEP_bis = new String("سبتمبر".getBytes(), "UTF-8");
String OCT = new String("أكتوبر".getBytes(), "UTF-8");
String OCT_bis = new String("اكتوبر".getBytes(), "UTF-8");
String NOV = new String("نونبر".getBytes(), "UTF-8");
String NOV_bis = new String("نوفمبر".getBytes(), "UTF-8");
String DEC = new String("دجنبر".getBytes(), "UTF-8");
String DEC_bis = new String("ديسمبر".getBytes(), "UTF-8");
months.put(JAN, 0);
months.put(FEB, 1);
months.put(MAR, 2);
months.put(APR, 3);
months.put(APR_bis, 3);
months.put(MAY, 4);
months.put(JUN, 5);
months.put(JUN_bis, 5);
months.put(JUL, 6);
months.put(AUG, 7);
months.put(SEP, 8);
months.put(SEP_bis, 8);
months.put(OCT, 9);
months.put(OCT_bis, 9);
months.put(NOV, 10);
months.put(NOV_bis, 10);
months.put(DEC, 11);
months.put(DEC_bis, 11);
StringTokenizer stringTokenizer = new StringTokenizer(strDate);
Calendar calendar = Calendar.getInstance();
while(stringTokenizer.hasMoreElements()) {
stringTokenizer.nextElement();// to skip the first string which is the name of the day
int day = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
String strMonth = stringTokenizer.nextElement().toString().trim();
int month = months.get(strMonth);
int year = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
calendar.set(year, month, day);
}
return calendar.getTime();
}
utput:
Fri Oct 20 15:26:47 WEST 2017
文字列変換のポイントがありません。私のコンピュータの 'new String("يناير "。getBytes()、" UTF-8 ")'は ''يناير ''と同じ文字列を返します。他のすべての月の名前と同じです。結果は、デフォルトの文字セットが異なるコンピュータでは異なる場合があります。 –
yeah @ OleV.V。必要な文字セットが「UTF-8」であることを指定するために追加されました –
@Oleと@slimの回答は機能していますが、考えている理由ではありません。
まず観測 - NU-拡張が与えられた例のために不要である:
OLES提案もロケールnew Locale("ar", "SA")
代わりにLocale.forLanguageTag("ar-SA-u-nu-arab")
ために働くことになります。では、ここでユニコード - ヌー拡張は何ですか?何もない。次の質問:
ここではnu-extensionとは何ですか?
ヌーコードワード "アラブ"は、アラビア数字の数字を得るためにspecified by the unicode consortiumです。しかし、の解析対象入力は西暦0-9(歴史的にアラブ人から追い抜かれており、コードワード "latn"として指定されています。NU-拡張子は本当にここにその仕事をしていたのであれば、その後解析はarabic-indic digitsために失敗した0-9はありませんが、しておく必要があります
明らかに、nu拡張はJava-8の新しいtime-APIによって一般的にサポートされていません。
SimpleDateFormat
はnu-extensionをサポートしていますか?
SimpleDateFormat sdf =
new SimpleDateFormat("EEEE d MMMM yyyy - HH:mm", Locale.forLanguageTag("ar-SA-nu-arab"));
Date d = sdf.parse(dateTimeString);
System.out.println(d);
String formatted = sdf.format(d);
System.out.println(formatted);
System.out.println(sdf.format(d).equals(dateTimeString));
sdf = new SimpleDateFormat("EEEE d MMMM uuuu - HH:mm", Locale.forLanguageTag("ar-SA-u-nu-thai"));
String thai = sdf.format(d);
System.out.println("u-nu-thai: " + thai);
私が想定しています。コードを以下のデバッグを使用し
は、私はNU-拡張子のみアラビア・インド数字の公式クラスjava.util.Locale
のjavadocのではなく、また、参照(タイ数字でサポートされていることを発見しましたJavaの-8のクラスDateTimeFormatter
もタイの数字をサポートしています
結論:
はNU-拡張子を忘れるだけのconst。 Unicode拡張を使わずに旧式の方法でロケールを破棄して、Olesにこのように答えてください。あなたの入力は0-9のウェスタン数字しかないので動作します。
さまざまなナンバリングシステム用のnu-extensionを含む広範なi18nサポート(このような入力がある場合)は、外部ライブラリ(ICU4JやmyLib Time4Jなど)を検討することもできます。
アラビア語を使用する国でロケールを使用しようとしましたか?参照してください:http://stackoverflow.com/questions/29154887/setting-arabic-numbering-system-locale-doesnt-show-arabic-numbers/29155743#29155743 – slim
これは、34ヒットで既にアドレス指定されていません"java arabic date"](http://stackoverflow.com/search?q=java+arabic+date)? –