1

我有这种类型的字符串格式的日期 01-18-2013 06:43:35 现在,我想将此日期与当前日期和时间进行比较,并得到日、月、小时、分钟、.. 我搜索了这个链接但没有'没有得到任何解决方案...请分享一些解决方案..谢谢..

4

4 回答 4

1

This might help, http://developer.android.com/reference/java/text/DateFormat.html

You can parse the Date from string using

DateFormat df = DateFormat.getDateInstance();
myDate = df.parse(myString);
于 2013-03-21T10:50:51.207 回答
1

If I get your question, you would like to Compare a Date object with the current date..

Let's say that 'date' is the Date object you want to compare with the current date: Why don't you just do something like date.after(new Date()) or date.before(new Date()) as suggested form the android doc?

于 2013-03-21T10:52:21.893 回答
1

这可以帮助您以毫秒为单位计算不同的日期时间

try{
            Calendar calender = Calendar.getInstance();
            Calendar calDb = Calendar.getInstance();
            Calendar matchd = Calendar.getInstance();

            mYear = calender.get(Calendar.YEAR);
            mMonth = calender.get(Calendar.MONTH);
            mDay = calender.get(Calendar.DAY_OF_MONTH);

            mendYear = calDb.get(Calendar.YEAR);
            mendMonth = calDb.get(Calendar.MONTH);
            mendDay = calDb.get(Calendar.DAY_OF_MONTH);    
                       // Here you can change day values        
                    calDb.set(Calendar.DAY_OF_MONTH, mDay-1);

            strbeforedate = mDateFormat.format(calDb.getTime());
            curentdate = mDateFormat.format(calender.getTime());

            calDb.setTime(mDateFormat.parse(strbeforedate));
            calender.setTime(mDateFormat.parse(curentdate));



            String mydate = "2013.03.14 03:11";
            String mdatetime = "";

            deletepath =  new ArrayList<String>();          
            for(int i=0;i<result.length;i++){

                try{
                    // here your matching goes and pass date here 

                     matchd.setTime(mDateFormat.parse(mdatetime));
                     long diff = calDb.getTimeInMillis() - calender.getTimeInMillis();
                     long matchdiff = matchd.getTimeInMillis() - calender.getTimeInMillis();
                        if(diff < matchdiff){

                                     // do your work here
                        }else{

                         // do your else work here

                        }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }


        }

        }catch(Exception e){
            e.printStackTrace();
        }

    }
于 2013-03-21T11:33:08.727 回答
1

你可以得到UTC

new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").parse("01-18-2013 06:43:35").getTime();

然后将结果与

System.currentTimeMillis();
于 2013-03-21T10:56:53.797 回答