get Differences between two date value in string - Android java.util

Android examples for java.util:Date Compare

Description

get Differences between two date value in string

Demo Code

import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Main{

    public static long getDifferences(String dateStr1, String dateStr2) {
        long interval = 0; 
        String format = "yyyy-MM-dd hh:mm:ss";
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(timeZone);//from   ww  w  .j ava 2 s  . c o  m
        try {
            long date1 = sdf.parse(dateStr1).getTime();
            long date2 = sdf.parse(dateStr2).getTime();
            interval = Math.abs(date1 - date2);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return interval;
    }

}

Related Tutorials