我从服务器获取日期( 2015-03-20T06:34:01.831183 + 01:00 ),这不是简单的日期格式我想将这种类型的日期转换为简单的日期格式,如( yyyyMMddHHmmssSSSS )
我该怎么办? 我用过这个但不适合我
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
答案 0 :(得分: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();
}