JAVA中日期计算

时间:2021-07-15 23:58:21
package com.sf.ccsp.exp.core.common.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


import com.sf.framework.utils.DateFormatUtils;


public class DateUtil {


public static final String pattern_24_type = "yyyy-MM-dd HH:mm:ss";


public static final String pattern_12_type = "yyyy-MM-dd HH:mm:ss";


/**
* 获取当前时间(字符串)

* @param pattern
*            时间格式
* @return
*/
public static String getCurrent24Time() {
return DateFormatUtils.format(new Date(), pattern_24_type);
}


public static Date format24Date(String dateStr) {
return DateFormatUtils.parseDate(dateStr, pattern_24_type);
}


/**
* 判断时间格式是否为 yyyy-MM-dd HH:mm:ss

* @param dateStr
* @return
* @throws ParseException
*/
public static boolean check24DateStr(String dateStr) throws ParseException {
return checkDateStrFormat(dateStr, pattern_24_type);
}


/**
* 校验时间格式

* @param dateStr
*            时间字符串
* @param pattern
*            格式
* @return
* @throws ParseException
*/
public static boolean checkDateStrFormat(String dateStr, String pattern) throws ParseException {
DateFormat formatter = new SimpleDateFormat(pattern);
Date date = (Date) formatter.parse(dateStr);
return dateStr.equals(getDateStr(date, pattern));
}


/**
* 把日期格式化成一字符串

* @param date
* @param fmt
* @return
*/
public static String getDateStr(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}


/**
* 比较两个日期的大小

* @param date1
* @param date2
* @return date1小于date2 返回-1,date1大于date2返回1,相等返回0
*/
public static int compareDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// different date might have different offset
cal1.setTime(date1);
long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);


cal2.setTime(date2);
long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);


long dateDiff = ldate1 - ldate2;
return dateDiff > 0 ? 1 : (dateDiff == 0 ? 0 : -1);
}


/**
* 获取开始时间 和结束时间
* @return
*/
public static String getCreateDate(){

        String msg="";  
        try {
Date date = new Date();  
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");  
msg+="["+sdf.format(date)+"]";
} catch (Exception e) {
e.printStackTrace();
}  
          
return msg;
}

/**
* 获取 一个主的序列号
* @return
*/
public static String getNo(){

        Random random = new Random();  
        
        Date date = new Date();  
        
   SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");  
  
        int rannum = (int) (random.nextDouble() * (99 - 10 + 1)) + 10;// 获取5位随机数  
  
        return "CX"+sdf.format(date)+rannum;// 当前时间  
}

/**
* 获取id
* @return
*/
public static String getOperationLogId(){
try {
Date date = new Date();  
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSSS");  
Random random = new Random();  
int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
return "CX"+sdf.format(date)+rannum;
} catch (Exception e) {
e.printStackTrace();
}

        return Uuid.getUUID();
        
}

public static String getDateAddOneHour(String createDate){

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=null;
try {
date = sdf.parse(createDate);
} catch (Exception e) {
e.printStackTrace();
}
Calendar ca=Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.HOUR_OF_DAY, 1);

return sdf.format(ca.getTime());

}

/**计算年月日时分秒毫秒**/
public static String Millisecond(String begins,String ends,String appointmentNo,String message)
{
   try {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
long between = 0;
try {
   Date begin = dfs.parse(begins);
   Date end = dfs.parse(ends);
   between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
} catch (Exception e) {

}
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);
long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000- min * 60 * 1000 - s * 1000);

StringBuilder sbf = new StringBuilder();
sbf.append("预约单号:"+appointmentNo+" ");
sbf.append(message);
sbf.append(":");
sbf.append(day);
sbf.append("天");
sbf.append(hour);
sbf.append("小时");
sbf.append(min);
sbf.append("分");
sbf.append(s);
sbf.append("秒");
sbf.append(ms);
sbf.append("毫秒");
return sbf.toString();
} catch (Exception e) {

}
   
   return "";
}
/**获取当前时间 年月日时分秒毫秒**/
public static String currentDate()
{
String result="";
try {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date data = new Date();
result = dfs.format(data);
} catch (Exception e) 
{

}
return result;
}

}