---
title: "如何计算时间差？"
author: "liuyuru"
date: "2011-03-22T14:31:18Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-03-22T14:31:18Z"
id: 970530
url: "https://liuyuru.iteye.com/blog/970530"
---

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;  
}