?
共享一下前几天整理的java时间工具类
?
1?import?java.io.PrintStream;
??2?import?java.sql.Time;
??3?import?java.sql.Timestamp;
??4?import?java.text.ParsePosition;
??5?import?java.text.SimpleDateFormat;
??6?import?java.util.Date;
??7?public?class?TimeUtil
??8?{
??9?/*
?10?*?www.pmjava.com?java编程
?11?*/
?12?public?TimeUtil()
?13?{
?14?}
?15?public?static?Date?strToDate(String?sStr)
?16?{
?17?if?(sStr?==?null)
?18?return?null;
?19?SimpleDateFormat?formatter;
?20?if?(sStr.length()?==?19)
?21?formatter?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
?22?else
?23?if?(sStr.length()?==?10)
?24?formatter?=?new?SimpleDateFormat("yyyy-MM-dd");
?25?else
?26?if?(sStr.length()?==?8)
?27?formatter?=?new?SimpleDateFormat("yyyyMMdd");
?28?else
?29?if?(sStr.length()?==?14)
?30?formatter?=?new?SimpleDateFormat("yyyyMMddHHmmss");
?31?else
?32?formatter?=?new?SimpleDateFormat("yyyyMMddHHmmss");
?33?ParsePosition?pos?=?new?ParsePosition(0);
?34?return?formatter.parse(sStr,?pos);
?35?}
?36?public?static?Timestamp?strToDatetime(String?s)
?37?{
?38?return?new?Timestamp(strToDate(s).getTime());
?39?}
?40?public?static?String?datetimeToChinese(Date?dtSource)
?41?{
?42?SimpleDateFormat?formatter?=?new?SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
?43?return?formatter.format(dtSource);
?44?}
?45?public?static?String?dateToStr(Date?date)
?46?{
?47?SimpleDateFormat?format?=?new?SimpleDateFormat("yyyyMMdd");
?48?return?format.format(date);
?49?}
?50?public?static?String?datetimeToStr(Date?date)
?51?{
?52?if?(date?==?null)
?53?{
?54?return?"";
?55?}?else
?56?{
?57?SimpleDateFormat?formatter?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
?58?return?formatter.format(date);
?59?}
?60?}
?61?public?static?String?today()
?62?{
?63?return?dateToStr(new?Date());
?64?}
?65?public?static?String?now()
?66?{
?67?return?datetimeToStr(new?Date());
?68?}
?69?public?static?String?calcTime(String?time,?int?diffYear,?int?diffMonth,?int?diffDate,?int?diffHour,?int?diffMinute,?int?diffSecond)
?70?{
?71?Timestamp?timestamp?=?strToDatetime(time);
?72?timestamp.setYear(timestamp.getYear()?+?diffYear);
?73?timestamp.setMonth(timestamp.getMonth()?+?diffMonth);
?74?timestamp.setDate(timestamp.getDate()?+?diffDate);
?75?timestamp.setHours(timestamp.getHours()?+?diffHour);
?76?timestamp.setMinutes(timestamp.getMinutes()?+?diffMinute);
?77?timestamp.setSeconds(timestamp.getSeconds()?+?diffSecond);
?78?return?datetimeToStr(timestamp);
?79?}
?80?public?static?String?getCurrentTime()
?81?{
?82?String?s?=?(new?Time(System.currentTimeMillis())).toString();
?83?return?s;
?84?}
?85?public?static?String?getCurrentDate()
?86?{
?87?String?s?=?(new?java.sql.Date(System.currentTimeMillis())).toString();
?88?return?s;
?89?}
?90?public?static?String?getCurrentNow()
?91?{
?92?String?s?=?getCurrentDate()?+?"?"?+?getCurrentTime();
?93?return?s;
?94?}
?95?public?static?String?dateFromStr(String?date)
?96?{
?97?if?(date?==?null?||?date.length()?<?10)
?98?return?getCurrentDate();
?99?else
100?return?date.substring(0,?10);
101?}
102?public?static?String?timeFromStr(String?date)
103?{
104?if?(date?==?null?||?date.length()?<?19)
105?return?"00:00:00";
106?else
107?return?date.substring(11,?19);
108?}
109?public?static?void?main(String?args[])
110?{
111?System.out.println(now());
112?}
113?}