如何计算时间差?

static Calendar getCalendar(String time)
    {
        if (null == time)
        {
            return null;
        }
        String[] times = time.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.valueOf(times[0]), Integer.valueOf(times[1]), Integer.valueOf(times[2]));
        return calendar;
    }
   
    public static void main(String[] args)
    {
        String startTime1 = "2011-03-02";
        String startTime2 = "2011-03-17";
        Calendar c1 = getCalendar(startTime1);
        Calendar c2 = getCalendar(startTime2);
       
       
        System.out.println("相差天数:"
                + (c2.getTimeInMillis() - c1.getTimeInMillis())/1000*60*60*24;
}

你可能感兴趣的:(java基础)