package com.ktv.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 日期格式化类
*/
public class DateUtils extends java.util.Date {
private static final long serialVersionUID = 1L;
private static final DateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private static final String strFormat1 = "yyyy-MM-dd HH:mm";
/**
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
*/
/**
* 构造函数
*/
public DateUtils() {
super(getSystemDate().getTime().getTime());
}
/**
* 当前时间
*
* @return 时间Timestamp
*/
public java.sql.Timestamp parseTime() {
return new java.sql.Timestamp(this.getTime());
}
/**
* 将Date类型转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param Date
* @return String
*/
public static String format(Date date) {
return format(date, null);
}
/**
* 将Date类型转换为字符串
*
* @param Date
* @param pattern 字符串格式
* @return String
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "";
} else if (pattern == null || pattern.equals("") || pattern.equals("null")) {
return datetimeFormat.format(date);
} else {
return new SimpleDateFormat(pattern).format(date);
}
}
/**
* 将Date类型转换为字符串 yyyy-MM-dd
*
* @param Date
* @return String
*/
public static String formatDate(Date date) {
if (date == null) {
return "";
}
return dateFormat.format(date);
}
/**
* 将字符串转换为Date类型
*
* @param sDate yyyy-MM-dd HH:mm:ss
* @return
*/
public static Date convert(String sDate) {
try {
if (sDate != null) {
if (sDate.length() == 10) {
return dateFormat.parse(sDate);
} else if (sDate.length() == 19) {
return datetimeFormat.parse(sDate);
}
}
} catch (ParseException pe) {
}
return convert(sDate, null);
}
/**
* 将字符串转换为Date类型
*
* @param sDate
* @param pattern 格式
* @return
*/
public static Date convert(String sDate, String pattern) {
Date date = null;
try {
if (sDate == null || sDate.equals("") || sDate.equals("null")) {
return null;
} else if (pattern == null || pattern.equals("") || pattern.equals("null")) {
return datetimeFormat.parse(sDate);
} else {
return new SimpleDateFormat(pattern).parse(sDate);
}
} catch (ParseException pe) {
}
return date;
}
/**
* String转换为Date
*
* @param sDate 日期"yyyy-MM-dd"
* @return 日期Date
*/
public static Date convertDate(String dateStr) {
return convert(dateStr, "yyyy-MM-dd");
}
/**
* String转换为Timestamp
*
* @param sDate 日期 "yyyy-MM-dd" / "yyyy-MM-dd HH:mm:ss"
* @return 日期Timestamp
*/
public static Timestamp convertTimestamp(String sDate) {
if (sDate.length() == 10) {
sDate = sDate + " 00:00:00";
}
if (sDate.length() == 16) {
sDate = sDate + ":00";
}
return Timestamp.valueOf(sDate);
}
/**
* Date转换为Timestamp
*/
public static Timestamp convert(Date date) {
return new Timestamp(date.getTime());
}
/**
* 取当前日期(yyyy-mm-dd)
*
* @return 时间Timestamp
*/
public static String getTodayDate() {
return formatDate(new Date());
}
/**
* 取当前日期(yyyy-mm-dd hh:mm:ss)
*
* @return 时间Timestamp
*/
public static String getTodayDateTime() {
return format(new Date());
}
/**
* 取得n分钟后的时间
*
* @param date 日期
* @param afterMins
* @return 时间Timestamp
*/
public static Date getAfterMinute(Date date, long afterMins) {
if (date == null)
date = new Date();
long longTime = date.getTime() + afterMins * 60 * 1000;
return new Date(longTime);
}
// public static void main(String[] arg) {
// System.err.println(format((new Date())));
// System.err.println(format(getAfterMinute(new Date(), 3)));
// }
/**
* 取得指定日期几天后的日期
*
* @param date 日期
* @param afterDays 天数
* @return 日期
*/
public static Date getAfterDay(Date date, int afterDays) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.DATE, afterDays);
return cal.getTime();
}
/**
* 取得指定日期几天后的日期
*
* @param sDate 日期 yyyy-MM-dd
* @param afterDays 天数
* @return 日期
*/
public static String getAfterDay(String sDate, int afterDays) {
Date date = convertDate(sDate);
date = getAfterDay(date, afterDays);
return formatDate(date);
}
/**
* 取得指定日期几天前的日期
*
* @param date 日期
* @param beforeDays 天数(大于0)
* @return 日期
*/
public static Date getBeforeDay(Date date, int beforeDays) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.DATE, 0 - Math.abs(beforeDays));
return cal.getTime();
}
/**
* 取得指定日期几天前的日期
*
* @param sDate 日期 yyyy-MM-dd
* @param beforeDays 天数(大于0)
* @return 日期
*/
public static String getBeforeDay(String sDate, int beforeDays) {
Date date = convertDate(sDate);
date = getBeforeDay(date, beforeDays);
return formatDate(date);
}
/**
* 获得几个月后的日期
*
* @param date 日期
* @param afterMonth 月数
* @return 日期Date
*/
public static Date getAfterMonth(Date date, int afterMonth) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.MONTH, afterMonth);
return cal.getTime();
}
/**
* 获得几个月后的日期
*
* @param sDate 日期
* @param afterMonth 月数
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的KTV管理系统源码.zip (1576个子文件)
fontawesome-webfont.ttf@v=3.2.1 77KB
fontawesome-webfont.woff@v=3.2.1 43KB
fontawesome-webfont.eot@v=3.2.1 37KB
M663 1125q-11 -1 -15.5 -10.5t-8.5 -9.5q-5 -1 -5 5q0 12 19 15h10zM750 1111q-4 -1 -11.5 6.5t-17.5 4.5q03F7DC5A5A 4KB
hx.bmp 6KB
dy.bmp 6KB
post.bmp 6KB
zx.bmp 6KB
abc.bmp 6KB
pingan.bmp 6KB
cmb.bmp 6KB
cmbc.bmp 6KB
ccb.bmp 6KB
gf.bmp 6KB
sfz.bmp 6KB
icbc.bmp 6KB
bj.bmp 6KB
ningbo.bmp 6KB
beijingnongshang.bmp 6KB
sh.bmp 6KB
zheshang.bmp 6KB
shpd.bmp 6KB
bcc.bmp 6KB
nanjing.bmp 6KB
bh.bmp 6KB
guangda.bmp 6KB
cib.bmp 6KB
bc.bmp 6KB
BaseDaoImpl.class 15KB
BaseDaoImpl.class 15KB
DateUtils.class 8KB
DateUtils.class 8KB
ProductAction.class 6KB
OrderAction.class 6KB
ProductAction.class 6KB
CommentAction.class 5KB
UserAction.class 4KB
CategorySecAction.class 4KB
ProductDaoImpl.class 4KB
OrderAction.class 4KB
LoginAction.class 4KB
CategoryAction.class 4KB
BookAction.class 4KB
CommentAction.class 3KB
AdminAction.class 3KB
JsonUtil.class 3KB
JsonUtil.class 3KB
CartAction.class 3KB
SystemContextFilter.class 2KB
SystemContextFilter.class 2KB
BaseServiceImpl.class 2KB
BaseServiceImpl.class 2KB
AdminDaoImpl.class 2KB
Comment.class 2KB
Comment.class 2KB
UserDaoImpl.class 2KB
Product.class 2KB
Product.class 2KB
UserAction.class 2KB
UUIDUtils.class 2KB
UUIDUtils.class 2KB
SystemContext.class 2KB
SystemContext.class 2KB
Book.class 2KB
Order.class 2KB
Order.class 2KB
User.class 2KB
User.class 2KB
TimestampMorpher.class 2KB
TimestampMorpher.class 2KB
CategoryDaoImpl.class 2KB
Book.class 2KB
Cart.class 2KB
CategorySecDaoImpl.class 2KB
CategorySecDaoImpl.class 2KB
ProductServiceImpl.class 2KB
OrderItem.class 2KB
OrderItem.class 2KB
CategoryDaoImpl.class 2KB
ProductDaoImpl.class 2KB
UserDaoImpl.class 2KB
OrderDaoImpl.class 2KB
Admin.class 2KB
Admin.class 2KB
CategorySec.class 1KB
CategorySec.class 1KB
Pager.class 1KB
Pager.class 1KB
AdminServiceImpl.class 1KB
OrderItemDaoImpl.class 1KB
MyStrutsFilter.class 1KB
MyStrutsFilter.class 1KB
OrderDaoImpl.class 1KB
BookDaoImpl.class 1KB
CategoryServiceImpl.class 1KB
LoginAction.class 1KB
UserServiceImpl.class 1KB
Category.class 1KB
Category.class 1KB
CategorySecServiceImpl.class 1KB
共 1576 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论
栾还是恋
- 粉丝: 29
- 资源: 4996
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功