package com.zhf.common.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.GregorianCalendar;
import static org.apache.commons.lang3.time.DateUtils.isSameDay;
public class DateUtil {
private DateUtil(){
}
private static String getDateFormat(Date date, String dateFormatType) {
SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
return simformat.format(date);
}
public static String formatCSTTime(String date, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date d = sdf.parse(date);
return DateUtil.getDateFormat(d, format);
}
public final static long ONE_DAY_SECONDS = 86400;
/*
* private static DateFormat dateFormat = null; private static DateFormat
* longDateFormat = null; private static DateFormat dateWebFormat = null;
*/
public final static String shortFormat = "yyyyMMdd";
public final static String longFormat = "yyyyMMddHHmmss";
public final static String webFormat = "yyyy-MM-dd";
public final static String timeFormat = "HHmmss";
public final static String monthFormat = "yyyyMM";
public final static String chineseDtFormat = "yyyy年MM月dd日";
public final static String newFormat = "yyyy-MM-dd HH:mm:ss";
public final static String newFormat2 = "yyyy-MM-dd HH:mm";
public final static String newFormat3 = "yyyy-MM-dd HH";
public final static String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
static {
/*
* dateFormat = new SimpleDateFormat(shortFormat);
* dateFormat.setLenient(false); longDateFormat = new
* SimpleDateFormat(longFormat); longDateFormat.setLenient(false);
* dateWebFormat = new SimpleDateFormat(webFormat);
* dateWebFormat.setLenient(false);
*/
}
public static long ONE_DAY_MILL_SECONDS = 86400000;
public static Date getNowDate() {
return new Date();
}
public static Date getDate(long millsecord) {
return new Date(millsecord);
}
public static DateFormat getNewDateFormat(String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
df.setLenient(false);
return df;
}
public static void main(String[] args) {
Date n = new Date();
Date mm = formatDate(n, "yyyy-MM-dd");
System.out.println(mm.toString());
}
//日期格式转换,返回日期类型
public static Date formatDate(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(date);
ParsePosition pos = new ParsePosition(0);
Date newDate = formatter.parse(dateString, pos);
return newDate;
}
//日期格式转换,返回String
public static String format(Date date, String format) {
if (date == null) {
return null;
}
return new SimpleDateFormat(format).format(date);
}
public static String formatByLong(long date, String format) {
return new SimpleDateFormat(format).format(new Date(date));
}
public static String formatByString(String date, String format) {
if (StringUtils.isNotBlank(date)) {
return new SimpleDateFormat(format).format(new Date(NumberUtils
.toLong(date)));
}
return StringUtils.EMPTY;
}
public static String formatShortFormat(Date date) {
if (date == null) {
return null;
}
return new SimpleDateFormat(shortFormat).format(date);
}
public static Date parseDateNoTime(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(shortFormat);
if ((sDate == null) || (sDate.length() < shortFormat.length())) {
throw new ParseException("length too little", 0);
}
if (!StringUtils.isNumeric(sDate)) {
throw new ParseException("not all digit", 0);
}
return dateFormat.parse(sDate);
}
public static Date parseDateNoTime(String sDate, String format)
throws ParseException {
if (StringUtils.isBlank(format)) {
throw new ParseException("Null format. ", 0);
}
DateFormat dateFormat = new SimpleDateFormat(format);
if ((sDate == null) || (sDate.length() < format.length())) {
throw new ParseException("length too little", 0);
}
return dateFormat.parse(sDate);
}
public static Date parseDateNoTimeWithDelimit(String sDate, String delimit)
throws ParseException {
sDate = sDate.replaceAll(delimit, "");
DateFormat dateFormat = new SimpleDateFormat(shortFormat);
if ((sDate == null) || (sDate.length() != shortFormat.length())) {
throw new ParseException("length not match", 0);
}
return dateFormat.parse(sDate);
}
public static Date parseDateLongFormat(String sDate) {
DateFormat dateFormat = new SimpleDateFormat(longFormat);
Date d = null;
if ((sDate != null) && (sDate.length() == longFormat.length())) {
try {
d = dateFormat.parse(sDate);
} catch (ParseException ex) {
return null;
}
}
return d;
}
public static Date parseDateNewFormat(String sDate) {
Date d = parseDateHelp(sDate, newFormat);
if (null != d) {
return d;
}
d = parseDateHelp(sDate, newFormat2);
if (null != d) {
return d;
}
d = parseDateHelp(sDate, newFormat3);
if (null != d) {
return d;
}
d = parseDateHelp(sDate, webFormat);
if (null != d) {
return d;
}
try {
DateFormat dateFormat = new SimpleDateFormat(newFormat);
return dateFormat.parse(sDate);
} catch (ParseException ex) {
return null;
}
}
private static Date parseDateHelp(String sDate, String format) {
if ((sDate != null) && (sDate.length() == format.length())) {
try {
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(sDate);
} catch (ParseException ex) {
return null;
}
}
return null;
}
/**
* 计算当前时间几小时之后的时间
*
* @param date
* @param hours
* @return
*/
public static Date addHours(Date date, long hours) {
return addMinutes(date, hours * 60);
}
/**
* 计算当前时间几分钟之后的时间
*
* @param date
* @param minutes
* @return
*/
public static Date addMinutes(Date date, long minutes) {
return addSeconds(date, minutes * 60);
}
/**
* @param date1
* @param secs
* @return
*/
public static Date addSeconds(Date date1, long secs) {
return new Date(date1.getTime() + (secs * 1000));
}
/**
* 判断输入的字符串是否为合法的小时
*
* @param hourStr
* @return true/false
*/
public static boolean isValidHour(String hourStr) {
if (!StringUtils.isEmpty(hourStr) && StringUtils.isNumeric(hourStr)) {
int hour = new Integer(hourStr).intValue();
if ((hour >= 0) && (hour <= 23)) {
return true;
}
}
return false;
}
/**
* 判断输入的字符串是否为合法的分或秒
*
* @param
* @return true/
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Python的股票自动交易系统的设计与实现.zip (687个子文件)
app.css 236KB
common.css 97KB
common.css 97KB
material-design-iconic-font.min.css 69KB
nwd_common.css 54KB
nwd_common.css 54KB
animate.min.css 52KB
style.min.css 27KB
jquery.scrollbar.css 22KB
sweetalert2.css 19KB
_all.css 14KB
index.css 13KB
index.css 13KB
product_details.css 12KB
product_details.css 12KB
disclosureCommon.css 11KB
login.css 8KB
daterangepicker.css 7KB
bootstrap-table.css 7KB
cart.css 6KB
header.css 6KB
header.css 6KB
product_search.css 5KB
product_search.css 5KB
cart.css 5KB
login.css 4KB
login_index.css 4KB
multiple-select.css 4KB
register.css 4KB
register.css 4KB
style.css 4KB
style.css 4KB
footer.css 2KB
footer.css 2KB
purple.css 2KB
orange.css 2KB
yellow.css 2KB
green.css 2KB
pink.css 1KB
blue.css 1KB
grey.css 1KB
aero.css 1KB
red.css 1KB
minimal.css 1KB
autocomplete.css 1KB
jquery.treegrid.css 913B
base.css 700B
base.css 700B
ip2region.db 2.6MB
Dockerfile 98B
.DS_Store 6KB
Material-Design-Iconic-Font.eot 41KB
Roboto-Medium-webfont.eot 21KB
Roboto-Regular-webfont.eot 21KB
Roboto-Bold-webfont.eot 20KB
Roboto-Light-webfont.eot 20KB
throbber.gif 2KB
.gitattributes 86B
.gitignore 101B
menuIcon.html 132KB
index.html 18KB
stock.html 13KB
index.html 13KB
product_details.html 12KB
product_details.html 12KB
newbie.html 10KB
500.html 10KB
404.html 10KB
403.html 10KB
profile.html 10KB
shenzhen.html 8KB
product_search.html 8KB
newbie_guide.html 7KB
userAdd.html 7KB
product_search.html 7KB
menuAdd.html 6KB
job.html 6KB
cart.html 5KB
cart.html 5KB
dict.html 5KB
user.html 4KB
itme7.html 4KB
itme4.html 4KB
itme6.html 4KB
itme5.html 4KB
itme3.html 4KB
itme2.html 4KB
itme1.html 4KB
info_open.html 4KB
menu.html 4KB
header.html 4KB
header.html 4KB
register.html 4KB
login.html 4KB
jobAdd.html 4KB
register.html 4KB
login.html 3KB
header.html 3KB
dictAdd.html 3KB
role.html 3KB
共 687 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
程皮
- 粉丝: 263
- 资源: 2567
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 重庆市首席信息官(CIO)协会 《软件及信息化工程造价规范V5.0》T/CQCIO 001-2019
- 联想Y471老A卡笔记本开机黑屏 需等待黑屏结束才能进入桌面,解决方法
- elasticsearch-7.14.0和ik分词器
- 利用MPI计算任意范围内的质数
- EnviroSkyandWeather v2.1.1(u2017.1.2)真实动态天气系统包
- React框架介绍及相关教程、案例.docx
- 基于Springboot和Vue教务评教系统(PC端+server端源码+数据库MySQL脚本+环境部署步骤讲解+运行步骤讲解)
- react-photo-view图片预览组件
- 雷赛控制C#使用包含定位、插补运动等
- 自存spss参考资料资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功