import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DayTest {
public static void main(String[] args){
String BeginDate = "2014-03-27 13:16:00.000";
String EndDate = "2014-03-28 11:46:00.000";
double day = getDay(BeginDate,EndDate);
System.out.println("天数:"+day);
}
private static double getDay(String beginDate, String endDate) {
SimpleDateFormat from = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置
//String times = from.format(beginDate);
double JSHH = 0;
double JSDay = 0;
double day = getDD(beginDate,endDate);
System.out.println(day);
if(day < 1){
JSHH = getHH(beginDate,endDate);
System.out.println("JSHH = "+JSHH);
if(JSHH%60 != 0){
System.out.println("JSHH/60 = "+JSHH/60);
int a = (int) (JSHH / 60);
System.out.println("a = "+a);
JSHH = a + 0.5;
JSDay = JSHH / 8;
return JSDay;
}else{
JSHH = JSHH / 60 ;
JSDay = JSHH / 8;
return JSDay;
}
}else{
day = getDD(beginDate,endDate);
System.out.println("day:"+day);
JSHH = getHH(getAddDay(beginDate,day),endDate);
System.out.println("JSHH = "+JSHH);
if(JSHH%60 != 0){
System.out.println("JSHH/60 = "+JSHH/60);
int a = (int) (JSHH / 60);
System.out.println("a = "+a);
JSHH = a + 0.5;
}else{
JSHH = JSHH / 60 ;
}
System.out.println("JSHH ="+JSHH);
JSHH = JSHH / 8;
JSDay = day + JSHH;
return JSDay;
}
}
public static double getDD(String time1, String time2){
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = ft.parse( time1 );
Date date2 = ft.parse( time2 );
quot = date2.getTime() - date1.getTime();
quot = quot / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
e.printStackTrace();
}
return quot;
}
public static double getHH(String time1, String time2){
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置
StringBuffer T = new StringBuffer();
T.append(time1.substring(0, 10));
SimpleDateFormat ftHH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置
try {
System.out.println("time1:"+time1);
Date date1 = ft.parse( time1 );
Date date2 = ft.parse( time2 );
Date DateHH1 = ftHH.parse(time1);
Date DateHH2 = ftHH.parse(time2);
System.out.println("DateHH1:"+DateHH1);
System.out.println("DateHH2:"+DateHH2);
if(DateHH1.getTime() >= ftHH.parse(T.toString()+" 8:30:00").getTime() &&
DateHH1.getTime() <= ftHH.parse(T.toString()+" 11:45:00").getTime() &&
DateHH2.getTime() >= ftHH.parse(T.toString()+" 13:15:00").getTime() &&
DateHH2.getTime() <= ftHH.parse(T.toString()+" 18:00:00").getTime()){
System.out.println("执行的是方法一 "+ftHH.parse(T.toString()+" 11:45:00").getTime() +" " + date1.getTime());
quot = ftHH.parse(T.toString()+" 11:45:00").getTime() - date1.getTime()+(date2.getTime()-ftHH.parse(T.toString()+" 13:15:00").getTime());
quot = quot / 1000 / 60 ;
System.out.println("quot = "+quot);
return quot;
}
else if(DateHH1.getTime() >= ftHH.parse(T.toString()+" 8:30:00").getTime() &&
DateHH1.getTime() <= ftHH.parse(T.toString()+" 11:45:00").getTime() &&
DateHH2.getTime() <= ftHH.parse(T.toString()+" 11:45:00").getTime() &&
DateHH2.getTime()>DateHH1.getTime()){
quot = DateHH2.getTime() - date1.getTime();
quot = quot / 1000 / 60 ;
return quot;
}
else if(DateHH1.getTime() >= ftHH.parse(T.toString()+" 13:15:00").getTime() &&
DateHH1.getTime() <= ftHH.parse(T.toString()+" 18:00:00").getTime() &&
DateHH2.getTime() <= ftHH.parse(T.toString()+" 18:00:00").getTime() &&
DateHH2.getTime()>DateHH1.getTime()){
quot = DateHH2.getTime() - date1.getTime();
quot = quot / 1000 / 60 ;
return quot;
}
else if(DateHH1.getTime() >= ftHH.parse(T.toString()+" 13:15:00").getTime() &&
DateHH1.getTime() <= ftHH.parse(T.toString()+" 18:00:00").getTime() &&
DateHH2.getTime() <= ftHH.parse(T.toString()+" 11:45:00").getTime()){
quot = ftHH.parse(T.toString()+" 18:00:00").getTime() - date1.getTime() + (date2.getTime()-ftHH.parse(T.toString()+" 8:30:00").getTime());
if(ftHH.parse(T.toString()+" 13:15:00").getTime() == DateHH1.getTime())
{
quot = 0;
}else{
quot = quot / 1000 / 60;
}
System.out.println("quot = "+ quot);
return quot;
}
} catch (ParseException e) {
e.printStackTrace();
}
return quot;
}
public static String getAddDay(String date, double day) {
SimpleDateFormat from = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置
Calendar startDT = Calendar.getInstance();
startDT.setTime(getDate(date));
startDT.add(Calendar.DAY_OF_MONTH,(int) day);
return from.format(startDT.getTime());
}
public static Date getDate(String date){
SimpleDateFormat from = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); //这里的格式可以自己设置
long t;
try {
t = from.parse(date).getTime();
Date d = new Date(t);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}