java⽇期格式转换yyyymmdd_java⽇期格式转换⼤全详解public class DateFormatUtils {
private static Log logger = Log(DateFormatUtils.class);
public static String formatDate(String formater,Date date){
SimpleDateFormat formate = new SimpleDateFormat(formater);
formate.format(date);
return formate.format(date);
}
/**
*
* @Title:formatDateToCommon
* @Description: 通⽤时间转换类型
* @param date
* @return
*/
public static String formatDateToCommon(Date date){
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formate.format(date);
}
/**
*
* @Title:getSystemDate
* @Description: 获取系统当前时间
* @param date
* @return
* @throws Exception
*/
public static Date getSystemDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(sdf.format(new Date()));
} catch (ParseException e) {
<("", e);
}
return null ;
}
/**
*
* @Title:SystemDateFormatToCommon
* @Description: 获取系统当前时间
* @return
*/
public static String getSystemDateFormatToCommon(){
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return formate.format(new Date());
}
public static String getSystemDateFormatToYYYYMMDD(){
SimpleDateFormat formate = new SimpleDateFormat("yyyyMMdd");
return formate.format(new Date());
}
public static String getSystemDateFormatToYYYYMMDDHHmmss(){ SimpleDateFormat formate = new SimpleDateFormat("yyyyMMddHHmmss"); return formate.format(new Date());
}
/**
*
* @Title:getFormatDateCommon
* @Description: 格式化时间
* @param date
* @return
* @throws Exception
*/
public static Date getFormatDateCommon(Date date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(sdf.format(date));
} catch (ParseException e) {
<("", e);
}
return null;
}
/**
*
* @Title:StringToDate
* @Description: 字符串转换成⽇期
* @param dateStr
* @param formatStr
* @return
* @throws ParseException
*/
public static Date StringToDate(String dateStr) throws ParseException { DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=null;
date = sdf.parse(dateStr);
return date;
}
public static Date StringToDate(String dateStr, String pattern){
try{
DateFormat sdf=new SimpleDateFormat(pattern);
Date date = sdf.parse(dateStr);
return date;
}catch(ParseException ex){
return null;
}
}
/
**
*
* @Title:fromDateStringToLong
* @Description: 获取字符串时间格式的毫秒数
* @param inVal
* @return
*/
public static long fromDateStringToLong(String inVal) {
return fromDateStringToLong(inVal, "yyyy-MM-dd HH:mm:ss");
}
public static long fromDateStringToLong(String inVal,String format) {
Date date = null; // 定义时间类型
SimpleDateFormat inputFormat = new SimpleDateFormat(format);
try {
date = inputFormat.parse(inVal); // 将字符型转换成⽇期型
} catch (Exception e) {
<("", e);
}
Time(); // 返回毫秒数
}
/**
*
* @Title:getMillForDateTimeDouble
* @Description: 获取两个时间之间的毫秒数
* @param inVal
* @return
*/
public static long getMillForDateTimeDouble(Date startTime, Date endTime) { long lTime = Time();
long eTime = Time();
long s = eTime - lTime ;
return s;
}
/**
*
* @Title:formatDuring
* @Description: 将毫秒数转换为时分秒
* @param mss
* @return
*/
public static String formatDuring(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
error parse newlong minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 时 " + minutes + " 分 "
+ seconds + " 秒 ";
}
/**
* 计算两个⽇期之间相差的天数
* @param format yyyyMMdd
* @param smdate 较⼩的时间
* @param bdate 较⼤的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(String format,Date smdate,Date bdate) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Instance();
cal.setTime(smdate);
long time1 = TimeInMillis();
cal.setTime(bdate);
long time2 = TimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 计算两个⽇期之间相差的天数
* 字符串的⽇期格式的计算
* @param format yyyyMMdd
* @param smdate 较⼩的时间
* @param bdate 较⼤的时间

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。