package selleck.util.mail;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
public class MailConstant {
//时间格式刷
public static SimpleDateFormat sde = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//生成的文件路径
public static String logPath = new String(ReadProperty.get("logPath"));
//获取时间差
public static void dateDiff(String desc,String startTime, String endTime) {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long between = 0;
try {
java.util.Date begin = dfs.parse(startTime);
java.util.Date end = dfs.parse(endTime);
between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
long day = between / (24 * 60 * 60 * 1000);
long hour = (between / (60 * 60 * 1000) - day * 24);
long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
MailConstant.WriterLine(desc+" 开始时间:"+startTime+" 结束时间:"+endTime);
MailConstant.WriterLine(desc+" 总该耗时:"+day + "天" + hour + "小时" + min + "分" + s + "秒");
} catch (Exception ex) {
ex.printStackTrace();
}
}
//比较两个时间是否存在交集
public static boolean isInDates(String times){
int strDateBeginH = Integer.parseInt(times.split("-")[0].split(":")[0]);
int strDateBeginM = Integer.parseInt(times.split("-")[0].split(":")[1]);
int beginM = strDateBeginH*3600+strDateBeginM*60;
//System.out.println("开始时间 "+strDateBeginH+":"+strDateBeginM+" "+beginM);
Date dt = chanageDate();
int strDateH = dt.getHours();
int strDateM = dt.getMinutes();
int thisM = strDateH*3600+strDateM*60;
//System.out.println("当前时间 "+strDateH+":"+strDateM+" "+thisM);
int strDateEndH = Integer.parseInt(times.split("-")[1].split(":")[0]);
int strDateEndM = Integer.parseInt(times.split("-")[1].split(":")[1]);
int endM = strDateEndH*3600+strDateEndM*60;
//System.out.println("结束时间 "+strDateEndH+":"+strDateEndM+" "+endM);
//System.out.println("比较实际 ["+beginM+" "+thisM+" "+endM+"] "+(thisM>= beginM && thisM <= endM));
if(thisM>= beginM && thisM <= endM){
return true;
}else{
return false;
}
}
//比较两个时间是否存在交集
public static boolean isInDates_old(String times){
Date dt = new Date();
int strDateH = dt.getHours();
int strDateM = dt.getMinutes();
if(strDateH == 0 && strDateM == 0){
strDateH = 23;strDateM = 59;
}else if(strDateH == 0){
strDateH = 24;
}else if(strDateM == 0){
strDateH = strDateH-1;strDateM = 59;
}
int strDateBeginH = Integer.parseInt(times.split("-")[0].split(":")[0]);
int strDateBeginM = Integer.parseInt(times.split("-")[0].split(":")[1]);
int strDateEndH = Integer.parseInt(times.split("-")[1].split(":")[0]);
int strDateEndM = Integer.parseInt(times.split("-")[1].split(":")[1]);
if(strDateEndH == 0 && strDateEndM == 0){
strDateEndH = 23;strDateEndM = 59;
}else if(strDateEndH == 0){
strDateEndH = 24;
}else if(strDateEndM == 0){
strDateEndH = strDateEndH-1;strDateEndM = 59;
}
if((strDateH>=strDateBeginH && strDateM>=strDateBeginM && strDateH<=strDateEndH && strDateM<=strDateEndM)){
return true;
}else{
return false;
}
}
//互补时间差
public static Date chanageDate(){
Date date = new Date();
//由于本地和服务器相差6小时,而程序中需要按照特定时间段进行判断,需要进行时间处理
//date.setHours(date.getHours()+18);
return date;
}
//写入html文件内容
public static String setLocalHtmlStream(String path,String html) {
//System.out.println(" === "+path);
FileWriter fw;
try {
File file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fw = new FileWriter(path); // "C://Users//Administrator//Desktop//demo//"+System.currentTimeMillis()+".html"
fw.write(html);
fw.flush();
fw.close();
} catch (Exception e) {}
return html;
}
public static File file = null;
//添加Log日志
public static void WriterLine(String val){
try {
String path = MailConstant.logPath+"//"+MailConstant.sds.format(MailConstant.chanageDate())+".txt";
if(file == null){
file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
FileWriter fw = new FileWriter(path,true);
PrintWriter pw = new PrintWriter(fw);
if("".equals(val)){
System.out.println("");
pw.println("\r");
}else{
System.out.println("["+sd.format(chanageDate())+"]"+val);
pw.println("["+sd.format(chanageDate())+"]"+val+"\r");
}
pw.flush();
fw.flush();
pw.close();
fw.close();
} catch (Exception e) {System.out.println(" Error ######################## Error "+e.getMessage());}
}
}