java.text.ParseException:无法解析的日期:“2016年6月26日”(偏移0处)

时间:2016-04-26 06:02:49

标签: android datepicker

我有两个textview,我在textview的onclick监听器上使用单独的datepicker,我试图在所选日期之间取得差异,但我收到了以下错误

java.text.ParseException: Unparseable date: "26/4/2016" (at offset 0)

在这一行

Date past = format.parse(enddt);

以下是我的代码,任何人都可以帮助我

startdates=(TextView)findViewById(R.id.activity_entry_startdate);
        startdates.setOnClickListener(this);



        enddates=(TextView)findViewById(R.id.activity_entry_enddate);

        enddates.setOnClickListener(this);

就像这样我打开datepickers

public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {




        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
             startc = Calendar.getInstance();
            int year = startc.get(Calendar.YEAR);
            int month = startc.get(Calendar.MONTH);
            int day = startc.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
            //dialog.getDatePicker().setMaxDate(new Date().getTime());

            dialog.getDatePicker().setMaxDate(System.currentTimeMillis());

            return  dialog;
            //return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {



            startdates.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
            Log.d("month", String.valueOf(year));


        }
    }


    public static class DatePickerFragments extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {




        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
             endc = Calendar.getInstance();
            int year = endc.get(Calendar.YEAR);
            int month = endc.get(Calendar.MONTH);
            int day = endc.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
            dialog.getDatePicker().setMaxDate(System.currentTimeMillis());

            return  dialog;
            //return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {

            enddates.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
            Log.d("month", String.valueOf(year));

            difs();


        }
    }



    public static void difs()
    {


        try {
            SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                    Locale.ENGLISH);

            String enddt= String.valueOf(format.parse(enddates.getText().toString()));
            Date past = format.parse(enddt);
            Date now = new Date();

            System.out.println(TimeUnit.MILLISECONDS.toMillis(now.getTime() - past.getTime()) + " milliseconds ago");
            System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
            System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
            System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
        }
        catch (Exception j){
            j.printStackTrace();
        }

    }

3 个答案:

答案 0 :(得分:1)

您的Dateformat不正确,因此请更改此代码

DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
Date past = df.parse(enddt);

答案 1 :(得分:1)

您的格式为new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH),但您的日期为26/4/2016,与您的SimpleDateFormat完全不同。

使用"dd/MM/yyyy"代替"EE MMM dd HH:mm:ss z yyyy"

答案 2 :(得分:0)

您必须更改SimpleDateFormat方法中的difs()字符串。并且一定要使用正确的一个,因为有些人在MM显示这个月并不是绝对正确的。如果你有一个像26/4/2016的日期,你只需要使用一个M

public static void difs()
{


    try {


 //change here to dd/M/yyyy for a date like 26/4/2016
     SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy",
                Locale.ENGLISH);


        String enddt= String.valueOf(format.parse(enddates.getText().toString()));
        Date past = format.parse(enddt);
        Date now = new Date();

        System.out.println(TimeUnit.MILLISECONDS.toMillis(now.getTime() - past.getTime()) + " milliseconds ago");
        System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
        System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
        System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
    }
    catch (Exception j){
        j.printStackTrace();
    }

}

编辑:

你的问题和你的意见有点令人困惑和不同。有关解释,如果您有一个SimpleDateFormat字符串,如:

Tue Apr 26 00:00:00 EDT 2016

您的SimpleDateFormat必须如下所示:

SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyyy",Locale.ENGLISH);

这意味着您必须提供与SimpleDateFormat匹配的精确字符串进行解析。换句话说,传递正确String的示例是:

Date past = format.parse("Tue Apr 26 01:20:56 EDT 2016");

如果你想抓住date string喜欢

26/4/2016

您的SimpleDateFormat必须如下所示:

 SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy",
                    Locale.ENGLISH);

并且应该格式化的字符串必须如下所示:

Date past = format.parse("26/4/2016");

等等......因为这里的任何人都无法准确知道你传递的是什么,我建议阅读有关SimpleDateFormat的文档。这是API中易于理解和易懂的部分之一:

http://developer.android.com/reference/java/text/SimpleDateFormat.html