2015-03-19 102 views
-1

我收到日期(2015-03-20T06:34:01.831183 + 01:00)從服務器這不是簡單的日期格式,我想這種類型的日期轉換爲簡單的日期格式一樣(yyyyMMddHHmmssSSSS在Android中轉換爲簡單日期格式或Unix時間戳日期?

我該怎麼做。 我用這一點,但不是爲我工作

String tdate="2015-03-15T14:04:43.877593+01:00";   
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a"); 
    formatter.setTimeZone(TimeZone.getTimeZone("IST")); 
    String dateInString = "2015-03-15T14:04:43.877593+01:00";  

    try { 

     Date date = null; 
     try { 
      date = formatter.parse(dateInString); 
     } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
     } 
     System.out.println(date); 
     System.out.println(formatter.format(date)); 
     System.out.println("System Date in GMT: "+formatDateToString(date, "yyyyMMddHHmmssSSSS", "IST")); 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

錯誤的logcat

03-19 12:51:39.222: W/System.err(32124): java.text.ParseException: Unparseable date: "2015-03-15T14:04:43.877593+01:00" (at offset 19) 
03-19 12:51:39.228: W/System.err(32124): at java.text.DateFormat.parse(DateFormat.java:555) 
03-19 12:51:39.228: W/System.err(32124): at com.example.xmldatademo.XmlActivity.onCreate(XmlActivity.java:51) 
03-19 12:51:39.228: W/System.err(32124): at android.app.Activity.performCreate(Activity.java:5248) 
03-19 12:51:39.228: W/System.err(32124): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
03-19 12:51:39.229: W/System.err(32124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
03-19 12:51:39.229: W/System.err(32124): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
03-19 12:51:39.229: W/System.err(32124): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
03-19 12:51:39.229: W/System.err(32124): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
03-19 12:51:39.229: W/System.err(32124): at android.os.Handler.dispatchMessage(Handler.java:102) 
03-19 12:51:39.230: W/System.err(32124): at android.os.Looper.loop(Looper.java:136) 
03-19 12:51:39.230: W/System.err(32124): at android.app.ActivityThread.main(ActivityThread.java:5086) 
03-19 12:51:39.230: W/System.err(32124): at java.lang.reflect.Method.invokeNative(Native Method) 
03-19 12:51:39.230: W/System.err(32124): at java.lang.reflect.Method.invoke(Method.java:515) 
03-19 12:51:39.231: W/System.err(32124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
03-19 12:51:39.231: W/System.err(32124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
03-19 12:51:39.231: W/System.err(32124): at dalvik.system.NativeStart.main(Native Method) 
03-19 12:51:39.231: I/System.out(32124): null 
03-19 12:51:39.233: D/AndroidRuntime(32124): Shutting down VM 
03-19 12:51:39.233: W/dalvikvm(32124): threadid=1: thread exiting with uncaught exception (group=0x416a5d40) 
03-19 12:51:39.235: E/AndroidRuntime(32124): FATAL EXCEPTION: main 
03-19 12:51:39.235: E/AndroidRuntime(32124): Process: com.example.xmldatademo, PID: 32124 
03-19 12:51:39.235: E/AndroidRuntime(32124): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmldatademo/com.example.xmldatademo.XmlActivity}: java.lang.NullPointerException 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.os.Handler.dispatchMessage(Handler.java:102) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.os.Looper.loop(Looper.java:136) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread.main(ActivityThread.java:5086) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.lang.reflect.Method.invokeNative(Native Method) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.lang.reflect.Method.invoke(Method.java:515) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at dalvik.system.NativeStart.main(Native Method) 
03-19 12:51:39.235: E/AndroidRuntime(32124): Caused by: java.lang.NullPointerException 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.util.Calendar.setTime(Calendar.java:1183) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:518) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.text.SimpleDateFormat.format(SimpleDateFormat.java:818) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at java.text.DateFormat.format(DateFormat.java:307) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at com.example.xmldatademo.XmlActivity.onCreate(XmlActivity.java:56) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.Activity.performCreate(Activity.java:5248) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
03-19 12:51:39.235: E/AndroidRuntime(32124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
03-19 12:51:39.235: E/AndroidRuntime(32124): ... 11 more 

回答

1

檢查了這一點。希望這會有所幫助。我不讀秒數(SSS)。

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
     formatter.setTimeZone(TimeZone.getTimeZone("IST")); 
     String dateInString = "2015-03-15T14:04:43.877593+01:00"; 

     SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSSS"); 

     Date date = null; 
     try { 
      //date instace 
      date = formatter.parse(dateInString); 
      //date format -> yyyyMMddHHmmssSSSS 
      String sNewDate = newDateFormat.format(date); 
      System.out.println("New Date Format " + sNewDate); 
     } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
     } 
相關問題