我已经尝试了两个多小时将字符串转换为日期。我不明白我做错了什么?
这是我收到的字符串日期=“星期四,2013 年 1 月 31 日 10:48:58 GMT”
这是我的代码:
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = new Date();
d = sdf.parse(expire);
long timestamp = d.getTime();
return timestamp;
} catch (ParseException e) {
e.printStackTrace();
}
我总是遇到一个例外,我想我的简单日期格式是错误的。任何可以提供帮助。
我的日志猫:
01-30 11:41:03.453: W/System.err(8577): java.text.ParseException: Unparseable date: "Thu, 31 Jan 2013 10:48:58 GMT" (at offset 0)
01-30 11:41:03.453: W/System.err(8577): at java.text.DateFormat.parse(DateFormat.java:626)
01-30 11:41:03.453: W/System.err(8577): at fr.haploid.lrucachelibrary.ImageFetcher.getTimestamp(ImageFetcher.java:305)
01-30 11:41:03.453: W/System.err(8577): at fr.haploid.lrucacheprotoptvo.activities.ImageGridActivity$ImageAdapter.getView(ImageGridActivity.java:152)
01-30 11:41:03.453: W/System.err(8577): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
01-30 11:41:03.453: W/System.err(8577): at android.widget.GridView.onMeasure(GridView.java:1030)
01-30 11:41:03.453: W/System.err(8577): at android.view.View.measure(View.java:15172)
01-30 11:41:03.453: W/System.err(8577): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-30 11:41:03.457: W/System.err(8577): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
01-30 11:41:03.457: W/System.err(8577): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-30 11:41:03.457: W/System.err(8577): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-30 11:41:03.457: W/System.err(8577): at android.view.View.measure(View.java:15172)
01-30 11:41:03.457: W/System.err(8577): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-30 11:41:03.457: W/System.err(8577): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-30 11:41:03.457: W/System.err(8577): at android.view.View.measure(View.java:15172)
01-30 11:41:03.457: W/System.err(8577): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-30 11:41:03.457: W/System.err(8577): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-30 11:41:03.461: W/System.err(8577): at android.view.View.measure(View.java:15172)
01-30 11:41:03.461: W/System.err(8577): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-30 11:41:03.461: W/System.err(8577): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-30 11:41:03.461: W/System.err(8577): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
01-30 11:41:03.461: W/System.err(8577): at android.view.View.measure(View.java:15172)
01-30 11:41:03.461: W/System.err(8577): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
01-30 11:41:03.464: W/System.err(8577): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
01-30 11:41:03.464: W/System.err(8577): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
01-30 11:41:03.464: W/System.err(8577): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-30 11:41:03.464: W/System.err(8577): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
01-30 11:41:03.464: W/System.err(8577): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-30 11:41:03.464: W/System.err(8577): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-30 11:41:03.464: W/System.err(8577): at android.view.Choreographer.doFrame(Choreographer.java:525)
01-30 11:41:03.464: W/System.err(8577): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-30 11:41:03.464: W/System.err(8577): at android.os.Handler.handleCallback(Handler.java:615)
01-30 11:41:03.464: W/System.err(8577): at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 11:41:03.464: W/System.err(8577): at android.os.Looper.loop(Looper.java:137)
01-30 11:41:03.468: W/System.err(8577): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-30 11:41:03.468: W/System.err(8577): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:41:03.468: W/System.err(8577): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:41:03.468: W/System.err(8577): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-30 11:41:03.468: W/System.err(8577): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-30 11:41:03.468: W/System.err(8577): at dalvik.system.NativeStart.main(Native Method)
编辑:这是我的新代码,我仍然有例外:
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date d = new Date();
d = sdf.parse(expire);
Log.i("ImageFetcher","d ="+d.getDay());
long timestamp = d.getTime();
return timestamp;
} catch (ParseException e) {
e.printStackTrace();
}