java 解决时间相减问题

比如 Date currentTime ="2011-06-20 9:23:50"; Date endTime="2011-06-21 10:33:56"; 
要求的结果是 endTime - currentTime = 还剩余1天1小时10分6秒;如何来实现这个功能??

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class MainActivity {

    /*public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] A = { 1,8,2,5,4,3,6 };
        int[] B = Insertion.sort(A);
        print(B);
        
    }
    
    static void print(int[] arr) {
        for(int i=0; i<arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }*/
    
    public static void main(String[] args) throws Exception{
         /* Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2015-04-24 12:14:57"); 
          Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse("2015-03-25 12:14:57");
          Calendar cal1=Calendar.getInstance();
          Calendar cal2=Calendar.getInstance();
          cal1.setTime(date1);
          cal2.setTime(date2);
          
          System.out.println(date1.compareTo(date2));
          System.out.println(cal1.compareTo(cal2));
          String now = new SimpleDateFormat("yyyy年MM月dd日").format(date1);
          System.out.println(now);*/
        
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           java.util.Date now = df.parse("2015-04-24 12:14:57");
           java.util.Date date=df.parse("2015-03-25 12:14:57");
           long l=now.getTime()-date.getTime();
           long day=l/(24*60*60*1000);
           long hour=(l/(60*60*1000)-day*24);
           long min=((l/(60*1000))-day*24*60-hour*60);
           long s=(l/1000-day*24*60*60-hour*60*60-min*60);
           System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
         }
    
    

}

 

如何利用java得到当前的时间和前一天的时间

博客分类: 常用资料
JavaRubyCC++C# 
1.得到当前的时间 
Ruby代码  收藏代码
Calendar   cal   =   Calendar.getInstance();   
String time=formatter.format(cal.getTime());  
String strEnd=time.substring(0, 16);// 2007-10-30 09:30  

2.得到前一天的时间 
Ruby代码  收藏代码
Calendar   c   =   Calendar.getInstance();   
c.add(Calendar.DAY_OF_MONTH, -1);  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
String mDateTime=formatter.format(c.getTime());  
String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30  

3.得到前几天的时间 
Ruby代码  收藏代码
Calendar   c   =   Calendar.getInstance();   
c.add(Calendar.DAY_OF_MONTH, -5);//得到前5  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
String mDateTime=formatter.format(c.getTime());  
String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30  

4.得到当前时间的前几年的时间 

Ruby代码  收藏代码
Calendar   c   =   Calendar.getInstance();   
  c.add(Calendar.YEAR, -5);//得到前5年  
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  String mDateTime=formatter.format(c.getTime());  
  String strStart=mDateTime.substring(0, 16);//2002-10-24 09:30  

 

CalendarDate转化

(1) Calendar转化Date
Calendar cal=Calendar.getInstance();
Date date=cal.getTime();

(2) Date转化Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);

 




posted @ 2015-04-24 17:16  疾风剑  阅读(7210)  评论(0编辑  收藏  举报