12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- package com.xtframe.util;
- import java.sql.Timestamp;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.logging.Logger;
- /**
- * 日期相关的共通
- */
- public class DateTime {
- private final static Logger LOG = Logger.getLogger(DateTime.class.getName());
- public DateTime() {
- }
-
- public static final String minDate = "1970-01";
-
- public static final String maxDate = "2099-01";
- /** 一天的毫秒数 */
- public static final long DAY_MILLI = 24 * 60 * 60 * 1000;
- /** 1小时的毫秒数 */
- public static final long HOUR_MILLI = 60 * 60 * 1000;
- /**
- * 要用到的缺省的Date only 格式定义 年/月/日
- **/
- public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 年/月/日
- /**
- * 要用到的缺省的Date time 格式定义 年/月/日 时:分:秒 YYYY/MM/DD HH:MM:SS
- **/
- public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
- /**
- * 闰年的每个月的天数数组
- **/
- public static int[] DAY_OF_MONTH_LEAP_YEAR = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- /**
- * 非闰年的每个月的天数数组
- **/
- public static int[] DAY_OF_MONTH_NON_LEAP_YEAR = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- /** Global SimpleDateFormat object */
- private static SimpleDateFormat sdfDateOnly = new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY);
- /** Datetime formatter */
- private static SimpleDateFormat sdfDateTime = new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME);
- /** Datetime full 格式的定义 */
- private static SimpleDateFormat sdfDateTimeFull = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
- /*****************************************************
- * java.util.Date ==> String 的转换函数
- *****************************************************/
- /**
- * String到Date类型
- *
- * @param sDate
- * 日期字符串
- * @return 日期类型
- */
- public static java.util.Date toDate(String sDate) {
- return toDate(sDate, DateTime.sdfDateOnly);
- }
- /**
- * 根据指定的格式将日期字符串转换为DATE类型
- *
- * @param sDate
- * 日期字符串
- * @param sFmt
- * 日期格式
- * @return 转换后的DATE类型
- * @see DateTime#DATE_FORMAT_DATEONLY
- * @see DateTime#DATE_FORMAT_DATETIME
- * @todo 日期String的长度不对的情况,需要对应
- */
- public static java.util.Date toDate(String sDate, String sFmt) {
- if (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) {// Date time
- return toDate(sDate, DateTime.sdfDateTime);
- } else if (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { // Date only
- return toDate(sDate, DateTime.sdfDateOnly);
- } else {
- // return null;
- return toDateAgain(sDate);
- }
- }
- /**
- * 利用指定SimpleDateFormat instance转化String到Date
- *
- * @param sDate
- * Date string
- * @param formatter
- * SimpleDateFormat instance
- * @return Date object
- * @since 1.0
- * @history 2004/05/01 ChenJP: parsing 失败的时候,试图做一些相对智能的处理
- */
- private static java.util.Date toDate(String sDate, SimpleDateFormat formatter) {
- if (sDate == null) {
- return null;
- }
- java.util.Date dt = null;
- try {
- dt = formatter.parse(sDate);
- } catch (Exception e) {
- // log.warn("Exception when parsing date in DateTime.toDate(), original String="
- // +sDate + ",formatter pattern=" + formatter.toPattern());
- // dt = null;
- dt = toDateAgain(sDate);
- }
- return dt;
- }
- /**
- * 试图做一些相对智能的日期处理
- *
- * @param sDate
- * @return 处理成功,返回Date object;
- * @return 处理失败,return null
- * @TODO: 是否可能对分隔符号,如"/" 或者":"也做一些智能的处理
- */
- private static java.util.Date toDateAgain(String sDate) {
- if (sDate == null) {
- return null;
- }
- java.util.Date dt = null;
- SimpleDateFormat formatter = null;
- // 试图解决日期String长度不对的问题
- if (sDate.length() < DateTime.DATE_FORMAT_DATEONLY.length()) { // 长度太短
- return null;
- } else if (sDate.length() < DateTime.DATE_FORMAT_DATETIME.length()) {
- // 根据in Date string的长度,new 一个SimpleDateFormat object,这种效率不高
- formatter = new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME.substring(0, sDate.length()));
- } else if (sDate.length() > DateTime.DATE_FORMAT_DATETIME.length()) { // in
- // 日期String太长,截断后处理
- sDate = sDate.substring(0, DateTime.DATE_FORMAT_DATETIME.length());
- formatter = DateTime.sdfDateTime;
- }
- try {
- dt = formatter.parse(sDate);
- } catch (Exception e) {
- LOG.warning("Exception when parsing date in DateTime.toDateAgain(), original String=" + sDate + ",formatter pattern=" + formatter.toPattern());
- dt = null;
- }
- return dt;
- }
- /**
- * 将一个long 型的毫秒值转换为日期String
- *
- * @param millis
- * @return 返回日期的格式由schema-config.xml中的DateTime 格式指定,缺省为yyyy/mm/dd hi24:mi:ss
- */
- public static String toString(long millis) {
- return toString(new java.util.Date(millis), DateTime.sdfDateTime);
- }
- /**
- * 将一个long 型的毫秒值转换为日期String
- *
- * @param millis
- * @return 精确到毫秒的日期String,格式固定为"yyyy/MM/dd HH:mm:ss.SSS" deprecated
- * 不推荐使用,目前仅Debug method中需要使用此method
- */
- public static String toStringFull(long millis) {
- return toString(new java.util.Date(millis), DateTime.sdfDateTimeFull);
- }
- /*****************************************************
- * String ==> java.util.Date 的转换函数
- *****************************************************/
- /**
- * 将日期DATE的变量转换为年月日字符串
- *
- * @param dt
- * 日期类型的变量
- * @return 返回转换以后的字符串
- */
- public static String toString(java.util.Date dt) {
- return toString(dt, DateTime.sdfDateOnly);
- }
- /**
- * 将日期DATE的变量转换为指定格式的字符串
- *
- * @param dt
- * 日期类型的变量
- * @param sFmt
- * 日期格式,DateTime中定义的两个常量,最终的格式来源与schma-config.xml中的定义.
- * @return 返回转换以后的字符串;如果指定的格式不符合定义的两种缺省格式,将返回Date.toString
- */
- public static String toString(java.util.Date dt, String sFmt) {
- // log.debug("DateTime.toString sFmt="+sFmt);
- // log.debug("DateTime.sdfDateTime="+DATE_FORMAT_DATETIME);
- if (dt == null) {
- return "";
- }
- if (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) { // Date time
- return toString(dt, DateTime.sdfDateTime);
- } else if (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { // Date only
- return toString(dt, DateTime.sdfDateOnly);
- } else { // Default , YYYY/MM/DD
- LOG.severe("Unknown date format in DateTime.toString, dateFormat = " + sFmt);
- return dt.toString();
- }
- }
- /**
- * 利用指定SimpleDateFormat instance转换java.util.Date到String
- *
- * @param dt
- * java.util.Date instance
- * @param formatter
- * SimpleDateFormat Instance
- * @return 转换为的String
- * @since 1.0
- */
- private static String toString(java.util.Date dt, SimpleDateFormat formatter) {
- if (dt == null || formatter == null) {
- return "";
- }
- String sRet = null;
- try {
- sRet = formatter.format(dt);
- } catch (Exception e) {
- LOG.severe("Exception in DateTime.toString");
- sRet = dt.toString();
- }
- return sRet;
- }
- /**
- * 在java.util.Date类型时间戳上增加/减少几天得到的新的时间戳
- *
- * @param date
- * java.util.Date类型的时间戳
- * @param days
- * 增加/减少的天数(此值>0表示是增加,<0表示是减少)
- * @return 计算后的日期
- */
- public static java.util.Date addDays(java.util.Date date, int days) {
- long temp = date.getTime();
- return new java.util.Date(temp + DateTime.DAY_MILLI * days);
- }
- /**
- * 把java.util.Date类型的日期转换为java.util.GregorianCalendar的日期
- *
- * @param date
- * java.util.Date类型的日期
- * @return java.util.GregorianCalendar的日期
- */
- private static java.util.GregorianCalendar convertToCalendar(java.util.Date date) {
- GregorianCalendar obj = new GregorianCalendar();
- obj.setTime(date);
- return obj;
- }
- /**
- * 在Date String时间戳上增加/减少几天得到的新的时间戳
- *
- * @param date
- * String类型的时间戳,date的格式应该是定义在schema-config.xml中的Date Only的格式
- * @param days
- * 增加/减少的天数
- * @return String类型的时间戳
- * @see DateTime#toDate(String)
- * @see DateTIme#addDays(String)
- */
- public static String addDays(String date, int days) {
- try {
- return DateTime.toString(DateTime.addDays(DateTime.toDate(date), days));
- } catch (Exception e) {
- LOG.severe("Exception in addDays,date=" + date + ",days add=" + days);
- return "";
- }
- }
- /**
- * 在String类型时间戳上增加/减少几个月得到的新的时间戳 日期的格式定义在schema-config.xml中
- *
- * @param paraDate
- * String类型的时间
- * @param mon
- * 增加/减少的月数
- * @return String类型的时间
- */
- public static String addMonths(String paraDate, int mon) {
- java.util.Date date = DateTime.toDate(paraDate);
- if (date == null) {
- return "";
- }
- GregorianCalendar obj = DateTime.convertToCalendar(date);
- // month : 0 -- 11
- int year = obj.get(GregorianCalendar.YEAR);
- int month = obj.get(GregorianCalendar.MONTH) + 1;
- int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
- month += mon;
- if (month < 1) {
- month += 12;
- year--;
- } else if (month > 12) {
- month -= 12;
- year++;
- }
- int lastDay = 0;
- // 对应通沙2004/03/30出现的Bug
- boolean laterThanLastMonth = false;
- if (obj.isLeapYear(year)) {
- laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]);
- } else {
- laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]);
- }
- if (isLastDayOfMonth(obj) || laterThanLastMonth) {
- if (obj.isLeapYear(year)) {
- lastDay = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
- } else {
- lastDay = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
- }
- if (lastDay < day) { // 结果的月份没有对应的天,则缩小天
- day = lastDay;
- }
- }
- obj.set(year, month - 1, day);
- java.util.Date resultDate = new java.util.Date(obj.getTimeInMillis());
- return DateTime.toString(resultDate);
- }
- /****************************************************
- * 后续要deprecated 的method
- *****************************************************/
- /**
- * 比较两个java.sql.Timestamp instance 的年月日部分是否相同
- *
- * @param date1
- * java.sql.Timestamp类型的时间措1
- * @param date2
- * java.sql.Timestamp类型的时间措2
- * @return 返回True : 年月日部分相同
- * @return 返回False: 年月日部分不同
- * @deprecated
- */
- public static boolean isSameDay(java.sql.Timestamp date1, java.sql.Timestamp date2) {
- String s1, s2 = null;
- s1 = date1.toString().substring(0, DateTime.DATE_FORMAT_DATEONLY.length());
- s2 = date2.toString().substring(0, DateTime.DATE_FORMAT_DATEONLY.length());
- // cat.debug("in isSameDay() , s1=" + s1 + ",s2=" + s2);
- return s1.equalsIgnoreCase(s2);
- }
- /**
- * 取得指定日期所在周的第一天(Sunday)
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回java.sql.Timestamp类型的日期
- */
- public static java.sql.Timestamp getFirstDayOfWeek(java.sql.Timestamp timestamp) {
- int no = DateTime.getWeekdayOfTimestamp(timestamp);
- java.sql.Timestamp out = DateTime.addDays(timestamp, 1 - no);
- return out;
- }
- /**
- * 取得指定日期所在周的最后一天(Saturday)
- *
- * @param timestamp
- * , java.sql.Timestamp类型的时间戳
- * @return java.sql.Timestamp类型的时间戳
- */
- public static java.sql.Timestamp getLastDayOfWeek(java.sql.Timestamp timestamp) {
- int no = DateTime.getWeekdayOfTimestamp(timestamp);
- java.sql.Timestamp out = DateTime.addDays(timestamp, 7 - no);
- return out;
- }
- /**
- * 取得指定日期所在月的1号所在周的Sunday(可能是上个月的日期)
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回java.sql.Timestamp类型的日期
- */
- public static java.sql.Timestamp getFirstSundayOfMonth(java.sql.Timestamp timestamp) {
- java.sql.Timestamp out = null;
- if (timestamp == null)
- return null;
- out = getFirstDayOfMonth(timestamp);
- out = DateTime.getFirstDayOfWeek(out);
- return out;
- }
- /**
- * 取得指定日期所在月的最后一天(如31号)所在周的Saturday(可能是下个月的日期)
- *
- * @param timestamp
- * , java.sql.Timestamp Object
- * @return java.sql.Timestamp instance
- */
- public static java.sql.Timestamp getLastSaturdayOfMonth(java.sql.Timestamp timestamp) {
- java.sql.Timestamp out = null;
- if (timestamp == null)
- return null;
- // cat.debug("In timestamp=" + timestamp.toString() );
- out = getLastDayOfMonth(timestamp);
- // cat.debug("LastDayOfMonth=" + out.toString() );
- out = DateTime.getLastDayOfWeek(out);
- // cat.debug("LastSaturdayOfMonth=" + out.toString() );
- return out;
- }
- /**
- * 取得指定日期所在月的第一天
- *
- * @param timestamp
- * , java.sql.Timestamp Object
- * @return java.sql.Timestamp instance
- */
- public static java.sql.Timestamp getFirstDayOfMonth(java.sql.Timestamp timestamp) {
- java.sql.Timestamp out = null;
- if (timestamp == null)
- return null;
- int day = DateTime.getDayOfTimestamp(timestamp);
- out = DateTime.addDays(timestamp, 1 - day);
- // out = DateTime.getFirstDayOfWeek(out);
- return out;
- }
- /**
- * 取得两个日期之间的日数
- *
- * @param t1
- * java.sql.Timestamp类型的日期1
- * @param t2
- * java.sql.Timestamp类型的日期2
- * @return 返回t1到t2间的日数。如果t2在t1之后,返回正数,否则返回负数
- */
- public static long daysBetween(java.sql.Timestamp t1, java.sql.Timestamp t2) {
- return (t2.getTime() - t1.getTime()) / DAY_MILLI;
- }
- /**
- * Format year/month/day to YYYY/MM/DD format
- *
- * @param year
- * @param month
- * @param day
- * @return YYYY/MM/DD format String
- */
- private static String formatYMD(int year, int month, int day) {
- String temp = String.valueOf(year) + "/";
- if (month < 10) {
- temp += "0" + String.valueOf(month) + "/";
- } else {
- temp += String.valueOf(month) + "/";
- }
- if (day < 10) {
- temp += "0" + String.valueOf(day);
- } else {
- temp += String.valueOf(day);
- }
- return temp;
- }
- /**
- * 取得指定日期的所在月的最后一天 目前只支持YYYY/MM/DD格式
- *
- * @param timestamp
- * Timestamp类型的日期
- * @return Timestamp类型的日期
- */
- public static java.sql.Timestamp getLastDayOfMonth(java.sql.Timestamp timestamp) {
- java.sql.Timestamp out = null;
- if (timestamp == null)
- return null;
- // out = (java.sql.Timestamp )timestamp.clone() ;
- // day = DateTime.getDayOfTimestamp(timestamp);
- // int month = DateTime.getMonthOfTimestamp(timestamp);
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- // month : 0 -- 11
- int day = 0;
- int year = obj.get(GregorianCalendar.YEAR);
- int month = obj.get(GregorianCalendar.MONTH) + 1;
- if (obj.isLeapYear(obj.get(GregorianCalendar.YEAR))) {
- day = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
- } else {
- day = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
- }
- /*
- * modified by ChenJP 2000/11/16 String temp = String.valueOf(year) +
- * "/"; if( month < 10 ){ temp += "0" + String.valueOf(month) + "/";
- * }else{ temp += String.valueOf(month)+ "/"; } if( day < 10 ){ temp +=
- * "0" + String.valueOf(day); }else{ temp += String.valueOf(day); }
- * //cat.debug("temp=" + temp); out = DateTime.toSqlTimestamp(temp);
- */
- out = DateTime.toSqlTimestamp(DateTime.formatYMD(year, month, day));
- /*
- * obj.set(GregorianCalendar.DAY_OF_MONTH , day) ; out = new
- * java.sql.Timestamp(obj.getTimeInMillis()); out =
- * DateTime.addDays(timestamp, 10) ; out =
- * DateTime.getFirstDayOfWeek(out);
- */
- return out;
- }
- /*****************************************************
- * 把java.sql.Timestamp Object 取出各部分的值
- *****************************************************/
- /**
- * 从java.sql.Timestamp日期信息中取出年
- *
- * @param timestamp
- * java.sql.Timestamp日期信息
- * @return 返回年
- */
- public static int getYearOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.YEAR);
- }
- /**
- * 从java.sql.Timestamp 日期中取出月份信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回月份(1 -- 12 )
- */
- public static int getMonthOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return (obj.get(GregorianCalendar.MONTH) + 1);
- }
- /**
- * 从java.sql.Timestamp日期中取出日信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回日信息
- */
- public static int getDayOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.DAY_OF_MONTH);
- }
- /**
- * 从java.sql.Timestamp类型的日期中取出周日1-7的信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return WeekDay周日信息
- */
- public static int getWeekdayOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.DAY_OF_WEEK);
- }
- /**
- * 返回指定日期的零时的Timestamp值
- *
- * @return 返回指定日期的零时的Timestamp值
- * @param timestamp
- * 返回指定日期的零时的Timestamp值
- */
- public static java.sql.Timestamp getZeroTime(java.sql.Timestamp timestamp) {
- if (timestamp == null) {
- return null;
- }
- return DateTime.toSqlTimestamp(DateTime.toString(new Date(timestamp.getTime())));
- // String tempStr = timestamp.toString().substring(0, 10);
- // return DateTime.toSqlTimestamp(tempStr);
- }
- /**
- * 从java.sql.Timestamp类型的日期中取出时间:分钟信息 HH:MM
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回HH:MM格式的时分信息
- */
- public static String getHourAndMinuteString(java.sql.Timestamp timestamp) {
- String out = null;
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- int hour = obj.get(GregorianCalendar.HOUR_OF_DAY);
- int minute = obj.get(GregorianCalendar.MINUTE);
- if (minute < 10) {
- out = String.valueOf(hour) + ":0" + String.valueOf(minute);
- } else {
- out = String.valueOf(hour) + ":" + String.valueOf(minute);
- }
- return out;
- }
- /**
- * 从java.sql.Timestamp Object 中取出24小时制的时间信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 24小时制的hour value
- */
- public static int getHourOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.HOUR_OF_DAY);
- }
- /**
- * 从java.sql.Timestamp类型的日期中取出分钟信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回分钟信息
- */
- public static int getMinuteOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.MINUTE);
- }
- /**
- * 从java.sql.Timestamp类型的日期中取出秒信息
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 返回秒信息
- */
- public static int getSecondOfTimestamp(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- return obj.get(GregorianCalendar.SECOND);
- }
- /**
- * 把java.sql.Timestamp类型的日期转换为java.util.GregorianCalendar的日期
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return java.util.GregorianCalendar的日期
- * @deprecated
- */
- public static java.util.GregorianCalendar convertToCalendar(java.sql.Timestamp timestamp) {
- GregorianCalendar obj = new GregorianCalendar();
- // Modified by ChenJP 2000/11/17
- obj.setTime(DateTime.convertTimestampToDate(timestamp));
- // 下面的method不能用,long ==> int 精度不对
- // obj.set(GregorianCalendar.MILLISECOND , (int)timestamp.getTime() );
- return obj;
- }
- /**
- * 把java.sql.Timestamp类型的日期转换为java.util.Date类型的日期
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return java.util.Date类型的日期
- */
- public static java.util.Date convertTimestampToDate(java.sql.Timestamp timestamp) {
- java.util.Date date = null;
- date = new Date(timestamp.getTime());
- return date;
- }
- /*****************************************************
- * 取系统日期、时间的函数
- *****************************************************/
- /**
- * 取系统日期、时间的函数。返回long型的系统日期时间
- *
- * @return long型的系统时间
- * @see System.currentTimeMillis()
- */
- public static long getSysDateLong() {
- return System.currentTimeMillis();
- }
- /**
- * 返回java.sql.Timestamp型的SYSDATE
- *
- * @return java.sql.Timestamp型的SYSDATE
- */
- public static java.sql.Timestamp getSysDateTimestamp() {
- return new java.sql.Timestamp(System.currentTimeMillis());
- }
- /**
- * 以配置的Date Only的格式返回系统日期
- *
- * @return 系统日期
- */
- public static String getSysDateString() {
- return toString(new java.util.Date(System.currentTimeMillis()), DateTime.sdfDateOnly);
- }
- /**
- * 以配置的Datetime格式返回系统日期时间
- *
- * @return 系统日期时间
- */
- public static String getSysDateTimeString() {
- return toString(new java.util.Date(System.currentTimeMillis()), DateTime.sdfDateTime);
- }
- /**
- * 以固定的YYYY/MM/DD HH24:MI:SS.sss格式返回系统日期时间
- *
- * @return 系统日期时间,准确到毫秒, YYYY/MM/DD HH24:MI:SS.sss
- * @since 2003/09/04
- */
- public static String getSysDateFullString() {
- return toStringFull(System.currentTimeMillis());
- }
- /*****************************************************
- * java.sql.Date ==> String 的转换函数
- *****************************************************/
- /**
- * 利用指定日期格式把字符串转化到java.sql.Date类型
- *
- * @param sDate
- * 日期字符串
- * @return 返回java.sql.Date类型的日期
- * @history 2004/04/30 保证日期格式的灵活性
- */
- public static java.sql.Date toSqlDate(String sDate) {
- // return toSqlDate( sDate, this.DATE_FORMAT_DATEONLY );
- // java.sql.Date.value() 要求的格式必须为YYYY-MM-DD
- return new java.sql.Date(DateTime.toDate(sDate).getTime());
- // return java.sql.Date.valueOf(sDate.replace('/', '-'));
- }
- /*****************************************************
- * String ==> java.sql.Date 的转换函数
- *****************************************************/
- /**
- * 将java.sql.Date类型的日期转换到日期字符串(格式为YYYY/MM/DD)
- *
- * @param dt
- * java.sql.Date 日期
- * @return 字符串(YYYY/MM/DD)
- * @history 2004/04/30 保证日期格式的灵活性
- */
- public static String toSqlDateString(java.sql.Date dt) {
- return DateTime.toString(new Date(dt.getTime()));
- // String temp = null;
- // temp = dt.toString();
- // return temp.replace('-', '/');
- }
- /*****************************************************
- * java.sql.Timestamp ==> String 的转换函数
- *****************************************************/
- /**
- * 转换GregorianCalendar Object到java.sql.Timestamp
- *
- * @param gcal
- * GregorianCalendar Object
- * @return java.sql.Timestamp object
- * @since 1.0
- */
- /*
- * public static java.sql.Timestamp toSqlTimestamp(GregorianCalendar gcal){
- * return new
- * java.sql.Timestamp((long)gcal.get(GregorianCalendar.MILLISECOND) ); }
- */
- /**
- * 将符合定义格式的Date only日期字符串转换到java.sql.Timestamp类型的时间戳
- *
- * @param sDate
- * 日期字符串(格式=YYYY/MM/DD)
- * @return java.sql.Timestamp类型时间戳
- */
- public static java.sql.Timestamp toSqlTimestamp(String sDate) {
- if (sDate == null)
- return null;
- // if (sDate.length() != DateTime.DATE_FORMAT_DATEONLY.length())
- // return null;
- return toSqlTimestamp(sDate, DateTime.DATE_FORMAT_DATEONLY);
- }
- /**
- * 利用指定格式的日期字符串转化到java.sql.Timestamp时间戳
- *
- * @param sDate
- * 日期字符串
- * @param sFmt
- * 日期格式 DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME
- * @return java.sql.Timestamp 时间戳
- * @history 2004/04/29 先把String转换为Date,然后利用Date的long 毫秒值来取得Timestamp
- * @history 2004/05/12 增加日期转换失败为Null的情况下,应该返回Null的判断
- */
- public static java.sql.Timestamp toSqlTimestamp(String sDate, String sFmt) {
- Date date = DateTime.toDate(sDate, sFmt);
- if (date != null) {
- return new java.sql.Timestamp(date.getTime());
- } else {
- return null;
- }
- /*
- * String temp = null; if (sDate == null || sFmt == null) return null;
- * if (sDate.length() != sFmt.length()) return null; if
- * (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) { temp =
- * sDate.replace('/', '-'); temp = temp + ".000000000"; } else if
- * (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { temp =
- * sDate.replace('/', '-'); temp = temp + " 00:00:00.000000000";
- *
- * } else { return null; } //java.sql.Timestamp.value()
- * 要求的格式必须为yyyy-mm-dd hh:mm:ss.fffffffff return
- * java.sql.Timestamp.valueOf(temp);
- */
- }
- /*****************************************************
- * String ==> java.sql.Date 的转换函数
- *****************************************************/
- /**
- * 将java.sql.Timestamp类型的时间戳转换到字符串格式为YYYY/MM/DD
- *
- * @param dt
- * java.sql.Timestamp类型的时间戳
- * @return 日期字符串
- */
- public static String toSqlTimestampString(java.sql.Timestamp dt) {
- // if (dt == null)
- // return null;
- return toSqlTimestampString(dt, DateTime.DATE_FORMAT_DATEONLY);
- }
- /**
- * 将java.sql.Timestamp时间戳转换到日期字符串,格式为YYYY/MM/DD HH24:MI
- *
- * @param dt
- * java.sql.Timestamp日期时间戳
- * @return 日期字符串
- * @history 2004/04/29 schema-config.xml
- * 中定义的Datetime格式可能小于16,所以返回是的substring ,应该先判断长度
- */
- public static String toSqlTimestampString2(java.sql.Timestamp dt) {
- if (dt == null)
- return null;
- String temp = toSqlTimestampString(dt, DateTime.DATE_FORMAT_DATETIME);
- return temp.substring(0, 16 < DateTime.DATE_FORMAT_DATETIME.length() ? 16 : DateTime.DATE_FORMAT_DATETIME.length());
- }
- /**
- * 将java.sql.Timestamp时间戳转换到日期字符串,schema-config.xml中指定的datetime 格式
- * NULL的场合,转换为NULL
- *
- * @param dt
- * java.sql.Timestamp 日期时间戳
- * @return 日期字符串
- */
- public static String toString(java.sql.Timestamp dt) {
- return dt == null ? "" : toSqlTimestampString2(dt);
- }
- /**
- * 将指定的java.sql.Timestamp类型的日期转变为指定的中文日期格式字符串 YYYY年MM月DD日 HH:MM:SS
- *
- * @param timestamp
- * java.sql.Timestamp类型的日期
- * @return 中文日期格式字符串
- * @deprecated
- */
- public static String convertTimestampToChinaCalendar(Timestamp timestamp) {
- StringBuffer sb = new StringBuffer();
- if (timestamp == null) {
- sb.append(" ");
- } else {
- sb = new StringBuffer();
- sb.append(DateTime.getYearOfTimestamp(timestamp));
- sb.append("年");
- sb.append(DateTime.getMonthOfTimestamp(timestamp));
- sb.append("月");
- sb.append(DateTime.getDayOfTimestamp(timestamp));
- sb.append("日");
- sb.append(" ");
- sb.append(DateTime.getHourOfTimestamp(timestamp));
- sb.append(":");
- if (DateTime.getMinuteOfTimestamp(timestamp) < 10) {
- sb.append(0);
- sb.append(DateTime.getMinuteOfTimestamp(timestamp));
- } else {
- sb.append(DateTime.getMinuteOfTimestamp(timestamp));
- }
- sb.append(":");
- if (DateTime.getSecondOfTimestamp(timestamp) < 10) {
- sb.append(0);
- sb.append(DateTime.getSecondOfTimestamp(timestamp));
- } else {
- sb.append(DateTime.getSecondOfTimestamp(timestamp));
- }
- }
- return sb.toString();
- }
- /**
- * 根据指定的java.sql.Timestamp类型的时间戳转换到指定格式的字符串
- *
- * @param dt
- * java.sql.Timestamp类型的时间戳
- * @param sFmt
- * 日期格式,DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME
- * @return 指定格式的日期字符串
- * @history 2004/04/29: 先转换为Date,然后利用Date ==> String
- * @see #toString(Date,String)
- */
- public static String toSqlTimestampString(java.sql.Timestamp dt, String sFmt) {
- if (dt == null) {
- return null;
- }
- return DateTime.toString(new Date(dt.getTime()), sFmt);
- }
- /**
- * 将java.sql.Timestamp类型的日期转换到HH24:MI格式的时间字符串
- *
- * @param dt
- * java.sql.Timestamp类型的字符串
- * @return HH24:MI格式的时间字符串
- */
- public static String toHourMinString(java.sql.Timestamp dt) {
- String temp = null;
- temp = dt.toString();
- // int len = 0;
- // len = DateTime.DATE_FORMAT_DATETIME.length ();
- temp = temp.substring(11, 16);
- return temp;
- }
- /*****************************************************
- * java.sql.Timestamp +/- 几天的计算函数
- *****************************************************/
- /**
- * 判断指定的日期是否是一个月的最后一天
- *
- * @param obj
- * GregorianCalendar object
- */
- private static boolean isLastDayOfMonth(GregorianCalendar obj) {
- int year = obj.get(GregorianCalendar.YEAR);
- int month = obj.get(GregorianCalendar.MONTH) + 1;
- int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
- if (obj.isLeapYear(year)) {
- // Modified by ChenJP, 2004/03/30, 2月份没有30号
- // if (day == DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]){
- if (day >= DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]) {
- return true;
- }
- } else {
- // Modified by ChenJP, 2004/03/30, 2月份没有30号
- // if (day == DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1])
- if (day >= DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]) {
- return true;
- }
- }
- return false;
- }
- /**
- * 在java.sql.Timestamp类型时间戳上增加/减少几个月得到的新的时间戳
- *
- * @param timestamp
- * java.sql.Timestamp类型的时间戳
- * @param mon
- * 增加/减少的月数
- * @return java.sql.Timestamp类型的时间戳
- */
- public static java.sql.Timestamp addMonths(java.sql.Timestamp timestamp, int mon) {
- java.sql.Timestamp out = null;
- GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
- // month : 0 -- 11
- int year = obj.get(GregorianCalendar.YEAR);
- int month = obj.get(GregorianCalendar.MONTH) + 1;
- int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
- month += mon;
- if (month < 1) {
- month += 12;
- year--;
- } else if (month > 12) {
- month -= 12;
- year++;
- }
- // 对应通沙2004/03/30出现的Bug
- boolean laterThanLastMonth = false;
- if (obj.isLeapYear(year)) {
- laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]);
- } else {
- laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]);
- }
- // 是否是一个月的最后一天
- if (isLastDayOfMonth(obj) || laterThanLastMonth) {
- if (obj.isLeapYear(year)) {
- day = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
- } else {
- day = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
- }
- }
- // Modified by SJNS/zq at 2009.10.29 支持HH:MM:SS
- String temp = DateTime.formatYMD(year, month, day);
- temp = temp + " " + DateTime.toHourMinString(timestamp) + ":00";
- out = DateTime.toSqlTimestamp(temp, DateTime.DATE_FORMAT_DATETIME);
- return out;
- }
- /**
- * 在java.sql.Timestamp类型时间戳上增加/减少几天得到的新的时间戳
- *
- * @param timestamp
- * java.sql.Timestamp类型的时间戳
- * @param days
- * 增加/减少的天数
- * @return java.sql.Timestamp类型的时间戳
- */
- public static java.sql.Timestamp addDays(java.sql.Timestamp timestamp, int days) {
- java.util.Date date = DateTime.convertTimestampToDate(timestamp);
- long temp = date.getTime();
- return new java.sql.Timestamp(temp + DateTime.DAY_MILLI * days);
- }
- // ***********************************************************************
- // Framework DateTime.java 原有的method
- // ********************************************************************
- /**
- * Return the current date as a formatted string
- *
- * @return String The current date
- * @deprecated
- */
- public synchronized static String getDateString() {
- return DateTime.toString(new Date(System.currentTimeMillis()));
- } /* getDateString() */
- /**
- *
- *
- * @param now
- * @return
- * @deprecated
- */
- public synchronized static String getDateString(Calendar now) {
- return DateTime.toString(new Date(now.getTimeInMillis()), DateTime.DATE_FORMAT_DATETIME);
- } /* getDateString(Calendar) */
- /**
- * Get a date/time field formatted for insertion into a database Value of
- * the date/time is the current date and time
- *
- * @return String The formatted date time field
- * @deprecated
- */
- public synchronized static String getDateTimeForDB() {
- return getDateTimeForDB(new Date());
- } /* getDateTimeForDB() */
- /**
- * Get a date/time field formatted for insertion into a database Value of
- * the date/time is the current date and time
- *
- * @param year
- * @param month
- * @param day
- * @param hour
- * @param min
- * @param sec
- * @return String The formatted date time field
- * @deprecated
- */
- public synchronized static String getDateTimeForDB(int year, int month, int day, int hour, int min, int sec) {
- Calendar cal = new GregorianCalendar(year, month, day, hour, min, sec);
- return getDateTimeForDB(cal.getTime()).trim();
- } /* getDateTimeForDB(int, int, int, int, int, int) */
- /**
- * Get a date/time field formatted for insertion into a database
- *
- * @param date
- * @return String The formatted date time field
- * @deprecated
- */
- public synchronized static String getDateTimeForDB(Date date) {
- // Format the current time. SimpleDateFormat formatter
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
- Date currentTime_1 = date;
- String dateString = formatter.format(currentTime_1);
- return dateString.trim();
- } /* getDateTimeForDB(Date) */
- /**
- * Get a date/time string containing the current date and time, formatted
- * for user readability
- *
- * @return String The formatted current date/time
- * @deprecated
- */
- public synchronized static String getDateTimeString() {
- /*
- * Calendar rightNow = Calendar.getInstance(); return new String("" +
- * (rightNow.get(Calendar.MONTH) + 1)+ "/" +
- * rightNow.get(Calendar.DAY_OF_MONTH) + "/" +
- * rightNow.get(Calendar.YEAR) + " " + rightNow.get(Calendar.HOUR) + ":"
- * + rightNow.get(Calendar.MINUTE) + ":" +
- * rightNow.get(Calendar.SECOND)).trim();
- */
- // Format the current time. SimpleDateFormat formatter
- SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
- Date currentTime_1 = new Date();
- String dateString = formatter.format(currentTime_1);
- return dateString.trim();
- } /* getDateTimeString() */
- /**
- * Get a date/time string for a given date and time, formatted for user
- * readability
- *
- * @return String The formatted current date/time
- * @deprecated
- */
- public synchronized static String getDateTimeString(Date date) {
- // Format the current time. SimpleDateFormat formatter
- SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
- String dateString = formatter.format(date);
- return dateString.trim();
- } /* getDateTimeString() */
- /**
- * get the <b>min</b> dateTime just as "0001/01/01" or "0001/01/01 00:00:00"
- *
- * @param sFmt
- * date format by user's setting
- * @return String the
- */
- public synchronized static String getMinDateTime(String sFmt) {
- Calendar cal = new GregorianCalendar(1, 0, 1, 0, 0, 0);
- Date dt = cal.getTime();
- return toString(dt, sFmt);
- } /* getMinDateTime(String) */
- /**
- * added by huangzz on 2007/01/05<br>
- * get the <b>max</b> dateTime just as "9999/12/31" or "9999/12/31 23:59:59"
- *
- * @param sFmt
- * date format by user's setting
- * @return String
- */
- public synchronized static String getMaxDateTime(String sFmt) {
- Calendar cal = new GregorianCalendar(9999, 11, 31, 23, 59, 59);
- Date dt = cal.getTime();
- return toString(dt, sFmt);
- } /* getMaxDateTime(String) */
- } /* DateTime */
|