Date date1 = new Date(20100101);
Date date2 = new Date(20100211);
long timediff = date1.getTime()-date2.getTime(); timediff/3600000/24
为什么结果会是10呢然后Date date1 = new Date(20100101);
Date date2 = new Date(20100111);
long timediff = date1.getTime()-date2.getTime(); timediff/3600000/24 结果也是10
Date date2 = new Date(20100211);
long timediff = date1.getTime()-date2.getTime(); timediff/3600000/24
为什么结果会是10呢然后Date date1 = new Date(20100101);
Date date2 = new Date(20100111);
long timediff = date1.getTime()-date2.getTime(); timediff/3600000/24 结果也是10
select (date1 - date2 + 1) from dual
Calendar c1 = Calendar.getInstance();
c1.set(2010,00,01);
Calendar c2 = Calendar.getInstance();
c2.set(2010,01,11); System.out.println(Math.ceil((c2.getTimeInMillis()-c1.getTimeInMillis())/(1000*60*60*24.0)));
cal = null;
fastTime = date;
}public long getTime(){
return getTimeImpl();
}public final long getTimeImpl(){
return (cal == null) ? fastTime : cal.getTimeInMillis();
}也就是说你调用getTime()方法时,实际返回的就是你传的long型参数20100101和20100211
import java.util.Date;
import java.text.SimpleDateFormat;public class Test
{
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date1 = sdf.parse("20100101");
Date date2 = sdf.parse("20100211");
long timediff = date2.getTime()-date1.getTime(); System.out.println(timediff/3600000/24);
}
}
start = DateUtil.getDate(applystartdate);
end = DateUtil.getDate(applyenddate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc1.setTime(start);
gc2.setTime(end);
//取两个日期间的间隔天数
int count = DateUtil.getDays(gc1, gc2);这样做绝对正确
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateUtil {
// 一天的毫秒数 60*60*1000*24
private final static long DAY_MILLIS = 86400000; // 一小时的毫秒数 60*60*1000
private final static long HOUR_MILLIS = 3600000; // 一分钟的毫秒数 60*1000
private final static long MINUTE_MILLIS = 60000;
//日期格式化串
private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception{
// 开始时间
Date startdate = DATE_FORMAT.parse("2010-01-01 08:01:00");
// 结束时间
Date enddate = DATE_FORMAT.parse("2010-01-03 00:00:00"); showTimedif(startdate, enddate); Calendar start = Calendar.getInstance();
start.setTime(startdate); Calendar end = Calendar.getInstance();
end.setTime(enddate); showTimedif(start, end);
} public static void showTimedif(Date start, Date end) {
show(start.getTime(),end.getTime());
} public static void showTimedif(Calendar start, Calendar end) {
show(start.getTimeInMillis(),end.getTimeInMillis());
} /**
* 计算两个日期相差的天、小时、分钟
*
* @param start
* @param end
*/
public static void show(long start, long end) {
long temp = end - start;
System.out.print(DATE_FORMAT.format(new Date(start)) + " 与 " + DATE_FORMAT.format(new Date(end)));
System.out.println("相差" + temp / DAY_MILLIS + "天" +
(temp % DAY_MILLIS)/ HOUR_MILLIS + "小时" +
((temp % DAY_MILLIS) % HOUR_MILLIS)/ MINUTE_MILLIS + "分");
}
}
输出结果:
2010-01-01 08:01:00 与 2010-01-03 00:00:00相差1天15小时59分
2010-01-01 08:01:00 与 2010-01-03 00:00:00相差1天15小时59分
* 计算2个日期之间的天数
* @param beginTime
* @param endTime
* @return 相差的天数
* @throws BusinessException
*/
public long getDate(String beginTime,String endTime) throws BusinessException{
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
try {
Date begin = s.parse(beginTime);
Date end = s.parse(endTime);
long time = (end.getTime()-begin.getTime())/1000/60/60/24;
return time;
} catch (Exception e) {
throw new BusinessException(e);
}
}
Date date1 = new Date(20100101);用这么这样也好啊
public Date(int year,
int month,
int date)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateUtil {
// 一天的毫秒数 60*60*1000*24
private final static long DAY_MILLIS = 86400000; // 一小时的毫秒数 60*60*1000
private final static long HOUR_MILLIS = 3600000; // 一分钟的毫秒数 60*1000
private final static long MINUTE_MILLIS = 60000;
//日期格式化串
private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception{
// 开始时间
Date startdate = DATE_FORMAT.parse("2010-01-01 08:01:00");
// 结束时间
Date enddate = DATE_FORMAT.parse("2010-01-03 00:00:00"); showTimedif(startdate, enddate); Calendar start = Calendar.getInstance();
start.setTime(startdate); Calendar end = Calendar.getInstance();
end.setTime(enddate); showTimedif(start, end);
} public static void showTimedif(Date start, Date end) {
show(start.getTime(),end.getTime());
} public static void showTimedif(Calendar start, Calendar end) {
show(start.getTimeInMillis(),end.getTimeInMillis());
} /**
* 计算两个日期相差的天、小时、分钟
*
* @param start
* @param end
*/
public static void show(long start, long end) {
long temp = end - start;
System.out.print(DATE_FORMAT.format(new Date(start)) + " 与 " + DATE_FORMAT.format(new Date(end)));
System.out.println("相差" + temp / DAY_MILLIS + "天" +
(temp % DAY_MILLIS)/ HOUR_MILLIS + "小时" +
((temp % DAY_MILLIS) % HOUR_MILLIS)/ MINUTE_MILLIS + "分");
}
}的运行结果为什么是这个啊?--------------------Configuration: <Default>--------------------
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrumentProcess completed.
* 返回同一年内两个日期相差的天数
*
* @param date1
* 日期1
* @param date2
* 日期2
*
* @return 相差的天数
* @throws ParseException
*/
public static int getIntervalOfDate(String date1, String date2)
throws ParseException {
int interval = 0;
// 设置日期
Calendar cal1 = Calendar.getInstance();
cal1.setTime(parseDateTimeByFormatstr(date1, "yyyy-MM-dd")); Calendar cal2 = Calendar.getInstance();
cal2.setTime(parseDateTimeByFormatstr(date2, "yyyy-MM-dd")); // 相差的天数
interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR)- cal2.get(Calendar.DAY_OF_YEAR)); return interval;
}
* 返回同一年内两个日期相差的天数
* @param date1
* 日期1
* @param date2
* 日期2
* @return 相差的天数
* @throws ParseException
*/
public static int getIntervalOfDate(String date1, String date2)
throws ParseException {
int interval = 0;
// 设置日期
Calendar cal1 = Calendar.getInstance();
cal1.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date1)); Calendar cal2 = Calendar.getInstance();
cal2.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date2)); // 相差的天数
interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR)- cal2.get(Calendar.DAY_OF_YEAR));
return interval;
}
201
*
* 类名称: Test.java
* @Title: differDays
* @Description:计算两个日期相隔多少天?
* @param date
* @return long
* @author
* @date Jun 14, 2011
*/
public long differDays(String date)
{
long differDays = 0;
long DAY = 24L * 60L * 60L * 1000L;
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
Date todayDate = new java.sql.Date(new Date().getTime());
Date paraDate = null;
try {
paraDate = df.parse( date );
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
differDays = ( todayDate.getTime() - paraDate.getTime() ) / DAY ;
return differDays;
}