package com.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public final class DateUtil {
/** 日期格式,指定到日期. */
public static final String DATE_FORMAT_DATE = "yyyy-MM-dd";
/** 日期格式,指定到时分秒. */
public static final String DATE_FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
/** 日期格式,指定到时分. */
public static final String DATE_FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
/** 日期格式,指定到年. */
public static final String DATE_FORMAT_YEAR_NAME = "yyyy";
/** 日期格式,指定到月. */
public static final String DATE_FORMAT_MONTH_NAME = "yyyyMM";
/** 日期格式,指定到日. */
public static final String DATE_FORMAT_DATE_NAME = "yyyyMMdd";
/** 开始时间后缀 */
public static final String TIME_START_SUFFIX = " 00:00:00";
/** 结束时间后缀 */
public static final String TIME_END_SUFFIX = " 23:59:59";
/** 日期格式,指定到时分秒纯数字 */
public static final String DATE_FORMAT_TIME_MILL_NUMBER = "yyyyMMddHHmmss";
/**
* Instantiates a new date util.
*/
private DateUtil() {
// do not
}
public static String getDateStrByTimestamp(Object intt) {
if (intt==null) return "";
String rtString = "";
String str = String.valueOf(intt);
if (str.matches("^[0-9]*$")) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_TIME);
Timestamp tt = new Timestamp(Integer.valueOf(String.valueOf(intt)));
rtString = format.format(tt);
} else {
rtString = str.substring(0,19);
}
return rtString;
}
/**
* Timestamp格式的日期转换成String字符串.
*
* @param t
* the t
* @return the string
*/
public static String timestampToDateString(Timestamp t) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_DATE);
Timestamp tt = t;
if (tt == null) {
tt = new Timestamp(System.currentTimeMillis());
}
return format.format(tt);
}
/**
* Timestamp格式的日期转换成String字符串.
*
* @param t
* the t
* @return the string
*/
public static String timestampToDateString2(Timestamp t) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_TIME);
Timestamp tt = t;
if (tt == null) {
tt = new Timestamp(System.currentTimeMillis());
}
return format.format(tt);
}
/**
* 字符串格式的时间转换成Timestamp类型,将时间转换为Timestamp.
*
* @param dateStr
* the date str
* @return the timestamp
*/
public static Timestamp stringToTimestamp(String dateStr) {
SimpleDateFormat sp = new SimpleDateFormat(DATE_FORMAT_TIME);
return stringToTimestamp0(dateStr, sp);
}
/**
* 字符串格式的时间转换成Timestamp类型.该方法作为一个底层方法.
*
* @param dateStr
* the date str
* @param sp
* the sp
* @return the timestamp
*/
private static Timestamp stringToTimestamp0(String dateStr, SimpleDateFormat sp) {
String str = dateStr.trim();
if (str.length() < 8) {
return new Timestamp(System.currentTimeMillis());
} else {
return new Timestamp(sp.parse(dateStr, new ParsePosition(0)).getTime());
}
}
/**
* 字符串格式的时间转换成Timestamp类型,将日期转换为Timestamp.
*
* @param dateStr
* the date str
* @return the timestamp
*/
public static Timestamp stringToTimestampWithDate(String dateStr) {
SimpleDateFormat sp = new SimpleDateFormat(DATE_FORMAT_DATE);
return stringToTimestamp0(dateStr, sp);
}
/**
* 获取当前时间,格式为yyyy-MM-dd HH:mm:ss.
*
* @return the current date time
*/
public static String getCurrentDateTime() {
return timestampToDatetimeString(new Timestamp(System.currentTimeMillis()));
}
public static String getCurrentDateNumber() {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_TIME_MILL_NUMBER);
return format.format(new Timestamp(System.currentTimeMillis()));
}
/**
* 获取当前日期,格式为yyyy-MM-dd.
*
* @return the current date
*/
public static String getCurrentDate() {
return timestampToDateString(new Timestamp(System.currentTimeMillis()));
}
/**
* 按照yyyy-MM-dd HH:mm:ss格式,格式化时间.
*
* @param t
* the t
* @return the string
*/
public static String timestampToDatetimeString(Timestamp t) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_TIME);
return format.format(t);
}
/**
* 计算begin和end相差的天数.
*
* @param begin
* the begin
* @param end
* the end
* @return the long
*/
public static long dateInterval(String begin, String end) {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_DATE);
try {
Date beginDate = formatter.parse(begin);
Date endDate = formatter.parse(end);
if (endDate.getTime() - beginDate.getTime() > 0) {
return (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000l) + 1;
} else {
return 0;
}
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
/**
* 计算begin和end相差的秒数.
*
* @param begin
* the begin
* @param end
* the end
* @return the long
*/
public static long secondsDiff(String begin, String end) {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_TIME);
try {
Date beginDate = formatter.parse(begin);
Date endDate = formatter.parse(end);
if (endDate.getTime() - beginDate.getTime() > 0) {
return (endDate.getTime() - beginDate.getTime()) / 1000l;
} else {
return 0;
}
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
/**
* 计算begin和end相差的秒数.
*
* @param begin
* the begin
* @param end
* the end
* @return the long
*/
public static long secondInterval(String begin, String end) {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_TIME);
try {
Date beginDate = formatter.parse(begin);
Date endDate = formatter.parse(end);
if (endDate.getTime() - beginDate.getTime() > 0) {
return (endDate.getTime() - beginDate.getTime());
} else {
return 0;
}
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
/**
* 计算begin和end相差的分钟数.
*
* @param begin
* the begin
* @param end
* the end
* @return the long
*/
public static long minuteInterval(String begin, String end) {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_MINUTE);
try {
Date beginDate = formatter.parse(begin);
Date endDate = formatter.parse(end);
if (endDate.getTime() - beginDate.getTime() > 0) {
return (endDate.getTime() - beginDate.getTime()) / (60 * 1000l);
} else {
return 0;
}
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取当前时间以前某天的日期,具体到时分秒.
*
* @param days
* the days
* @return the before current with days
*/
public static String getBeforeCurrentWithDays(int days) {
return timestampToDatetimeString(new Timestamp(System.currentTimeMillis() - (days * 24 * 60 * 60 * 1000l)));
}
/**
* 获取当前时间以后某天的日期,具体到时分秒.
*
* @param days
* the days
* @return the before current with days
*/
public static String getAfterCurrentWithDays(int days) {
return timestampToDatetimeString(new Timestamp(System.currentTimeMillis() + (days * 24 * 60 * 60 * 1000l)));
}
/**
* 获取当前时间几天以后的时间.格式为
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot+mysql+jsp的学生宿舍管理系统 (861个子文件)
AbstractDao.class 15KB
DateUtil.class 12KB
CFUtils.class 11KB
TypeUtil.class 10KB
UserController.class 10KB
CmmController.class 9KB
FeedbackController.class 8KB
ShujutsController.class 8KB
Shuju2Controller.class 8KB
ShujujfController.class 8KB
Zhigong3Controller.class 7KB
ShujuwController.class 7KB
ShujukController.class 7KB
ClassesController.class 6KB
CmmServiceImpl.class 6KB
NewsController.class 6KB
UserInfo20Controller.class 6KB
UserInfo30Controller.class 5KB
CategoryController.class 5KB
CmmDao.class 4KB
DbBackupUtil.class 3KB
PageUtil.class 3KB
RandomUtil.class 2KB
CommandUtil.class 2KB
WebMvcConfig.class 2KB
Page.class 2KB
MathUtil.class 2KB
StringUtil.class 2KB
WebUtils.class 2KB
MapUtil.class 2KB
Recover.class 1KB
AbstractRestController.class 1KB
Application.class 1KB
CmmService.class 788B
SqlMapper.class 748B
.classpath 6KB
sb-admin-2.min.css 192KB
summernote-bs3.css 143KB
bootstrap.min14ed.css 118KB
style.min862f.css 112KB
layui.css 73KB
layui.css 71KB
layui.css 68KB
ueditor.css 45KB
video-js.min.css 38KB
ueditor.min.css 34KB
jquery-ui.min.css 30KB
reg.css 30KB
font-awesome.min93e3.css 26KB
video-js.css 21KB
admin.css 21KB
image.css 19KB
ui.jqgridffe4.css 16KB
video.css 15KB
attachment.css 15KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
weadmin.css 12KB
video-js.min.css 11KB
layer.css 11KB
layim.css 11KB
summernote.css 10KB
xadmin.css 10KB
layui.mobile.css 10KB
layui.mobile.css 10KB
laydate.css 8KB
laydate.css 7KB
laydate.css 7KB
shCoreDefault.css 7KB
laydate.css 6KB
laydate.css 6KB
cmm.css 4KB
scrawl.css 4KB
style.css 4KB
cmm.css 4KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
layer.ext.css 3KB
codemirror.css 3KB
theme89.css 3KB
theme83.css 3KB
theme26.css 3KB
theme87.css 3KB
charts.css 3KB
background.css 2KB
theme84.css 2KB
theme85.css 2KB
login.css 2KB
theme88.css 2KB
css.css 2KB
theme82.css 2KB
theme86.css 2KB
theme27.css 2KB
emotion.css 2KB
dialogbase.css 2KB
共 861 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
Coding路人王
- 粉丝: 1w+
- 资源: 124
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功