标签:
比如 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+"秒"); } }
标签:
原文地址:http://www.cnblogs.com/hixin/p/4454021.html