现写的,没有完全实现你的功能,可以参考
package com.ljh.date;import java.util.Date;
import java.text.SimpleDateFormat;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author liu
* @version 1.0
*/
public class DateFunction {
public DateFunction() {
} public long fromDateStringToLong(String inVal) {
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
} public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
} public static void main(String[] args) {
// 2005-03-28 14:50:23 - 2004-10-21 14:17:03
DateFunction df = new DateFunction();
long startT=df.fromDateStringToLong("2005-03-03 14:51:23");
long endT=df.fromDateStringToLong("2004-03-03 13:50:23");
// long fin=df.fromDateStringToLong("1970-01-01 0:00");
// System.out.println(startT+"=="+endT+"=="+(startT-endT)+"=="+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("共"+day+"天 准确时间是:小时="+hor+" 分钟"+secd );
}
}
package com.ljh.date;import java.util.Date;
import java.text.SimpleDateFormat;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author liu
* @version 1.0
*/
public class DateFunction {
public DateFunction() {
} public long fromDateStringToLong(String inVal) {
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
} public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
} public static void main(String[] args) {
// 2005-03-28 14:50:23 - 2004-10-21 14:17:03
DateFunction df = new DateFunction();
long startT=df.fromDateStringToLong("2005-03-03 14:51:23");
long endT=df.fromDateStringToLong("2004-03-03 13:50:23");
// long fin=df.fromDateStringToLong("1970-01-01 0:00");
// System.out.println(startT+"=="+endT+"=="+(startT-endT)+"=="+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("共"+day+"天 准确时间是:小时="+hor+" 分钟"+secd );
}
}
MM和mm不一样。SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long seconds = (df.parse(sdt1).getTime()- df.parse(sdt2).getTime())/1000;
转成秒后再换算年时分秒...
用getTime()可以得到毫秒数。
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
try{
Date dt1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2005-04-11 16:47:30");
Date dt2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2005-04-11 17:47:30");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long seconds = (dt1.getTime()- dt2.getTime())/1000; //相差的天数
long date = seconds/(24*60*60); //相差的天数
long hour = (seconds-date*24*60*60)/(60*60); //相差的小时数
long minut = (seconds-date*24*60*60-hour*60*60)/(60); //相差的分钟数
long second = (seconds-date*24*60*60-hour*60*60-minut*60); //相差的秒数
System.out.println(date);
System.out.println(hour);
System.out.println(minut);
System.out.println(second);
}catch(Exception e){e.printStackTrace();}
//加减天数
calendar.add(Calendar.DAY_OF_MONTH, -3 );
Date Days = calendar.getTime();
//转换成年月日的形式
SimpleDateFormat DateTime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String sDateTime = DateTime.format(Days);