您的位置:首页 > 其它

开发常用的日期格式处理(Date)

2013-11-08 08:46 621 查看
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil {
private static final Log logger = LogFactory.getLog(DateUtil.class);

/**
* 设置当前时间为当天的最初时间(即00时00分00秒)
*
* @param cal
* @return
*/
public static Calendar setStartDay(Calendar cal) {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal;
}
/**
* 设置当前时间为当天的最后时间(即23时59分59秒)
*
* @param cal
* @return
*/
public static Calendar setEndDay(Calendar cal) {
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal;
}

/**
* 获得两个时间差,秒
* @param d1
* @param d2
* @return
*/
public static Long getDateDifference(Date d1, Date d2) {
try {
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60);
return days;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 把源日历的年月日设置到目标日历对象中
*
* @param destCal
* @param sourceCal
*/
public static void copyYearMonthDay(Calendar destCal, Calendar sourceCal) {
destCal.set(Calendar.YEAR, sourceCal.get(Calendar.YEAR));
destCal.set(Calendar.MONTH, sourceCal.get(Calendar.MONTH));
destCal.set(Calendar.DAY_OF_MONTH, sourceCal.get(Calendar.DAY_OF_MONTH));
}

/**
* 格式化日期
*
* @return
*/
public static String formatEnDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

return sdf.format(date).replaceAll("上午", "AM").replaceAll("下午", "PM");
}
/**
* 格式化日期为
*
* @return
*/
public static String formatCnDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* 格式化日期
* @param date
* @param 自定义格式
*
* @return
*/
public static String formatDate(Date date,String fmt) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
return sdf.format(date);
}
/**
* 格式化日期
* 格式:yyyy-MM-dd
* 如果日期为null,返回一个空字符串
* @return
*/
public static String formatDateIncludeNull(Date date) {
if(date!=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
return "";
}

/**
* 格式化日期
* @param dateString
* @return
*/
public static Date parseDate(String dateString) {
Date date = null;
try {
date = DateUtils.parseDate(dateString, new String[] {
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"});
} catch (Exception ex) {
logger.error("Pase the Date(" + dateString + ") occur errors:"
+ ex.getMessage());
}
return date;
}
/**
* 字符串转日期
* @param date
* @return
*/
public static Date strToDate(String date){
if(date==null||date.equals(""))return null;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
try {
dt = sf.parse(date);
} catch (ParseException e) {
dt = null;
}
return dt;
}

/**
* 获取现在时间
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static Date strToDate() {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
String strDate = formatter.format(new Date());
Date strtodate = null;
try {
strtodate = formatter.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}

return strtodate;

}
/**
* 获取形如yyyyMMddHHmmss的当前日期字串
*
* @return String
*/
public static String getDateString() {
// 日期格式
DateFormat dfmt = new SimpleDateFormat("yyyyMMddHHmmss");
return dfmt.format(new Date());
}

/**
* 获取形如yyyyMMdd的当前日期字串
*
* @return String
*/
public static String getCurDate() {
// 日期格式
DateFormat dfmt = new SimpleDateFormat("yyyyMMdd");
return dfmt.format(new Date());
}

/**
* 判断日期格式:yyyy-mm-dd
*
* @param sDate
* @return
*/
public static boolean isValidDate(String sDate) {
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if ((sDate != null)) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(sDate);
if (match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(sDate);
return match.matches();
} else {
return false;
}
}
return false;
}

public static void main(String args[]){
// Date d = new Date();
// System.out.println(DateUtil.formatDate(d,"yyyy-MM-dd"));
// String sDate="2011-11-10";
// boolean flag=DateUtil.isValidDate(sDate);
// System.out.println(flag);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: