package com.els.common.util;
import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.springframework.util.StringUtils;
/**
*
* 类描述:时间操作定义类
*
* @Author: wyssss
*/
public class DateUtils extends PropertyEditorSupport {
public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy年MM月dd日");
}
};
public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
};
public static ThreadLocal<SimpleDateFormat> yyyymmddhhmmss = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyyMMddHHmmss");
}
};
public static ThreadLocal<SimpleDateFormat> short_time_sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("HH:mm");
}
};
public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
/** 以毫秒表示的时间 DAY_IN_MILLIS */
private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
/** 以毫秒表示的时间 HOUR_IN_MILLIS */
private static final long HOUR_IN_MILLIS = 3600 * 1000;
/** 以毫秒表示的时间 MINUTE_IN_MILLIS */
private static final long MINUTE_IN_MILLIS = 60 * 1000;
/** 以毫秒表示的时间 SECOND_IN_MILLIS */
private static final long SECOND_IN_MILLIS = 1000;
/** 指定模式的时间格式 */
private static SimpleDateFormat getSDFormat(String pattern) {
return new SimpleDateFormat(pattern);
}
/**
* 当前日历,这里用中国时间表示
*
* @return 以当地时区表示的系统当前日历
*/
public static Calendar getCalendar() {
return Calendar.getInstance();
}
/**
* 指定毫秒数表示的日历
*
* @param millis
* 毫秒数
* @return 指定毫秒数表示的日历
*/
public static Calendar getCalendar(long millis) {
Calendar cal = Calendar.getInstance();
// --------------------cal.setTimeInMillis(millis);
cal.setTime(new Date(millis));
return cal;
}
/**
* 当前日期
*
* @return 系统当前时间
*/
public static Date getDate() {
return new Date();
}
/**
* 指定毫秒数表示的日期
*
* @param millis
* 毫秒数
* @return 指定毫秒数表示的日期
*/
public static Date getDate(long millis) {
return new Date(millis);
}
/**
* 字符串转换时间戳
*
* @param str
* @return
*/
public static Timestamp str2Timestamp(String str) {
SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = str2Date(str, dateSdf);
return new Timestamp(date.getTime());
}
public static Date parseDateNew(String d,String format) {
try {
return new SimpleDateFormat(format).parse(d);
} catch (ParseException e) {
return null;
}
}
public static Date parseDate(Object o) {
if (o == null) {
return null;
}
if (o instanceof Date) {
return (Date) o;
}
if (o instanceof java.sql.Date) {
return (Date) o;
}
if (o instanceof String) {
// yyyy-MM-dd HH:mm:ss /
String d=(String) o;
StringBuilder format=new StringBuilder("yyyy");
if (d.charAt(4)=='-') {
format.append("-MM-dd");
}else if(d.charAt(4)=='/') {
format.append("/MM/dd");
}else if (d.charAt(4)=='_'){
format.append("_MM_dd");
}else {
format.append("MMdd");
}
if (d.length()<format.length()) {
return null;
}else if (d.length()==format.length()) {
return parseDateNew(d, format.toString());
}
if (d.charAt(format.length())==' ') {
format.append(' ');
}
if (d.charAt(format.length()+2)==':') {
format.append("HH:mm:ss");
}else if (d.charAt(format.length()+2)=='/') {
format.append("HH/mm/ss");
}else {
format.append("HHmmss");
}
if (d.length()<format.length()) {
return null;
}
if (d.length()==format.length()) {
return parseDateNew(d,format.toString());
}
if (d.charAt(format.length())=='.'&&d.length()==(format.length()+4)) {
format.append(".SSS");
}else if(d.length()==(format.length()+3)){
format.append("SSS");
}else {
d=d.substring(0, format.length());
}
return parseDateNew(d, format.toString());
}
if (o instanceof Long) {
long l=(long) o;
if (l<10000000000l) {
return new Date(l*1000);
}
return new Date(l);
}
if (o instanceof Integer) {
long l = Long.valueOf((Integer) o + "000");
return new Date(l);
}
return null;
}
/**
* 字符串转换成日期
*
* @param str
* @param sdf
* @return
*/
public static Date str2Date(String str, SimpleDateFormat sdf) {
if (null == str || "".equals(str)) {
return null;
}
Date date = null;
try {
date = sdf.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 格式化时间
*
* @param date
* @param format
* @return
*/
public static String dateformat(String date, String format) {
SimpleDateFormat sformat = new SimpleDateFormat(format);
Date newDate = null;
try {
newDate = sformat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return sformat.format(newDate);
}
/**
* 日期转换为字符串
*
* @param date
* 日期
* @param format
* 日期格式
* @return 字符串
*/
public static String date2Str(Date date, SimpleDateFormat dateSdf) {
if (nul
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
utils.rar (17个子文件)
utils
IPUtils.java 2KB
PoiElUtil.java 6KB
MD5Util.java 1KB
ExcelUtil.java 9KB
PasswordUtil.java 5KB
RedisUtil.java 15KB
JwtUtil.java 4KB
AutoLog
CommonConstant.java 5KB
AutoLogAspect.java 10KB
AutoLog.java 1KB
AopElUtils.java 3KB
SpringContextUtils.java 2KB
GenInviteCode.java 2KB
DateUtils.java 22KB
UUIDGenerator.java 2KB
SysUtil.java 8KB
PdfUtil.java 7KB
共 17 条
- 1
iiYcyk
- 粉丝: 5
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0