public class DateFormat {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat dateFormatNumber = new SimpleDateFormat(
"yyyyMMddHHmmss");
public static String DateToString(Date date) {
return dateFormat.format(date);
}
public static String DateToStringNumber(Date date) {
return dateFormatNumber.format(date);
}
// 获取当前时间,long型
public static long currentTimeLong() {
return new Date().getTime();
}
// 字符串日期转long
public static long StrDateToLong(String strDate) {
if (strDate == "" || strDate == null) {
return Long.parseLong("0");
} else {
return DateToLong(toDate(strDate));
}
}
// 日期转long
public static long DateToLong(Date date) {
return date.getTime();
}
// 获取当前时间,字符串形式
public static String currentTime() {
Date date = new Date();
return toString(date);
}
// 从字符串, 获取日期, 如time = "2016-3-16 4:12:16"
public static Date toDate(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
return date;
} catch (ParseException e) {
return null;
}
}
public static Date toDateNoHour(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(time);
return date;
} catch (ParseException e) {
return null;
}
}
//定时时间
public static Date toTimingDate(String time,String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(time);
return date;
} catch (ParseException e) {
return null;
}
}
// 从long, 获取日期
public static Date LongtoDate(long millSec) {
return new Date(millSec);
}
// 日期转化为字符串形式
public static String toString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
}