当前位置 博文首页 > Monsterof的博客:java处理日期的工具类
/**
* 处理日期的工具类
*/
@Slf4j
public class DateUtils {
/**
* <p>将字符串日期转换为Date</p>
*/
public static Date convertToDate(String s) {
DateFormat df;
if (s == null) {
return null;
}
if (s.contains(":")) {
try {
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(s);
} catch (Exception e) {
log.error(e.getMessage());
}
} else {
try {
df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(s);
} catch (Exception e) {
log.error(e.getMessage());
}
}
return null;
}
/**
* <p>将Date转换为String(格式:yyyy-MM-dd)</p>
*/
public static String formatDate(Date d) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* <p>将Date转换为String(格式:yyyyMMdd)</p>
*/
public static String formatDateString(Date d) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
public static Date strToYYMMDDDate(String dateString){
if(null == dateString) {
return new Date();
}
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
} catch (ParseException e) {
return new Date();
}
}
/**
* <p>获取当前时间戳</p>
*/
public static String getTimeStamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
try {
return sdf.format(new java.util.Date());
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* <p>将Date转换为String(格式:yyyy-MM-dd HH:mm:ss)</p>
*/
public static String formatDateTime(Date d) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* <p>将Date转换为String(格式:yyyy-MM-dd HH:mm:ss)</p>
*/
public static String formatDateTime(Date d,String format) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* <p>将Date按locale转换为String(格式:yyyy-MM-dd)</p>
*/
public static String formatLocaleDate(Date d, Locale locale) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", locale);
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* <p>将Date按locale转换为String(格式:yyyy-MM-dd HH:mm:ss)</p>
*/
public static String formatLocaleDateTime(Date d, Locale locale) {
if (d == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);
try {
return sdf.format(d);
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* <p>得到每月多少天</p>
*/
public static int getDaysByMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
} else {
return 28;
}
}
return -1;
}
/**
* <p>把星期的数字转换成汉字</p>
* 1:周日,2:周一.....7:周六
*/
public static String dayOfWeekByDayNum(int x) {
String str = "星期日";
if (x == 7) {
str = "星期六";
} else if (x == 6) {
str = "星期五";
} else if (x == 5) {
str = "星期四";
} else if (x == 4