概述
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalculateIntervalBetween2Dates {
private static final long DAY=24*60*60*1000;
private static final long HOUR=60*60*1000;
private static final long MINUTES=60*1000;
private static final long SECONDS=1000;
private static final long MILLIONSECONDS=1;
public static void main(String[] args) {
String startDateStr = "20121022";
String endDateStr = "20121206";
CalculateIntervalBetween2Dates.listNumberOfDates(startDateStr, endDateStr);
CalculateIntervalBetween2Dates.calculateInterval(startDateStr, endDateStr,MILLIONSECONDS);
CalculateIntervalBetween2Dates.calculateInterval(startDateStr, endDateStr,SECONDS);
CalculateIntervalBetween2Dates.calculateInterval(startDateStr, endDateStr,MINUTES);
CalculateIntervalBetween2Dates.calculateInterval(startDateStr, endDateStr,HOUR);
CalculateIntervalBetween2Dates.calculateInterval(startDateStr, endDateStr,DAY);
}
/**
* List dates between start date and end date, including the start date and end date.
* @param startDateStr
* @param endDateStr
*/
public static void listNumberOfDates(String startDateStr,String endDateStr){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Date startDate = null;
Date endDate = null;
try {
startDate = sf.parse(startDateStr);
endDate = sf.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c1 = Calendar.getInstance();
List<Date> cancelDates = new ArrayList<Date>();
if (startDate != null && endDate != null && startDate.before(endDate)) {
Date tempDate = startDate;
do {
cancelDates.add(tempDate);
c1.setTime(tempDate);
c1.add(Calendar.DAY_OF_MONTH, 1);
tempDate = c1.getTime();
} while (tempDate.before(endDate));
cancelDates.add(endDate);
}
System.out.println("There are " + cancelDates.size() + " days between " + sf.format(startDate) + " and " + sf.format(endDate) + ", inclusive.");
System.out.println("See the following list:");
for (Date date : cancelDates) {
System.out.println(sf.format(date));
}
System.out.println();
}
/**
* Calculate how many days/hours/minutes/seconds/million seconds between start date and end date, including the start date and end date.
* @param startDateStr
* @param endDateStr
* @param interval
*/
public static void calculateInterval(String startDateStr,String endDateStr,long interval){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Date startDate = null;
Date endDate = null;
try {
startDate = sf.parse(startDateStr);
endDate = sf.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c1 = Calendar.getInstance();
c1.setTime(startDate);
long startInMillis=c1.getTimeInMillis();
c1.setTime(endDate);
//inclusive,2009/10/24-2009/10/23=1,but actually 2 days
c1.add(Calendar.DAY_OF_MONTH, 1);
long endInMillis=c1.getTimeInMillis();
long between=endInMillis-startInMillis;
System.out.println("There are " + (between/interval) +" "+getIntervalLabel(interval) + " between " + sf.format(startDate) + " and " + sf.format(endDate) + ", inclusive");
}
private static String getIntervalLabel(long interval){
if(interval==DAY){
return "days";
}else if(interval==HOUR){
return "hours";
}else if(interval==MINUTES){
return "minutes";
}else if(interval==SECONDS){
return "seconds";
}else if(interval==MILLIONSECONDS){
return "million seconds";
}else{
return "error";
}
}
}
最后
以上就是昏睡高跟鞋为你收集整理的计算两个日期之间有多少天多少小时多少分钟的全部内容,希望文章能够帮你解决计算两个日期之间有多少天多少小时多少分钟所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复