将Tue Dec 09 00:00:00 GMT + 04:00转换为yyyy-MM-dd android

时间:2015-03-06 07:45:42

标签: android android-calendar android-date

以下代码生成error Caused by: java.lang.NullPointerException        在java.util.Calendar.setTime。这是转换的正确方式吗? 使用"Tue Dec 09 00:00:00 GMT+04:00 2014"

yyyy-MM-ddsimpledateformat android
SimpleDateFormat parseFormat = 
            new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

    Date date = null;

    try {

        date = parseFormat.parse(dateString);

        System.out.println("Cresult_pass: "+date.toString());
        SimpleDateFormat cformat = new SimpleDateFormat("yyyy-MM-dd");
        String result = cformat.format(date);
        System.out.println("Cresult:result "+result);

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

日志:

03-06 12:11:11.420: W/System.err(9478): java.text.ParseException: Unparseable date: "Tue Dec 09 00:00:00 GMT+04:00 2014" (at offset 0)
03-06 12:11:11.455: W/System.err(9478):     at java.text.DateFormat.parse(DateFormat.java:555)
03-06 12:11:11.455: W/System.err(9478):     at trail.kreol.view.PalmaresFragment.revertDate(FicheCoursePalmaresFragment.java:296)
03-06 12:11:11.455: W/System.err(9478):     at trail.kreol.view.PalmaresFragment.filteredList(FicheCoursePalmaresFragment.java:230)
03-06 12:11:11.455: W/System.err(9478):     at trail.kreol.view.PalmaresFragment.onCreateView(FicheCoursePalmaresFragment.java:137)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
03-06 12:11:11.455: W/System.err(9478):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
03-06 12:11:11.455: W/System.err(9478):     at android.view.View.measure(View.java:16831)
03-06 12:11:11.455: W/System.err(9478):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
03-06 12:11:11.455: W/System.err(9478):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
03-06 12:11:11.455: W/System.err(9478):     at android.view.View.measure(View.java:16831)

2 个答案:

答案 0 :(得分:1)

默认情况下,

SimpleDateFormat使用默认语言环境来解析工作日(或月份,时代,上午/下午)。

使用

SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

如果您想应用特定的区域设置。

答案 1 :(得分:0)

我现在无法测试它,但你已经忘记了你所在时区之后的几个小时。您将SimpleDateFormat设置如下:

SimpleDateFormat parseFormat = 
        new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

并尝试使用以下内容解析日期:

Tue Dec 09 00:00:00 GMT+04:00 2014

所以我想,你刚忘了时间。将您的格式parseFormat的字符串更改为:

SimpleDateFormat parseFormat = 
        new SimpleDateFormat("EEE MMM dd HH:mm:ss z HH:mm yyyy"); //put HH:mm into the format

所以,如果它没有修复它,请播放一点,因为它可能是字符串与我的例子略有不同。但是,确切地说,你已经忘记了时区之后的几个小时。 另一种可能的解决方案,你必须在dateFormat中使用4个大写Z:

     SimpleDateFormat parseFormat = 
        new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");