package com.ava.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/***
* @author xp.wang
* 时间类型定义:<br/>
* 1、normalDate:"yyyy-MM-dd",String<br/>
* 2、normalDateTime:"yyyy-MM-dd HH:mm:ss",String<br/>
* 2、millis: 毫秒格式,Long<br/>
* 3、shortDate: 20120602,Integer<br/>
* 4、shortDateTime: 20120602101203,Long<br/>
* 5、date: Date对象<br/>
*/
public class DateTime{
private final static Logger logger = LoggerFactory.getLogger(DateTime.class);
/** yyyyMM */
public final static String PATTERN_SHORTMONTH = "yyyyMM";
/** yyyy-MM */
public final static String PATTERN_YEAR_MONTH = "yyyy-MM";
/** yyyy-MM-dd */
public final static String PATTERN_DATE = "yyyy-MM-dd";
/** yyyyMMdd */
public final static String PATTERN_SHORTDATE = "yyyyMMdd";
/** yyyy-MM-dd HH:mm:ss */
public final static String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
/** yyyyMMddHHmmss */
public final static String PATTERN_SHORTDATETIME = "yyyyMMddHHmmss";
/** yyyy-MM-dd HH:mm */
public final static String PATTERN_DIVISION = "yyyy-MM-dd HH:mm";
/** HH:mm:ss */
// private static String PATTERN_TIME = "HH:mm:ss";
/** can't Construct */
private DateTime() {
}
public static Timestamp getTimestamp() {
return new Timestamp(new Date().getTime());
}
public static Timestamp toTimestamp(Date date) {
if(date == null){
return null;
}
return new Timestamp(date.getTime());
}
/** 返回Date对象,yyyyMM格式,yyyyMMdd格式,yyyyMMddHHmmss格式,yyyy-MM格式,yyyy-MM-dd格式,yyyy-MM-dd HH:mm:ss格式*/
public static Timestamp toTimestamp(String dateTime) {
return toTimestamp(toDate(dateTime));
}
/** 返回yyyy-MM-dd格式的字符串 */
public static String getNormalDate() {
return toNormalDate(new java.util.Date());
}
/** 返回yyyy-MM-dd HH:mm:ss格式的字符串 */
public static String getNormalDateTime() {
return toNormalDateTime(new java.util.Date());
}
/** 返回yyyy-MM-dd HH:mm格式的字符串 */
public static String getDivisionDateTime() {
return toDivisionDateTime(new java.util.Date());
}
/** 返回yyyyMMdd格式的字符串 */
public static String getShortDate() {
return toShortDate(new java.util.Date());
}
/** 返回yyyyMMddHHmmss格式的字符串 */
public static String getShortDateTime() {
return toShortDateTime(new java.util.Date());
}
/** 返回yyyyMMddHHmmss格式的长整型 */
public static Long getShortDateTimeL() {
return toShortDateTimeL(new java.util.Date());
}
/**返回yyyyMM格式 */
public static Integer getYearMonth(){
String yearMonth = getYear() + getMonth();
return TypeConverter.toInteger(yearMonth);
}
/**返回yyyy格式字符串 */
public static String getYear() {
return getYear(new Date());
}
/**返回yyyy格式 字符串*/
public static String getYear(Date date) {
if(date == null){
return null;
}
return TypeConverter.toString(date.getYear() + 1900);//Date类年份是从1900年算的
}
/**返回MM格式字符串 */
public static String getMonth() {
return getNormalDate().substring(5,7);
}
/**返回MM格式的整型 */
public static Integer getMonthInt() {
return TypeConverter.toInteger(getMonth());
}
/**返回dd格式的字符串 */
public static String getDay(String datestr) {
if(datestr == null){
return null;
}
return datestr.substring(8,10);
}
/**返回dd格式的字符串 */
public static String getDay() {
return getDay(getNormalDate());
}
/** 返回Date对象,yyyyMM格式,yyyyMMdd格式,yyyyMMddHHmmss格式,yyyy-MM格式,yyyy-MM-dd格式,yyyy-MM-dd HH:mm:ss格式*/
public static Date toDate(String dateTime) {
if(dateTime == null){
return null;
}
try {
Date d = null;
if(dateTime.length() == 6){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_SHORTMONTH});
}else if(dateTime.length() == 7){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_YEAR_MONTH});
}else if(dateTime.length() == 8){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_SHORTDATE});
}else if(dateTime.length() == 10){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_DATE});
}else if(dateTime.length() == 14){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_SHORTDATETIME});
}else if(dateTime.length() == 19){
d = DateUtils.parseDate(dateTime, new String[]{PATTERN_DATETIME});
}
return d;
} catch (ParseException e) {
logger.warn("toDate(String dateTime) error...");
e.printStackTrace();
}
return null;
}
/** 返回Date对象,可以输入yyyyMMdd格式和yyyyMMddHHmmss格式*/
public static Date toDate(Long dateTime) {
if(dateTime == null){
return null;
}
String dateStr = String.valueOf(dateTime);
Date d = toDate(dateStr);
return d;
}
/** 返回yyyy-MM-dd格式的字符串 */
public static String toNormalDate(Integer shortDate) {
if(shortDate == null || shortDate.intValue() == 0){
return null;
}
try {
Date d = DateUtils.parseDate(shortDate.toString(), new String[]{"yyyyMMdd"});
return DateFormatUtils.format(d, PATTERN_DATE);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/** 返回yyyy-MM-dd格式的字符串 */
public static String toNormalDate(Date dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_DATE);
}
/** 返回yyyy-MM-dd格式的字符串 */
public static String toNormalDate(Timestamp dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_DATE);
}
/** 返回yyyy-MM-dd HH:mm:ss格式的字符串 */
public static String toNormalDateTime(Date dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_DATETIME);
}
/** 返回yyyy-MM-dd HH:mm格式的字符串 */
public static String toDivisionDateTime(Date dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_DIVISION);
}
/** 返回yyyy-MM-dd HH:mm:ss格式的字符串 */
public static String toNormalDateTime(Timestamp dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_DATETIME);
}
/** 返回yyyy-MM-dd HH:mm:ss格式的字符串,可以输入yyyyMMdd格式和yyyyMMddHHmmss格式*/
public static String toNormalDateTime(Long dateTime) {
if(dateTime == null){
return null;
}
Date d = toDate(dateTime);
if(d == null){
return null;
}
return DateFormatUtils.format(d, PATTERN_DATETIME);
}
/** 返回yyyyMMdd格式的字符串 */
public static String toShortDate(Date dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_SHORTDATE);
}
/** 返回yyyyMMdd格式的字符串 */
public static String toShortDate(Timestamp dateTime) {
if(dateTime == null){
return null;
}
return DateFormatUtils.format(dateTime, PATTERN_SHORTDATE);
}
/** 返回yyyyMMdd格式的整型,可以输入yyyy-MM-dd格式和yyyy-MM-dd HH:mm:ss格式*/
public static Integer toShortDate(String dateTime) {
Date d = toDate(dateTime);
return TypeConverter.toInteger(toShortDate(d));
}
/** 返回yyyyMMddHHmmss格式的字符串 */
public static String toShortDateTime(Date dateTime) {
if(dateTime == null){
return null;
}
return DateFormatU
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
cassandra入门项目源代码 (183个子文件)
app.log.2014-05-05 21KB
app.err.2014-05-05 0B
app.log.2014-05-07 43KB
app.err.2014-05-07 12KB
app.log.2014-05-08 683KB
_0.cfe 268B
_0.cfe 268B
_0.cfs 2KB
_0.cfs 2KB
DateTime.class 17KB
DateTime.class 17KB
TypeConverter.class 12KB
TypeConverter.class 12KB
GlobalConstant.class 12KB
GlobalConstant.class 12KB
TweetSearch.class 6KB
TweetSearch.class 6KB
TimeDurationFormat.class 4KB
TimeDurationFormat.class 4KB
LoginFilter.class 4KB
LoginFilter.class 4KB
TweetResource.class 3KB
TweetResource.class 3KB
TweetRepository.class 3KB
TweetRepository.class 3KB
Tweet.class 3KB
Tweet.class 3KB
TweetService.class 3KB
TweetService.class 3KB
CassandraManager.class 3KB
CassandraManager.class 3KB
LuceneUtil.class 2KB
LuceneUtil.class 2KB
ResponseData.class 2KB
ResponseData.class 2KB
MyRandomUtils.class 2KB
User.class 2KB
MyRandomUtils.class 2KB
User.class 2KB
TimeDurationFormat$Format.class 2KB
TimeDurationFormat$Format.class 2KB
TestUserRepository.class 2KB
UserService.class 2KB
UserService.class 2KB
TestTweetRepository.class 2KB
UserRepository.class 2KB
UserRepository.class 2KB
GlobalConfig.class 1KB
MyStringUtils.class 1KB
GlobalConfig$1InitAppPath.class 1KB
GlobalConfig.class 1KB
GlobalConfig$1InitAppPath.class 1KB
MyStringUtils.class 1KB
SpringContext.class 1KB
SpringContext.class 1KB
UserResource.class 1KB
UserResource.class 1KB
ReturnCode.class 1012B
ReturnCode.class 1004B
ITweetRepository.class 685B
ITweetRepository.class 685B
TestBase.class 621B
IUserRepository.class 507B
IUserRepository.class 507B
ITweetService.class 476B
ITweetService.class 476B
IUserService.class 231B
IUserService.class 231B
.classpath 543B
app.err 359KB
segments.gen 20B
segments.gen 20B
lucene-core-4.6.1.jar 2.24MB
guava-16.0.1.jar 2.12MB
lucene-analyzers-common-4.6.1.jar 1.52MB
netty-3.9.0.Final.jar 1.17MB
jaxb-impl-2.2.7.jar 898KB
spring-context-3.1.1.RELEASE.jar 812KB
resteasy-jaxrs-3.0.6.Final.jar 805KB
jackson-mapper-asl-1.9.12.jar 762KB
spring-beans-3.1.1.RELEASE.jar 576KB
spring-webmvc-3.1.1.RELEASE.jar 560KB
spring-web-3.1.1.RELEASE.jar 532KB
cassandra-driver-core-2.0.0.jar 507KB
log4j-1.2.16.jar 470KB
spring-core-3.1.1.RELEASE.jar 439KB
httpclient-4.2.1.jar 417KB
commons-lang3-3.3.jar 403KB
lucene-queryparser-4.6.1.jar 375KB
spring-aop-3.1.1.RELEASE.jar 324KB
FastInfoset-1.2.12.jar 287KB
spring-data-commons-core-1.4.1.RELEASE.jar 261KB
junit-4.8.1.jar 231KB
commons-codec-1.6.jar 227KB
jackson-core-asl-1.9.12.jar 227KB
httpcore-4.2.1.jar 218KB
jaxb-core-2.2.7.jar 217KB
lucene-queries-4.6.1.jar 206KB
spring-test-3.0.0.RELEASE.jar 202KB
spring-expression-3.1.1.RELEASE.jar 172KB
共 183 条
- 1
- 2
资源评论
mykingbull
- 粉丝: 3
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功