如何找到android中字符串格式的两个日期之间的差异

时间:2013-02-15 09:35:12

标签: android datetime

Calendar zCalendarL = Calendar.getInstance(); 
SimpleDateFormat zDateFormatL = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

String DateTime="2013/02/15 15:05:01";
String zUpdatedDateTimeG =zDateFormatL.format(DateTime);

String zCurrentDateTimeL=zDateFormatL.format(zCalendarL.getTime());

我需要知道zCurrentDateTimeL和zUpdatedDateTimeG之间的日期时间

怎么做?

2 个答案:

答案 0 :(得分:1)

试试这个: 我希望这对你有所帮助,这对我有用......

        private void getDifference(java.util.Date startTime,
            java.util.Date endTime, java.util.Date eartime,
            java.util.Date curtime) {

        Calendar startDateTime = Calendar.getInstance(TimeZone
                .getTimeZone("GMT-5:00"));
        startDateTime.setTime(startTime);
        Calendar endDateTime = Calendar.getInstance();
        endDateTime.setTime(endTime);           
        long milliseconds1 = startDateTime.getTimeInMillis();
        long milliseconds2 = endDateTime.getTimeInMillis();
        long diff = Math.abs(milliseconds1 - milliseconds2);
        long seconds = diff / (1000);

              }

答案 1 :(得分:1)

Calendar zCalendarL = Calendar.getInstance(); 
SimpleDateFormat zDateFormatL = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

String DateTime="2013/02/15 15:05:01";

Date d = zDateFormatL.parse(DateTime);
long diff = System.currentTimeMillis() - d.getTime();

这将返回以毫秒为单位的差异