2012-05-23 18 views
9

文字列があるので、 "Tue May 21 Tue May 14:32:00 GMT 2012"と書いておきます。この文字列を現地時間形式の5月21日に変換したい、2012 2:32 pm。私はSimpleDateFormat( "MM dd、yyyy hh:mm a")。parse()を試しましたが、例外がスローされました。だから何をすべきか?Java/Android - GMT時間文字列を現地時間に変換する

例外は「報告されていない例外java.text.ParseException;キャッチされるか、スローされる必要があります。」ラインDate date = inputFormat.parse(inputText);

私はTextMateの上で走ったコードで

public class test{ 
    public static void main(String arg[]) { 
     String inputText = "Tue May 22 14:52:00 GMT 2012"; 
     SimpleDateFormat inputFormat = new SimpleDateFormat(
      "EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US); 
     inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); 
     SimpleDateFormat out = new SimpleDateFormat("MMM dd, yyyy h:mm a"); 
     Date date = inputFormat.parse(inputText); 
     String output = out.format(date); 
     System.out.println(output); 
    } 
} 
+0

どういう例外がありますか? –

+0

何が例外でしたか? – Jeshurun

+0

あなたの例文の上には21日があります。月曜日は火曜日ではありません。サンプルコードとは異なります。 –

答えて

20

あなたが実際に持っているテキスト形式に対応していないを解析ために提供フォーマット文字列。最初に解析し、次にフォーマットする必要があります。あなたが望むように見えます:

SimpleDateFormat inputFormat = new SimpleDateFormat(
    "EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US); 
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); 

SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a"); 
// Adjust locale and zone appropriately 

Date date = inputFormat.parse(inputText); 
String outputText = outputFormat.format(date); 

編集:ここでは短いが、完全なプログラムの形で同じコードは、あなたのサンプル入力で、です:

import java.util.*; 
import java.text.*; 

public class Test { 
    public static void main(String[] args) throws ParseException { 
     String inputText = "Tue May 21 14:32:00 GMT 2012"; 
     SimpleDateFormat inputFormat = new SimpleDateFormat 
      ("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US); 
     inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); 

     SimpleDateFormat outputFormat = 
      new SimpleDateFormat("MMM dd, yyyy h:mm a"); 
     // Adjust locale and zone appropriately 
     Date date = inputFormat.parse(inputText); 
     String outputText = outputFormat.format(date); 
     System.out.println(outputText); 
    } 
} 

あなたはその正確なコードをコンパイルして実行することができます

+1

私はそれを試しましたが、行Date date = inputFormat.parse(inputText);上記の例外がスローされます:( – Manto

+0

@ user1066956:あなたが私に質問に与えた文字列を使用したので、*正確な文字列を投稿してください。それは問題ありませんでした。 –

+0

"Tue May 21 14:32:00 GMT 2012 "は私が使用した正確な文字列です。私はTextMateで次のコードを実行し、上記のエラーが発生しました。 – Manto

3

構文解析に使用するフォーマッタは、期待する形式に定義する必要があります。

String date = "Tue May 21 14:32:00 GMT 2012"; 
DateFormat inputFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zz yyy"); 
Date d = inputFormat.parse(date); 
DateFormat outputFormat = new SimpleDateFormat("MMM dd, yyy h:mm a zz"); 
System.out.println(outputFormat.format(d)); 
1

方法SimpleDateFormat.parseは、解析例外がスローされます。ここでは、いくつかのエッジケースが入力のために行動する方法に応じてそれを変更する必要があるかもしれませんが、あなたが与えられた値のために働く例があります。

あなたはあなたにこのことを言っている取得している例外...

例外は「報告されていない例外java.text.ParseException; はキャッチまたはスローされるように宣言されなければなりません

ラップのtry-catch、あなたは黄金であるべきとの構文解析を行いライン..

Date d=null; 
try{ 
    d = inputFormat.parse(date); 
catch(ParseException e){ 
    // handle the error here.... 
} 

R

1

現在、面倒な古い日時クラスを使用していますが、現在はjava.timeクラスに置き換えられています。

間違った入力データ

21日は月曜日ではない火曜日であるとしてあなたの最初の例の文字列は、間違っています。 22番目の例の文字列は正しく、以下の私のコード例で使用されています。 java.time

を使用して

は、日付・時刻の値のテキスト表現のためのフォーマットのこの種を使用することは避けてください。特に、この種の形式で表示されるESTISTのような3〜4文字の略語は、実際のタイムゾーンではなく、標準化されておらず、ユニークではない(!)ので使用しないでください。 proper time zone nameを指定します。この特定の場合、java.timeはGMTUTCとして変換できますが、他の値は失敗する可能性があります。

String input = "Tue May 22 14:52:00 GMT 2012"; 
DateTimeFormatter f = DateTimeFormatter.ofPattern ("EEE MMM dd HH:mm:ss z uuuu").withLocale (Locale.US); 
ZonedDateTime zdt = ZonedDateTime.parse (input , f); 

System.out.println ("zdt: " + zdt); 

コンソールにダンプします。 toStringメソッドは、標準のISO 8601形式の文字列を生成し、大括弧内にゾーンの名前を追加して拡張します。これらの標準形式は、日時の値をテキストにシリアライズする必要がある場合には、はるかに優れた選択肢です。

System.out.println ("zdt: " + zdt); 

ZDT:2012-05-22T14:52Z [GMT]

あなたはあなたが望む任意の形式で、この値を表す文字列を生成することができます文字列

を生成します。通常は、LocaleDateTimeFormatterを使用して自動的にjava.timeをローカライズするのが最適です。

希望するフォーマットでは、日付部分には中程度のスタイルを使用しますが、時刻部分には短いスタイルを使用します。幸いにも、DateTimeFormatterは、FormatStyleオブジェクトのペアを渡すここに見られるように、各部分を別々にローカライズすることを可能にします。

Locale l = Locale.US; // Or Locale.CANADA_FRENCH, or Locale.ITALY, etc. 
DateTimeFormatter fOutput = DateTimeFormatter.ofLocalizedDateTime (FormatStyle.MEDIUM , FormatStyle.SHORT).withLocale (l); 
String output = zdt.format (fOutput); 

コンソールにダンプします。

System.out.println ("zdt: " + zdt + " | output: " + output); 

ZDT:2012-05-22T14:52Z [GMT] |出力:2012年5月22日14:52

java.timeについて

java.timeフレームワークは、Java 8に組み込まれており、後にされています。これらのクラスは、java.util.Date.Calendar、& java.text.SimpleDateFormatなどの古い面倒な日時クラスに取って代わります。

にあるJoda-Timeプロジェクトは、java.timeへの移行を推奨しています。

詳しくはOracle Tutorialをご覧ください。そして、多くの例と説明のためにStack Overflowを検索してください。

java.time機能ThreeTen-BackportでJava 6 & 7に戻って、移植、さらにThreeTenABPAndroidに適合されているの多く(How to use…を参照)。

ThreeTen-Extraプロジェクトは、追加のクラスでjava.timeを拡張します。このプロジェクトは、将来のjava.timeへの追加の可能性を証明する土台です。ここでは、IntervalYearWeekYearQuarterなどの便利なクラスがあります。

関連する問題