/**
* DateTool.java 2017年7月20日
*
* Copyright 2001-2017 织巢信息 All rights reserved.
* 织巢信息 PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.jxxt.common.utils;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
/**
* <summary>
* <description>
*
* @author hqsun
* @since 2017年7月20日
* @see [Class/Method]
*
*/
public class DateUtil {
public static final TimeZone UTC_TZ = TimeZone.getTimeZone("UTC");
public static final int YEAR = 0;
public static final int MONTH = 1;
public static final int WEEK = 2;
public static final int DAY = 3;
public static final int HOUR = 4;
public static final int MINUTE = 5;
public static final int SECOND = 6;
public static final int MILLISECOND = 7;
public static final int MINUTEOFDAY = 8;
/** 一天的毫秒数 */
public static final long DAY_LONG = 1 * 24 * 60 * 60 * 1000;
/** 一小时的毫秒数 */
public static final long HOUR_LONG = 1 * 1 * 60 * 60 * 1000;
/** UTC_8 东八时区8小时的毫秒数 */
public static final long UTC_8_HOUR_LONG = 1 * 8 * 60 * 60 * 1000;
/** 半小时的毫秒数 */
public static final long HALF_HOUR_LONG = 1 * 1 * 30 * 60 * 1000;
/** 一分钟的毫秒数 */
public static final long MINUTE_LONG = 1 * 1 * 1 * 60 * 1000;
// add by wangzheng at 20140710 begin
public static final String DATE_PATTERN_S = "yyyyMMdd";
/**
* "yyyy-MM-dd"
*/
public static final String DATE_PATTERN_S_SHOW = "yyyy-MM-dd";
/**
* "yyyy/MM/dd"
*/
public static final String DATE_PATTERN_SHOW_FOR_WEB = "yyyy/MM/dd";
/**
* "yyyy-MM"
*/
public static final String DATE_PATTERN_Y_SHOW = "yyyy-MM";
/**
* "HH:mm"
*/
public static final String RESEND_DATE_PATTERN_SHOW = "HH:mm";
/**
* "yyyyMM"
*/
public static final String DATE_YEAR_MONTH = "yyyyMM";
/**
* "yyyyMMddHHmmss"
*/
public static final String DATE_PATTERN = "yyyyMMddHHmmss";
public static final String SHARE_FILE_MENU_ID = "0006";
public static final String DATE_PATTERN2 = "yyyyMMddHHmm";
public static final String DATE_LOG_PATTERN = "yyyyMMddHHmmssSSS";
public static final String LOG_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.S";
public static final String SHOW_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String SHOW_DATE_PATTERN_FOR_WEB = "yyyy/MM/dd HH:mm:ss";
public static final String SHOW_DATE_MINUTE = "yyyy-MM-dd HH:mm";
public static final String SHOW_TIME_NO_ZERO_FORMAT = "H:mm:ss";
public static final String SHOW_TIME_FORMAT = "HH:mm:ss";
public static final String SHOW_TIME_NO_SEC_ZERO_FORMAT = "H:mm";
public static final String SHOW_TIME_NO_SEC_FORMAT = "HH:mm";
public static final String RESEND_DATE_PATTERN = "HHmm";
public static final String DB_TIME_FORMAT = "HHmmss";
// add by wangzheng at 20140710 end
/**
* 按要求转化时间的显示格式 参数:oldpattern,旧日期格式,如:yyyyMMddhhmmss 格式描述符的含义参见JDK
* simpleDateFormat类 newpattern,新日期格式
*/
public static String timeTransform(String time, String oldpattern, String newpattern) {
// 打印调试信息
String oldtime;
if (oldpattern == null) {
throw new IllegalArgumentException("the old pattern is null");
}
if (newpattern == null) {
throw new IllegalArgumentException("the new pattern is null");
}
SimpleDateFormat olddatepattern = new SimpleDateFormat(oldpattern);
Date now;
try {
now = olddatepattern.parse(time);
// 用原来的pattern解析日期,再和原来的比较,由此来检查时间是否合法
oldtime = olddatepattern.format(now);
if (!oldtime.equals(time)) {
throw new IllegalArgumentException("using Illegal time");
}
}
catch (ParseException e) {
throw new IllegalArgumentException("using [" + oldpattern + "] parse [" + time + "] failed");
}
SimpleDateFormat newdatepattern = new SimpleDateFormat(newpattern);
return newdatepattern.format(now);
}
/**
* 获取指定格式的当前日期 参数:pattern,日期格式,如:yyyyMMddhhmmss 格式描述符的含义参见JDK
* simpleDateFormat类
*/
public static String getCurrentDate(String pattern) {
if (pattern == null) {
throw new IllegalArgumentException("input string parameter is null");
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date now = new Date();
return sdf.format(now);
}
/**
* 获取当前日期是星期几<br>
*
* @param dt
* @return 当前日期是星期几
*/
public static String getWeekOfDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now;
try {
now = sdf.parse(time);
}
catch (ParseException e) {
throw new IllegalArgumentException("using [yyyy-MM-dd] parse [" + time + "] failed");
}
String[] weekDays = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六",
};
Calendar cal = Calendar.getInstance();
cal.setTime(now);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 将日期长整型转换成字符串 参数:time,long,从格林威治时间:1970年1月1日0点起的毫秒数 pattern, String,
* 转换的目标格式
*/
public static String long2TimeStr(long time, String pattern) {
if (pattern == null) {
throw new IllegalArgumentException("pattern parameter can not be null");
}
Date dt = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(dt);
}
/**
* 将日期型转换成字符串 参数:time,Date pattern, String, 转换的目标格式
*/
public static String date2TimeStr(Date time, String pattern) {
if (pattern == null) {
throw new IllegalArgumentException("pattern parameter can not be null");
}
if (time == null) {
throw new IllegalArgumentException("time parameter can not be null");
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(time);
}
/**
* 将日期增加一个增量,目前只能是,年,月,周,日,时、分、秒、毫秒 参数:date, long,原始时间 delta,int,增量的大小 unit,
* int, 增量的单位,YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND
* 返回:long,从格林威治时间:1970年1月1日0点起的毫秒数
*/
public static long addDate(long date, int delta, int unit) {
if ((unit < YEAR) || (unit > MILLISECOND)) {
throw new IllegalArgumentException(
"time unit must in [YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND], others not support");
}
Date dt = new Date(date);
Calendar calendar = getLocalCalendar(dt);
// 增加增量
switch (unit) {
case YEAR:
calendar.add(Calendar.YEAR, delta);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java基于SSM网上试衣间设计-Eclipse源码.zip (337个子文件)
DateUtil.class 13KB
DateUtil.class 13KB
PaginationInterceptor.class 11KB
PaginationInterceptor.class 11KB
ExcelUtil.class 8KB
ExcelUtil.class 8KB
ExportExcel.class 8KB
ExportExcel.class 8KB
Tools.class 6KB
Tools.class 6KB
ExcelReader.class 6KB
ExcelReader.class 6KB
ExcelImportUtil.class 6KB
ExcelImportUtil.class 6KB
TInfoController.class 5KB
TInfoController.class 5KB
PDFReader.class 5KB
PDFReader.class 5KB
ImageCutUtil.class 5KB
ImageCutUtil.class 5KB
AjaxResponse.class 5KB
AjaxResponse.class 5KB
AddressUtils.class 4KB
AddressUtils.class 4KB
Doer.class 4KB
Doer.class 4KB
BeanToMapUtil.class 4KB
BeanToMapUtil.class 4KB
MTLoginController.class 4KB
MTLoginController.class 4KB
TMineController.class 4KB
TMineController.class 4KB
Page.class 3KB
Page.class 3KB
PropertiesHolder.class 3KB
PropertiesHolder.class 3KB
TInfo.class 2KB
TInfo.class 2KB
EncryptUtil.class 2KB
EncryptUtil.class 2KB
SystemConfigPojo.class 2KB
SystemConfigPojo.class 2KB
TMine.class 2KB
TMine.class 2KB
DateFormatter.class 2KB
DateFormatter.class 2KB
TLogin.class 1KB
TLogin.class 1KB
Constants$Commons.class 1KB
Constants$Commons.class 1KB
FileUpResModel.class 1KB
FileUpResModel.class 1KB
RandomUtil.class 1KB
RandomUtil.class 1KB
PaginationInterceptor$BoundSqlSqlSource.class 982B
PaginationInterceptor$BoundSqlSqlSource.class 982B
Constants$SystemConfig.class 946B
Constants$SystemConfig.class 946B
BasicModel.class 941B
BasicModel.class 941B
MessageMail.class 744B
MessageMail.class 744B
TLoginMapper.class 616B
TLoginMapper.class 616B
TInfoMapper.class 565B
TMineMapper.class 565B
TInfoMapper.class 565B
TMineMapper.class 565B
Template.class 554B
Template.class 554B
Constants.class 490B
Constants.class 490B
Constants$FileType.class 434B
Constants$FileType.class 434B
ClazzImpl.class 333B
ClazzImpl.class 333B
ClazzService.class 122B
ClazzService.class 122B
.classpath 886B
org.eclipse.wst.common.component 535B
org.eclipse.wst.jsdt.ui.superType.container 49B
ace.min.css 265KB
bootstrap.css 143KB
style-home.css 137KB
bootstrap.min.css 118KB
video-js.min.css 37KB
font-awesome-ie7.min.css 37KB
style2.css 35KB
font-awesome.min.css 30KB
ace-skins.min.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
font-awesome.min.css 20KB
select2.min.css 15KB
jquery-confirm.css 13KB
bootstrap-datetimepicker.min.css 11KB
bootstrap-select.min.css 10KB
style.css 9KB
fileinput.min.css 8KB
custom-styles.css 7KB
共 337 条
- 1
- 2
- 3
- 4
资源评论
BinaryStarXin
- 粉丝: 8742
- 资源: 287
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功