package com.ld.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.commons.lang3.time.DateUtils;
/**
* @author 刘迪 日期类型处理类 2014-1-20 下午1:19:35
*/
public class MyDateUtil extends DateUtils{
public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_DATE = "yyyy-MM-dd";
private static SimpleDateFormat SDF;
/**
* getDate() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:27:29
* <p>
* 转换字符串为指定格式日期时间
* </p>
* @param dtstring
* 字符型日期
* @param pattern
* 需要转换的格式
* @return Date 转换后的日期
*/
public static Date getDate(String dtstring, String pattern) {
int pmIndex = dtstring.toUpperCase().indexOf("PM");
int amIndex = dtstring.toUpperCase().indexOf("AM");
int aIndex = pattern.indexOf("a");
if ((pmIndex >= 0 || amIndex >= 0) && aIndex == -1) {
if (pattern.indexOf("HH:mm:ss") != -1)
pattern = pattern.replaceAll("HH:mm:ss", "hh:mm:ss a");
else
pattern = pattern.replaceAll("hh:mm:ss", "hh:mm:ss a");
}
if (pmIndex == -1 && amIndex == -1 && aIndex >= 0) {
pattern = pattern.replaceAll("hh:mm:ss a", "HH:mm:ss");
}
SDF = new SimpleDateFormat(pattern, Locale.getDefault());
Date date = null;
try {
date = SDF.parse(dtstring);
} catch (Exception e) {
}
if (date == null) {
try {
SDF = new SimpleDateFormat(pattern, Locale.US);
date = SDF.parse(dtstring);
} catch (Exception e) {
}
}
if (date == null) {
try {
SDF = new SimpleDateFormat(pattern, Locale.CHINA);
date = SDF.parse(dtstring);
} catch (Exception e) {
}
}
if (amIndex >= 0) {// 解决 12:15 AM 类似问题, 应该是 00:15 AM
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (cal.get(Calendar.HOUR_OF_DAY) == 12) {
cal.add(Calendar.HOUR_OF_DAY, -12);
date = cal.getTime();
}
}
return date;
}
/**
* convertString() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:57:06
* <p>
* 转换日期类型为指定格式字符串
* </p>
* @param date
* 日期
* @param pattern
* 需要转换的格式
* @return String 转换后的日期字符串
*/
public static String convertString(Date date, String pattern) {
SDF = new SimpleDateFormat(pattern);
try {
return SDF.format(date);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* getDateTimeByString() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:41:51
* <p>
* 日期转换
* </p>
* @param dtstring
* @return Date 返回带时分秒的日期
*/
public static Date getDateTimeByString(String dtstring) {
return getDate(dtstring, FORMAT_DATETIME);
}
/**
* getDateByString() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:44:01
* <p>
* 日期转换
* </p>
* @param dtstring
* @return Date 只返回年月日
*/
public static Date getDateByString(String dtstring) {
return getDate(dtstring, FORMAT_DATE);
}
/**
* getStringByDateTime() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:58:29
* <p>
* 日期转换
* </p>
* @param date
* @return String 返回包含时分秒的字符串日期
*/
public static String getStringByDateTime(Date date) {
return convertString(date, FORMAT_DATETIME);
}
/**
* getStringByDate() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:59:07
* <p>
* 日期转换
* </p>
* @param date
* @return
* @return String 仅返回年月日字符串
*/
public static String getStringByDate(Date date) {
return convertString(date, FORMAT_DATE);
}
/**
* getStringByDate() method.
*
* @author 刘迪
* @date 2014-9-4 下午3:36:57
* <p>
* 转换日期为指定格式字符串
* </p>
* @param date
* @param pattern
* @return
* @return String
*/
public static String getStringByDate(Date date, String pattern) {
return convertString(date, pattern);
}
/**
* getDateByDate() method.
*
* @author 刘迪
* @date 2014-1-20 下午1:47:22
* <p>
* 日期转换
* </p>
* @param day
* 日期类型
* @return Date 只保留年月日
*/
public static Date getDateByDateTime(Date day) {
return truncate(day, Calendar.DAY_OF_MONTH);
}
/**
* calculateSeconds() method.
*
* @author 刘迪
* @date 2014-1-20 下午3:46:29
* <p>
* 计算两个时间相差的秒
* </p>
* @param sDay
* 起码日期
* @param eDay
* 结束日期
* @return int
*/
public static int calculateSeconds(Date sDay, Date eDay) {
return minusDateSeconds(sDay.getTime(), eDay.getTime());
}
public static int calculateSeconds(Calendar sDay, Calendar eDay) {
return minusDateSeconds(sDay.getTimeInMillis(), eDay.getTimeInMillis());
}
private static int minusDateSeconds(Long startSeconds, Long endSeconds) {
return (int) ((endSeconds - startSeconds) / 1000);
}
/**
* calculateMintues() method.
*
* @author 刘迪
* @date 2014-1-20 下午4:06:11
* <p>
* 计算两个时间相差的分钟
* </p>
* @param sDay
* @param eDay
* @return double
*/
public static double calculateMintues(Date sDay, Date eDay) {
return minusMinutes(minusDateSeconds(sDay.getTime(), eDay.getTime()));
}
public static double calculateMintues(Calendar sDay, Calendar eDay) {
return minusMinutes(minusDateSeconds(sDay.getTimeInMillis(), eDay.getTimeInMillis()));
}
private static double minusMinutes(Integer seconds) {
return Math.abs(seconds / (60 * 1.0));
}
/**
* calculateHours() method.
*
* @author 刘迪
* @date 2014-1-20 下午4:05:43
* <p>
* 计算两个时间相差的小时
* </p>
* @param sDay
* @param eDay
* @return double
*/
public static double calculateHours(Date sDay, Date eDay) {
return minusHours(minusDateSeconds(sDay.getTime(), eDay.getTime()));
}
public static double calculateHours(Calendar sDay, Calendar eDay) {
return minusHours(minusDateSeconds(sDay.getTimeInMillis(), eDay.getTimeInMillis()));
}
private static double minusHours(Integer seconds) {
return seconds / (60 * 60 * 1.0);
}
/**
* daysBetween() method.
*
* @author 刘迪
* @date 2014-1-20 下午3:42:37
* <p>
* 计算两个日期相差的天数
* </p>
* @param sDay
* @param eDay
* @return int
*/
public static int calculateDay(Date sDay, Date eDay) {
Calendar s = Calendar.getInstance();
s.setTime(sDay);
Calendar e = Calendar.getInstance();
e.setTime(eDay);
return calculateDay(s, e);
}
public static int calculateDay(Calendar sDay, Calendar eDay) {
sDay = truncate(sDay, Calendar.DAY_OF_MONTH);
eDay = truncate(eDay, Calendar.DAY_OF_MONTH);
return (int) (minusDateSeconds(sDay.getTimeInMillis(), eDay.getTimeInMillis()) / (60 * 60 * 24));
}
/**
* calculateDay() method.
*
* @author 刘迪
* @date 2014-1-20 下午5:11:05
* <p>
* 增减天数
* </p>
* @param day
* @param num
* @return Date
*/
public static Date calculateAddDay(Date day, int num) {
return calculateMinusOrPlus(day, Calendar.DAY_OF_MONTH, num);
}
public static Date calculateAddDay(Calendar day, int num) {
return calculateMinusOrPlus(day, Calendar.DAY_OF_MONTH,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TPY.rar (61个子文件)
TPY
pom.xml 4KB
target
test-classes
m2e-jee
web-resources
META-INF
maven
TPY
TPY
pom.xml 4KB
pom.properties 190B
MANIFEST.MF 110B
classes
jdbc.properties 458B
log4j.properties 645B
com
ld
dao
action
model
UsrAdmin.class 1KB
common
dao
BaseDao.class 3KB
utils
DefaultRepositoryFactoryBean.class 1KB
DefaultRepositoryFactory.class 2KB
MyQuery.class 12KB
impl
BaseDaoImpl.class 13KB
action
AuthType.class 1KB
ActionAuth.class 458B
service
BaseService.class 1KB
impl
BaseServiceImpl.class 4KB
service
utils
Constants.class 467B
MyValidateUtil.class 8KB
DateConverter.class 1KB
MyStringUtil.class 7KB
MySecurityUtil.class 6KB
MyDateUtil.class 8KB
MyNumberUtil.class 2KB
dispatcher-servlet.xml 2KB
applicationContext.xml 3KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 450B
org.eclipse.core.resources.prefs 191B
org.eclipse.wst.common.component 659B
org.eclipse.wst.common.project.facet.core.prefs.xml 161B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 530B
org.eclipse.jdt.core.prefs 430B
src
test
resources
java
main
webapp
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 3KB
classes
web
index.jsp 829B
resources
jdbc.properties 458B
log4j.properties 645B
dispatcher-servlet.xml 2KB
applicationContext.xml 3KB
java
com
ld
dao
action
model
UsrAdmin.java 976B
common
dao
BaseDao.java 4KB
utils
MyQuery.java 10KB
DefaultRepositoryFactory.java 1KB
DefaultRepositoryFactoryBean.java 702B
impl
BaseDaoImpl.java 8KB
action
ActionAuth.java 510B
AuthType.java 431B
service
impl
BaseServiceImpl.java 3KB
BaseService.java 825B
service
utils
Constants.java 250B
MySecurityUtil.java 5KB
MyValidateUtil.java 12KB
DateConverter.java 673B
MyDateUtil.java 13KB
MyNumberUtil.java 959B
MyStringUtil.java 7KB
.project 1KB
.classpath 1KB
共 61 条
- 1
资源评论
- mwt_yan2017-02-07资源不错,但不是全注解的
- IT包身工2016-04-10非常不错的资源,谢谢楼主分享
刘老高
- 粉丝: 16
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功