我有这段代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%@page import="java.text.*,org.joda.time.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try {
int freq=30; //30 minutes
String d1=request.getParameter("date");
String d2=d1;
String t1=request.getParameter("starttime");
String t2=request.getParameter("endtime");
String date1 = d1;
String time1 = t1;
String date2 = d2;
String time2 = t2;
String format = "dd/MM/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(date1 + " " + time1);
Date dateObj2 = sdf.parse(date2 + " " + time2);
DateTime dateObj12 = new DateTime(dateObj1);
DateTime dateObj22 = new DateTime(dateObj2);
long diff = dateObj2.getTime() - dateObj1.getTime();
double diffInHours = diff / ((double) 1000 * 60 * 60);
//out.println(diffInHours+" hours"+ "<br>");
final Period period = new Period(dateObj12, dateObj22);
//out.print(period.getDays() + " days, ");
out.print(period.getHours() + " hours, ");
out.print(period.getMinutes() + " minutes, ");
//out.print(period.getSeconds() + " seconds.");
}
catch (ParseException e) {
e.printStackTrace();
}
%>
</body>
</html>
这给了我几小时的差异。
接下来我想得到间隔的数量。
如上所述,您可以看到我使用了freq=30;
这是我想要的间隔
所以在一定时间之间,例如上午11点30分到下午5点,间隔时间为30分钟,我应该得到11个间隔。
我应该怎么做?
答案 0 :(得分:0)
int minutesBetween = Minutes.minutesBetween(dateObj12, dateObj22).getMinutes();
int numberOfIntervals = minutesBetween / freq;
Javadoc:http://joda-time.sourceforge.net/apidocs/org/joda/time/Minutes.html