0

我已经尝试了两个多小时将字符串转换为日期。我不明白我做错了什么?

这是我收到的字符串日期=“星期四,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();
            }
4

6 回答 6

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;
    }
}
于 2013-01-30T11:02:42.447 回答
0

我认为您缺少 yy 和 Z。

所以你需要

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");

由于下一行代码,您可能不需要 Z,但您肯定需要 4 y。

于 2013-01-30T10:55:44.393 回答
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();  
}
于 2013-01-30T10:56:45.010 回答
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();
    } 
于 2013-01-30T11:11:16.357 回答
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();
        }
于 2013-01-30T11:00:28.520 回答
0

在 SimpleDateFormat 构造函数中更改您的模式 - 使用 yyyy,而不是 yy。

于 2013-01-30T11:03:32.447 回答