2016-06-16 6 views
0

日付の文字列を日付に解析しようとしています。 I私はコード実行中に、私はこの出力を取得しています上記の入力文字列 の間違った出力を取得しています、コード日付の変換中に間違った日付を取得しています

public class convertDate 
{ 
    public static void main(String args[]) 
    { 
     String strlastruntime ="16/06/2016 9:17:00 AM",dateFormat ="MM/dd/yyyy hh:mm:ss a"; 

     try 
     { 
      strlastruntime = strlastruntime.trim(); 
      System.out.println("strlastruntime = "+strlastruntime+" dateFormat = "+dateFormat); 
      java.util.Locale l = java.util.Locale.US; 
      java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(dateFormat,l); 
      //System.out.println("formatter = "+formatter); 
      java.util.Date date = formatter.parse(strlastruntime); 
      System.out.println("date = "+date); 
      long time = date.getTime(); 
      System.out.println("time = "+time); 

     } 
     catch(java.text.ParseException ee) 
     { 
      ee.printStackTrace(); 
      System.out.println(ee); 
     } 
    } 
} 

次試みたが、している:だから

strlastruntime = 16/06/2016 9:17:00 AM dateFormat = MM/dd/yyyy hh:mm:ss a 
date = Thu Apr 06 09:17:00 IST 2017 
time = 1491450420000 

を、見つけるために私を助けてくださいソリューション..

+0

何の出力あなたの代わりに期待したのですか? –

答えて

2

変更dateFormat ="MM/dd/yyyy hh:mm:ss a";

dateFormat ="dd/MM/yyyy hh:mm:ss a";への完全なコード

public static void main(String args[]) 
    { 
     String strlastruntime ="16/06/2016 9:17:00 AM",dateFormat ="dd/MM/yyyy hh:mm:ss a"; 

     try 
     { 
      strlastruntime = strlastruntime.trim(); 
      System.out.println("strlastruntime = "+strlastruntime+" dateFormat = "+dateFormat); 
      java.util.Locale l = java.util.Locale.US; 
      java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(dateFormat,l); 
      //System.out.println("formatter = "+formatter); 
      java.util.Date date = formatter.parse(strlastruntime); 
      System.out.println("date = "+date); 
      long time = date.getTime(); 
      System.out.println("time = "+time); 

     } 
     catch(java.text.ParseException ee) 
     { 
      ee.printStackTrace(); 
      System.out.println(ee); 
     } 
    } 

出力:

strlastruntime = 16/06/2016 9:17:00 AM dateFormat = dd/MM/yyyy hh:mm:ss a 
date = Thu Jun 16 09:17:00 IST 2016 
time = 1466048820000 
+0

Hai、なぜ私のコードで日付形式(dateFormat = "MM/dd/yyyy hh:mm:ss a")が動作しないのか分かりますか? –

+0

@KaviChinna 16/06/2016はdd/mm/yyyy形式です – SpringLearner

+0

@KaviChinnaはあなたの問題を解決しましたか? – SpringLearner

関連する問題