我一直在尝试将字符串转换为日期超过两个小时。 我不明白我做错了什么?
这是我收到的字符串日期=“星期四,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();
}
我总是得到一个例外,我想我的简单日期格式是错误的。 任何可以帮助。
我的logcat:
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();
}
答案 0 :(得分:2)
使用此代码可以帮助您
private String getFormatedTime(String someDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss");
Date date = sdf.parse(someDate);
sdf.applyPattern("h:mm a");
String formatedDate = sdf.format(date);
return formatedDate;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
答案 1 :(得分:0)
我认为你错过了一个yy和Z.
所以你需要
SimpleDateFormat sdf = new SimpleDateFormat(“EEE,dd MMM yyyy hh:mm:ss Z”);
你可能不需要Z因为下一行代码,但你肯定需要4年代。
答案 2 :(得分:0)
试试如下:
从字符串到日期
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
从日期到字符串 ..
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = new Date();
String datetime = dateFormat.format(date);
System.out.println("Current Date Time : " + datetime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 3 :(得分:0)
尝试:
DateFormat df = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss");
try {
Date today = df.parse("Thu, 31 Jan 2013 10:48:58 GMT");
System.out.println("Converted Date = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
答案 4 :(得分:0)
在SimpleDateFormat构造函数中更改模式 - 使用yyyy,而不是yy。
答案 5 :(得分:0)
试试这段代码:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss");
String Todtime = df.format(c.getTime());
System.out.println("Time: "+Todtime);
try {
Date convertedDate = df.parse(Todtime);
System.out.println("Time: "+convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}