DateTime.java 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. package com.xtframe.util;
  2. import java.sql.Timestamp;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.GregorianCalendar;
  7. import java.util.logging.Logger;
  8. /**
  9. * 日期相关的共通
  10. */
  11. public class DateTime {
  12. private final static Logger LOG = Logger.getLogger(DateTime.class.getName());
  13. public DateTime() {
  14. }
  15. public static final String minDate = "1970-01";
  16. public static final String maxDate = "2099-01";
  17. /** 一天的毫秒数 */
  18. public static final long DAY_MILLI = 24 * 60 * 60 * 1000;
  19. /** 1小时的毫秒数 */
  20. public static final long HOUR_MILLI = 60 * 60 * 1000;
  21. /**
  22. * 要用到的缺省的Date only 格式定义 年/月/日
  23. **/
  24. public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 年/月/日
  25. /**
  26. * 要用到的缺省的Date time 格式定义 年/月/日 时:分:秒 YYYY/MM/DD HH:MM:SS
  27. **/
  28. public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
  29. /**
  30. * 闰年的每个月的天数数组
  31. **/
  32. public static int[] DAY_OF_MONTH_LEAP_YEAR = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  33. /**
  34. * 非闰年的每个月的天数数组
  35. **/
  36. public static int[] DAY_OF_MONTH_NON_LEAP_YEAR = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  37. /** Global SimpleDateFormat object */
  38. private static SimpleDateFormat sdfDateOnly = new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY);
  39. /** Datetime formatter */
  40. private static SimpleDateFormat sdfDateTime = new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME);
  41. /** Datetime full 格式的定义  */
  42. private static SimpleDateFormat sdfDateTimeFull = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
  43. /*****************************************************
  44. * java.util.Date ==> String 的转换函数
  45. *****************************************************/
  46. /**
  47. * String到Date类型
  48. *
  49. * @param sDate
  50. * 日期字符串
  51. * @return 日期类型
  52. */
  53. public static java.util.Date toDate(String sDate) {
  54. return toDate(sDate, DateTime.sdfDateOnly);
  55. }
  56. /**
  57. * 根据指定的格式将日期字符串转换为DATE类型
  58. *
  59. * @param sDate
  60. * 日期字符串
  61. * @param sFmt
  62. * 日期格式
  63. * @return 转换后的DATE类型
  64. * @see DateTime#DATE_FORMAT_DATEONLY
  65. * @see DateTime#DATE_FORMAT_DATETIME
  66. * @todo 日期String的长度不对的情况,需要对应
  67. */
  68. public static java.util.Date toDate(String sDate, String sFmt) {
  69. if (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) {// Date time
  70. return toDate(sDate, DateTime.sdfDateTime);
  71. } else if (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { // Date only
  72. return toDate(sDate, DateTime.sdfDateOnly);
  73. } else {
  74. // return null;
  75. return toDateAgain(sDate);
  76. }
  77. }
  78. /**
  79. * 利用指定SimpleDateFormat instance转化String到Date
  80. *
  81. * @param sDate
  82. * Date string
  83. * @param formatter
  84. * SimpleDateFormat instance
  85. * @return Date object
  86. * @since 1.0
  87. * @history 2004/05/01 ChenJP: parsing 失败的时候,试图做一些相对智能的处理
  88. */
  89. private static java.util.Date toDate(String sDate, SimpleDateFormat formatter) {
  90. if (sDate == null) {
  91. return null;
  92. }
  93. java.util.Date dt = null;
  94. try {
  95. dt = formatter.parse(sDate);
  96. } catch (Exception e) {
  97. // log.warn("Exception when parsing date in DateTime.toDate(), original String="
  98. // +sDate + ",formatter pattern=" + formatter.toPattern());
  99. // dt = null;
  100. dt = toDateAgain(sDate);
  101. }
  102. return dt;
  103. }
  104. /**
  105. * 试图做一些相对智能的日期处理
  106. *
  107. * @param sDate
  108. * @return 处理成功,返回Date object;
  109. * @return 处理失败,return null
  110. * @TODO: 是否可能对分隔符号,如"/" 或者":"也做一些智能的处理
  111. */
  112. private static java.util.Date toDateAgain(String sDate) {
  113. if (sDate == null) {
  114. return null;
  115. }
  116. java.util.Date dt = null;
  117. SimpleDateFormat formatter = null;
  118. // 试图解决日期String长度不对的问题
  119. if (sDate.length() < DateTime.DATE_FORMAT_DATEONLY.length()) { // 长度太短
  120. return null;
  121. } else if (sDate.length() < DateTime.DATE_FORMAT_DATETIME.length()) {
  122. // 根据in Date string的长度,new 一个SimpleDateFormat object,这种效率不高
  123. formatter = new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME.substring(0, sDate.length()));
  124. } else if (sDate.length() > DateTime.DATE_FORMAT_DATETIME.length()) { // in
  125. // 日期String太长,截断后处理
  126. sDate = sDate.substring(0, DateTime.DATE_FORMAT_DATETIME.length());
  127. formatter = DateTime.sdfDateTime;
  128. }
  129. try {
  130. dt = formatter.parse(sDate);
  131. } catch (Exception e) {
  132. LOG.warning("Exception when parsing date in DateTime.toDateAgain(), original String=" + sDate + ",formatter pattern=" + formatter.toPattern());
  133. dt = null;
  134. }
  135. return dt;
  136. }
  137. /**
  138. * 将一个long 型的毫秒值转换为日期String
  139. *
  140. * @param millis
  141. * @return 返回日期的格式由schema-config.xml中的DateTime 格式指定,缺省为yyyy/mm/dd hi24:mi:ss
  142. */
  143. public static String toString(long millis) {
  144. return toString(new java.util.Date(millis), DateTime.sdfDateTime);
  145. }
  146. /**
  147. * 将一个long 型的毫秒值转换为日期String
  148. *
  149. * @param millis
  150. * @return 精确到毫秒的日期String,格式固定为"yyyy/MM/dd HH:mm:ss.SSS" deprecated
  151. * 不推荐使用,目前仅Debug method中需要使用此method
  152. */
  153. public static String toStringFull(long millis) {
  154. return toString(new java.util.Date(millis), DateTime.sdfDateTimeFull);
  155. }
  156. /*****************************************************
  157. * String ==> java.util.Date 的转换函数
  158. *****************************************************/
  159. /**
  160. * 将日期DATE的变量转换为年月日字符串
  161. *
  162. * @param dt
  163. * 日期类型的变量
  164. * @return 返回转换以后的字符串
  165. */
  166. public static String toString(java.util.Date dt) {
  167. return toString(dt, DateTime.sdfDateOnly);
  168. }
  169. /**
  170. * 将日期DATE的变量转换为指定格式的字符串
  171. *
  172. * @param dt
  173. * 日期类型的变量
  174. * @param sFmt
  175. * 日期格式,DateTime中定义的两个常量,最终的格式来源与schma-config.xml中的定义.
  176. * @return 返回转换以后的字符串;如果指定的格式不符合定义的两种缺省格式,将返回Date.toString
  177. */
  178. public static String toString(java.util.Date dt, String sFmt) {
  179. // log.debug("DateTime.toString sFmt="+sFmt);
  180. // log.debug("DateTime.sdfDateTime="+DATE_FORMAT_DATETIME);
  181. if (dt == null) {
  182. return "";
  183. }
  184. if (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) { // Date time
  185. return toString(dt, DateTime.sdfDateTime);
  186. } else if (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { // Date only
  187. return toString(dt, DateTime.sdfDateOnly);
  188. } else { // Default , YYYY/MM/DD
  189. LOG.severe("Unknown date format in DateTime.toString, dateFormat = " + sFmt);
  190. return dt.toString();
  191. }
  192. }
  193. /**
  194. * 利用指定SimpleDateFormat instance转换java.util.Date到String
  195. *
  196. * @param dt
  197. * java.util.Date instance
  198. * @param formatter
  199. * SimpleDateFormat Instance
  200. * @return 转换为的String
  201. * @since 1.0
  202. */
  203. private static String toString(java.util.Date dt, SimpleDateFormat formatter) {
  204. if (dt == null || formatter == null) {
  205. return "";
  206. }
  207. String sRet = null;
  208. try {
  209. sRet = formatter.format(dt);
  210. } catch (Exception e) {
  211. LOG.severe("Exception in DateTime.toString");
  212. sRet = dt.toString();
  213. }
  214. return sRet;
  215. }
  216. /**
  217. * 在java.util.Date类型时间戳上增加/减少几天得到的新的时间戳
  218. *
  219. * @param date
  220. * java.util.Date类型的时间戳
  221. * @param days
  222. * 增加/减少的天数(此值>0表示是增加,<0表示是减少)
  223. * @return 计算后的日期
  224. */
  225. public static java.util.Date addDays(java.util.Date date, int days) {
  226. long temp = date.getTime();
  227. return new java.util.Date(temp + DateTime.DAY_MILLI * days);
  228. }
  229. /**
  230. * 把java.util.Date类型的日期转换为java.util.GregorianCalendar的日期
  231. *
  232. * @param date
  233. * java.util.Date类型的日期
  234. * @return java.util.GregorianCalendar的日期
  235. */
  236. private static java.util.GregorianCalendar convertToCalendar(java.util.Date date) {
  237. GregorianCalendar obj = new GregorianCalendar();
  238. obj.setTime(date);
  239. return obj;
  240. }
  241. /**
  242. * 在Date String时间戳上增加/减少几天得到的新的时间戳
  243. *
  244. * @param date
  245. * String类型的时间戳,date的格式应该是定义在schema-config.xml中的Date Only的格式
  246. * @param days
  247. * 增加/减少的天数
  248. * @return String类型的时间戳
  249. * @see DateTime#toDate(String)
  250. * @see DateTIme#addDays(String)
  251. */
  252. public static String addDays(String date, int days) {
  253. try {
  254. return DateTime.toString(DateTime.addDays(DateTime.toDate(date), days));
  255. } catch (Exception e) {
  256. LOG.severe("Exception in addDays,date=" + date + ",days add=" + days);
  257. return "";
  258. }
  259. }
  260. /**
  261. * 在String类型时间戳上增加/减少几个月得到的新的时间戳 日期的格式定义在schema-config.xml中
  262. *
  263. * @param paraDate
  264. * String类型的时间
  265. * @param mon
  266. * 增加/减少的月数
  267. * @return String类型的时间
  268. */
  269. public static String addMonths(String paraDate, int mon) {
  270. java.util.Date date = DateTime.toDate(paraDate);
  271. if (date == null) {
  272. return "";
  273. }
  274. GregorianCalendar obj = DateTime.convertToCalendar(date);
  275. // month : 0 -- 11
  276. int year = obj.get(GregorianCalendar.YEAR);
  277. int month = obj.get(GregorianCalendar.MONTH) + 1;
  278. int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
  279. month += mon;
  280. if (month < 1) {
  281. month += 12;
  282. year--;
  283. } else if (month > 12) {
  284. month -= 12;
  285. year++;
  286. }
  287. int lastDay = 0;
  288. // 对应通沙2004/03/30出现的Bug
  289. boolean laterThanLastMonth = false;
  290. if (obj.isLeapYear(year)) {
  291. laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]);
  292. } else {
  293. laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]);
  294. }
  295. if (isLastDayOfMonth(obj) || laterThanLastMonth) {
  296. if (obj.isLeapYear(year)) {
  297. lastDay = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
  298. } else {
  299. lastDay = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
  300. }
  301. if (lastDay < day) { // 结果的月份没有对应的天,则缩小天
  302. day = lastDay;
  303. }
  304. }
  305. obj.set(year, month - 1, day);
  306. java.util.Date resultDate = new java.util.Date(obj.getTimeInMillis());
  307. return DateTime.toString(resultDate);
  308. }
  309. /****************************************************
  310. * 后续要deprecated 的method
  311. *****************************************************/
  312. /**
  313. * 比较两个java.sql.Timestamp instance 的年月日部分是否相同
  314. *
  315. * @param date1
  316. * java.sql.Timestamp类型的时间措1
  317. * @param date2
  318. * java.sql.Timestamp类型的时间措2
  319. * @return 返回True : 年月日部分相同
  320. * @return 返回False: 年月日部分不同
  321. * @deprecated
  322. */
  323. public static boolean isSameDay(java.sql.Timestamp date1, java.sql.Timestamp date2) {
  324. String s1, s2 = null;
  325. s1 = date1.toString().substring(0, DateTime.DATE_FORMAT_DATEONLY.length());
  326. s2 = date2.toString().substring(0, DateTime.DATE_FORMAT_DATEONLY.length());
  327. // cat.debug("in isSameDay() , s1=" + s1 + ",s2=" + s2);
  328. return s1.equalsIgnoreCase(s2);
  329. }
  330. /**
  331. * 取得指定日期所在周的第一天(Sunday)
  332. *
  333. * @param timestamp
  334. * java.sql.Timestamp类型的日期
  335. * @return 返回java.sql.Timestamp类型的日期
  336. */
  337. public static java.sql.Timestamp getFirstDayOfWeek(java.sql.Timestamp timestamp) {
  338. int no = DateTime.getWeekdayOfTimestamp(timestamp);
  339. java.sql.Timestamp out = DateTime.addDays(timestamp, 1 - no);
  340. return out;
  341. }
  342. /**
  343. * 取得指定日期所在周的最后一天(Saturday)
  344. *
  345. * @param timestamp
  346. * , java.sql.Timestamp类型的时间戳
  347. * @return java.sql.Timestamp类型的时间戳
  348. */
  349. public static java.sql.Timestamp getLastDayOfWeek(java.sql.Timestamp timestamp) {
  350. int no = DateTime.getWeekdayOfTimestamp(timestamp);
  351. java.sql.Timestamp out = DateTime.addDays(timestamp, 7 - no);
  352. return out;
  353. }
  354. /**
  355. * 取得指定日期所在月的1号所在周的Sunday(可能是上个月的日期)
  356. *
  357. * @param timestamp
  358. * java.sql.Timestamp类型的日期
  359. * @return 返回java.sql.Timestamp类型的日期
  360. */
  361. public static java.sql.Timestamp getFirstSundayOfMonth(java.sql.Timestamp timestamp) {
  362. java.sql.Timestamp out = null;
  363. if (timestamp == null)
  364. return null;
  365. out = getFirstDayOfMonth(timestamp);
  366. out = DateTime.getFirstDayOfWeek(out);
  367. return out;
  368. }
  369. /**
  370. * 取得指定日期所在月的最后一天(如31号)所在周的Saturday(可能是下个月的日期)
  371. *
  372. * @param timestamp
  373. * , java.sql.Timestamp Object
  374. * @return java.sql.Timestamp instance
  375. */
  376. public static java.sql.Timestamp getLastSaturdayOfMonth(java.sql.Timestamp timestamp) {
  377. java.sql.Timestamp out = null;
  378. if (timestamp == null)
  379. return null;
  380. // cat.debug("In timestamp=" + timestamp.toString() );
  381. out = getLastDayOfMonth(timestamp);
  382. // cat.debug("LastDayOfMonth=" + out.toString() );
  383. out = DateTime.getLastDayOfWeek(out);
  384. // cat.debug("LastSaturdayOfMonth=" + out.toString() );
  385. return out;
  386. }
  387. /**
  388. * 取得指定日期所在月的第一天
  389. *
  390. * @param timestamp
  391. * , java.sql.Timestamp Object
  392. * @return java.sql.Timestamp instance
  393. */
  394. public static java.sql.Timestamp getFirstDayOfMonth(java.sql.Timestamp timestamp) {
  395. java.sql.Timestamp out = null;
  396. if (timestamp == null)
  397. return null;
  398. int day = DateTime.getDayOfTimestamp(timestamp);
  399. out = DateTime.addDays(timestamp, 1 - day);
  400. // out = DateTime.getFirstDayOfWeek(out);
  401. return out;
  402. }
  403. /**
  404. * 取得两个日期之间的日数
  405. *
  406. * @param t1
  407. * java.sql.Timestamp类型的日期1
  408. * @param t2
  409. * java.sql.Timestamp类型的日期2
  410. * @return 返回t1到t2间的日数。如果t2在t1之后,返回正数,否则返回负数
  411. */
  412. public static long daysBetween(java.sql.Timestamp t1, java.sql.Timestamp t2) {
  413. return (t2.getTime() - t1.getTime()) / DAY_MILLI;
  414. }
  415. /**
  416. * Format year/month/day to YYYY/MM/DD format
  417. *
  418. * @param year
  419. * @param month
  420. * @param day
  421. * @return YYYY/MM/DD format String
  422. */
  423. private static String formatYMD(int year, int month, int day) {
  424. String temp = String.valueOf(year) + "/";
  425. if (month < 10) {
  426. temp += "0" + String.valueOf(month) + "/";
  427. } else {
  428. temp += String.valueOf(month) + "/";
  429. }
  430. if (day < 10) {
  431. temp += "0" + String.valueOf(day);
  432. } else {
  433. temp += String.valueOf(day);
  434. }
  435. return temp;
  436. }
  437. /**
  438. * 取得指定日期的所在月的最后一天 目前只支持YYYY/MM/DD格式
  439. *
  440. * @param timestamp
  441. * Timestamp类型的日期
  442. * @return Timestamp类型的日期
  443. */
  444. public static java.sql.Timestamp getLastDayOfMonth(java.sql.Timestamp timestamp) {
  445. java.sql.Timestamp out = null;
  446. if (timestamp == null)
  447. return null;
  448. // out = (java.sql.Timestamp )timestamp.clone() ;
  449. // day = DateTime.getDayOfTimestamp(timestamp);
  450. // int month = DateTime.getMonthOfTimestamp(timestamp);
  451. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  452. // month : 0 -- 11
  453. int day = 0;
  454. int year = obj.get(GregorianCalendar.YEAR);
  455. int month = obj.get(GregorianCalendar.MONTH) + 1;
  456. if (obj.isLeapYear(obj.get(GregorianCalendar.YEAR))) {
  457. day = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
  458. } else {
  459. day = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
  460. }
  461. /*
  462. * modified by ChenJP 2000/11/16 String temp = String.valueOf(year) +
  463. * "/"; if( month < 10 ){ temp += "0" + String.valueOf(month) + "/";
  464. * }else{ temp += String.valueOf(month)+ "/"; } if( day < 10 ){ temp +=
  465. * "0" + String.valueOf(day); }else{ temp += String.valueOf(day); }
  466. * //cat.debug("temp=" + temp); out = DateTime.toSqlTimestamp(temp);
  467. */
  468. out = DateTime.toSqlTimestamp(DateTime.formatYMD(year, month, day));
  469. /*
  470. * obj.set(GregorianCalendar.DAY_OF_MONTH , day) ; out = new
  471. * java.sql.Timestamp(obj.getTimeInMillis()); out =
  472. * DateTime.addDays(timestamp, 10) ; out =
  473. * DateTime.getFirstDayOfWeek(out);
  474. */
  475. return out;
  476. }
  477. /*****************************************************
  478. * 把java.sql.Timestamp Object 取出各部分的值
  479. *****************************************************/
  480. /**
  481. * 从java.sql.Timestamp日期信息中取出年
  482. *
  483. * @param timestamp
  484. * java.sql.Timestamp日期信息
  485. * @return 返回年
  486. */
  487. public static int getYearOfTimestamp(java.sql.Timestamp timestamp) {
  488. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  489. return obj.get(GregorianCalendar.YEAR);
  490. }
  491. /**
  492. * 从java.sql.Timestamp 日期中取出月份信息
  493. *
  494. * @param timestamp
  495. * java.sql.Timestamp类型的日期
  496. * @return 返回月份(1 -- 12 )
  497. */
  498. public static int getMonthOfTimestamp(java.sql.Timestamp timestamp) {
  499. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  500. return (obj.get(GregorianCalendar.MONTH) + 1);
  501. }
  502. /**
  503. * 从java.sql.Timestamp日期中取出日信息
  504. *
  505. * @param timestamp
  506. * java.sql.Timestamp类型的日期
  507. * @return 返回日信息
  508. */
  509. public static int getDayOfTimestamp(java.sql.Timestamp timestamp) {
  510. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  511. return obj.get(GregorianCalendar.DAY_OF_MONTH);
  512. }
  513. /**
  514. * 从java.sql.Timestamp类型的日期中取出周日1-7的信息
  515. *
  516. * @param timestamp
  517. * java.sql.Timestamp类型的日期
  518. * @return WeekDay周日信息
  519. */
  520. public static int getWeekdayOfTimestamp(java.sql.Timestamp timestamp) {
  521. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  522. return obj.get(GregorianCalendar.DAY_OF_WEEK);
  523. }
  524. /**
  525. * 返回指定日期的零时的Timestamp值
  526. *
  527. * @return 返回指定日期的零时的Timestamp值
  528. * @param timestamp
  529. * 返回指定日期的零时的Timestamp值
  530. */
  531. public static java.sql.Timestamp getZeroTime(java.sql.Timestamp timestamp) {
  532. if (timestamp == null) {
  533. return null;
  534. }
  535. return DateTime.toSqlTimestamp(DateTime.toString(new Date(timestamp.getTime())));
  536. // String tempStr = timestamp.toString().substring(0, 10);
  537. // return DateTime.toSqlTimestamp(tempStr);
  538. }
  539. /**
  540. * 从java.sql.Timestamp类型的日期中取出时间:分钟信息 HH:MM
  541. *
  542. * @param timestamp
  543. * java.sql.Timestamp类型的日期
  544. * @return 返回HH:MM格式的时分信息
  545. */
  546. public static String getHourAndMinuteString(java.sql.Timestamp timestamp) {
  547. String out = null;
  548. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  549. int hour = obj.get(GregorianCalendar.HOUR_OF_DAY);
  550. int minute = obj.get(GregorianCalendar.MINUTE);
  551. if (minute < 10) {
  552. out = String.valueOf(hour) + ":0" + String.valueOf(minute);
  553. } else {
  554. out = String.valueOf(hour) + ":" + String.valueOf(minute);
  555. }
  556. return out;
  557. }
  558. /**
  559. * 从java.sql.Timestamp Object 中取出24小时制的时间信息
  560. *
  561. * @param timestamp
  562. * java.sql.Timestamp类型的日期
  563. * @return 24小时制的hour value
  564. */
  565. public static int getHourOfTimestamp(java.sql.Timestamp timestamp) {
  566. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  567. return obj.get(GregorianCalendar.HOUR_OF_DAY);
  568. }
  569. /**
  570. * 从java.sql.Timestamp类型的日期中取出分钟信息
  571. *
  572. * @param timestamp
  573. * java.sql.Timestamp类型的日期
  574. * @return 返回分钟信息
  575. */
  576. public static int getMinuteOfTimestamp(java.sql.Timestamp timestamp) {
  577. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  578. return obj.get(GregorianCalendar.MINUTE);
  579. }
  580. /**
  581. * 从java.sql.Timestamp类型的日期中取出秒信息
  582. *
  583. * @param timestamp
  584. * java.sql.Timestamp类型的日期
  585. * @return 返回秒信息
  586. */
  587. public static int getSecondOfTimestamp(java.sql.Timestamp timestamp) {
  588. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  589. return obj.get(GregorianCalendar.SECOND);
  590. }
  591. /**
  592. * 把java.sql.Timestamp类型的日期转换为java.util.GregorianCalendar的日期
  593. *
  594. * @param timestamp
  595. * java.sql.Timestamp类型的日期
  596. * @return java.util.GregorianCalendar的日期
  597. * @deprecated
  598. */
  599. public static java.util.GregorianCalendar convertToCalendar(java.sql.Timestamp timestamp) {
  600. GregorianCalendar obj = new GregorianCalendar();
  601. // Modified by ChenJP 2000/11/17
  602. obj.setTime(DateTime.convertTimestampToDate(timestamp));
  603. // 下面的method不能用,long ==> int 精度不对
  604. // obj.set(GregorianCalendar.MILLISECOND , (int)timestamp.getTime() );
  605. return obj;
  606. }
  607. /**
  608. * 把java.sql.Timestamp类型的日期转换为java.util.Date类型的日期
  609. *
  610. * @param timestamp
  611. * java.sql.Timestamp类型的日期
  612. * @return java.util.Date类型的日期
  613. */
  614. public static java.util.Date convertTimestampToDate(java.sql.Timestamp timestamp) {
  615. java.util.Date date = null;
  616. date = new Date(timestamp.getTime());
  617. return date;
  618. }
  619. /*****************************************************
  620. * 取系统日期、时间的函数
  621. *****************************************************/
  622. /**
  623. * 取系统日期、时间的函数。返回long型的系统日期时间
  624. *
  625. * @return long型的系统时间
  626. * @see System.currentTimeMillis()
  627. */
  628. public static long getSysDateLong() {
  629. return System.currentTimeMillis();
  630. }
  631. /**
  632. * 返回java.sql.Timestamp型的SYSDATE
  633. *
  634. * @return java.sql.Timestamp型的SYSDATE
  635. */
  636. public static java.sql.Timestamp getSysDateTimestamp() {
  637. return new java.sql.Timestamp(System.currentTimeMillis());
  638. }
  639. /**
  640. * 以配置的Date Only的格式返回系统日期
  641. *
  642. * @return 系统日期
  643. */
  644. public static String getSysDateString() {
  645. return toString(new java.util.Date(System.currentTimeMillis()), DateTime.sdfDateOnly);
  646. }
  647. /**
  648. * 以配置的Datetime格式返回系统日期时间
  649. *
  650. * @return 系统日期时间
  651. */
  652. public static String getSysDateTimeString() {
  653. return toString(new java.util.Date(System.currentTimeMillis()), DateTime.sdfDateTime);
  654. }
  655. /**
  656. * 以固定的YYYY/MM/DD HH24:MI:SS.sss格式返回系统日期时间
  657. *
  658. * @return 系统日期时间,准确到毫秒, YYYY/MM/DD HH24:MI:SS.sss
  659. * @since 2003/09/04
  660. */
  661. public static String getSysDateFullString() {
  662. return toStringFull(System.currentTimeMillis());
  663. }
  664. /*****************************************************
  665. * java.sql.Date ==> String 的转换函数
  666. *****************************************************/
  667. /**
  668. * 利用指定日期格式把字符串转化到java.sql.Date类型
  669. *
  670. * @param sDate
  671. * 日期字符串
  672. * @return 返回java.sql.Date类型的日期
  673. * @history 2004/04/30 保证日期格式的灵活性
  674. */
  675. public static java.sql.Date toSqlDate(String sDate) {
  676. // return toSqlDate( sDate, this.DATE_FORMAT_DATEONLY );
  677. // java.sql.Date.value() 要求的格式必须为YYYY-MM-DD
  678. return new java.sql.Date(DateTime.toDate(sDate).getTime());
  679. // return java.sql.Date.valueOf(sDate.replace('/', '-'));
  680. }
  681. /*****************************************************
  682. * String ==> java.sql.Date 的转换函数
  683. *****************************************************/
  684. /**
  685. * 将java.sql.Date类型的日期转换到日期字符串(格式为YYYY/MM/DD)
  686. *
  687. * @param dt
  688. * java.sql.Date 日期
  689. * @return 字符串(YYYY/MM/DD)
  690. * @history 2004/04/30 保证日期格式的灵活性
  691. */
  692. public static String toSqlDateString(java.sql.Date dt) {
  693. return DateTime.toString(new Date(dt.getTime()));
  694. // String temp = null;
  695. // temp = dt.toString();
  696. // return temp.replace('-', '/');
  697. }
  698. /*****************************************************
  699. * java.sql.Timestamp ==> String 的转换函数
  700. *****************************************************/
  701. /**
  702. * 转换GregorianCalendar Object到java.sql.Timestamp
  703. *
  704. * @param gcal
  705. * GregorianCalendar Object
  706. * @return java.sql.Timestamp object
  707. * @since 1.0
  708. */
  709. /*
  710. * public static java.sql.Timestamp toSqlTimestamp(GregorianCalendar gcal){
  711. * return new
  712. * java.sql.Timestamp((long)gcal.get(GregorianCalendar.MILLISECOND) ); }
  713. */
  714. /**
  715. * 将符合定义格式的Date only日期字符串转换到java.sql.Timestamp类型的时间戳
  716. *
  717. * @param sDate
  718. * 日期字符串(格式=YYYY/MM/DD)
  719. * @return java.sql.Timestamp类型时间戳
  720. */
  721. public static java.sql.Timestamp toSqlTimestamp(String sDate) {
  722. if (sDate == null)
  723. return null;
  724. // if (sDate.length() != DateTime.DATE_FORMAT_DATEONLY.length())
  725. // return null;
  726. return toSqlTimestamp(sDate, DateTime.DATE_FORMAT_DATEONLY);
  727. }
  728. /**
  729. * 利用指定格式的日期字符串转化到java.sql.Timestamp时间戳
  730. *
  731. * @param sDate
  732. * 日期字符串
  733. * @param sFmt
  734. * 日期格式 DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME
  735. * @return java.sql.Timestamp 时间戳
  736. * @history 2004/04/29 先把String转换为Date,然后利用Date的long 毫秒值来取得Timestamp
  737. * @history 2004/05/12 增加日期转换失败为Null的情况下,应该返回Null的判断
  738. */
  739. public static java.sql.Timestamp toSqlTimestamp(String sDate, String sFmt) {
  740. Date date = DateTime.toDate(sDate, sFmt);
  741. if (date != null) {
  742. return new java.sql.Timestamp(date.getTime());
  743. } else {
  744. return null;
  745. }
  746. /*
  747. * String temp = null; if (sDate == null || sFmt == null) return null;
  748. * if (sDate.length() != sFmt.length()) return null; if
  749. * (sFmt.equals(DateTime.DATE_FORMAT_DATETIME)) { temp =
  750. * sDate.replace('/', '-'); temp = temp + ".000000000"; } else if
  751. * (sFmt.equals(DateTime.DATE_FORMAT_DATEONLY)) { temp =
  752. * sDate.replace('/', '-'); temp = temp + " 00:00:00.000000000";
  753. *
  754. * } else { return null; } //java.sql.Timestamp.value()
  755. * 要求的格式必须为yyyy-mm-dd hh:mm:ss.fffffffff return
  756. * java.sql.Timestamp.valueOf(temp);
  757. */
  758. }
  759. /*****************************************************
  760. * String ==> java.sql.Date 的转换函数
  761. *****************************************************/
  762. /**
  763. * 将java.sql.Timestamp类型的时间戳转换到字符串格式为YYYY/MM/DD
  764. *
  765. * @param dt
  766. * java.sql.Timestamp类型的时间戳
  767. * @return 日期字符串
  768. */
  769. public static String toSqlTimestampString(java.sql.Timestamp dt) {
  770. // if (dt == null)
  771. // return null;
  772. return toSqlTimestampString(dt, DateTime.DATE_FORMAT_DATEONLY);
  773. }
  774. /**
  775. * 将java.sql.Timestamp时间戳转换到日期字符串,格式为YYYY/MM/DD HH24:MI
  776. *
  777. * @param dt
  778. * java.sql.Timestamp日期时间戳
  779. * @return 日期字符串
  780. * @history 2004/04/29 schema-config.xml
  781. * 中定义的Datetime格式可能小于16,所以返回是的substring ,应该先判断长度
  782. */
  783. public static String toSqlTimestampString2(java.sql.Timestamp dt) {
  784. if (dt == null)
  785. return null;
  786. String temp = toSqlTimestampString(dt, DateTime.DATE_FORMAT_DATETIME);
  787. return temp.substring(0, 16 < DateTime.DATE_FORMAT_DATETIME.length() ? 16 : DateTime.DATE_FORMAT_DATETIME.length());
  788. }
  789. /**
  790. * 将java.sql.Timestamp时间戳转换到日期字符串,schema-config.xml中指定的datetime 格式
  791. * NULL的场合,转换为NULL
  792. *
  793. * @param dt
  794. * java.sql.Timestamp 日期时间戳
  795. * @return 日期字符串
  796. */
  797. public static String toString(java.sql.Timestamp dt) {
  798. return dt == null ? "" : toSqlTimestampString2(dt);
  799. }
  800. /**
  801. * 将指定的java.sql.Timestamp类型的日期转变为指定的中文日期格式字符串 YYYY年MM月DD日 HH:MM:SS
  802. *
  803. * @param timestamp
  804. * java.sql.Timestamp类型的日期
  805. * @return 中文日期格式字符串
  806. * @deprecated
  807. */
  808. public static String convertTimestampToChinaCalendar(Timestamp timestamp) {
  809. StringBuffer sb = new StringBuffer();
  810. if (timestamp == null) {
  811. sb.append("&nbsp");
  812. } else {
  813. sb = new StringBuffer();
  814. sb.append(DateTime.getYearOfTimestamp(timestamp));
  815. sb.append("年");
  816. sb.append(DateTime.getMonthOfTimestamp(timestamp));
  817. sb.append("月");
  818. sb.append(DateTime.getDayOfTimestamp(timestamp));
  819. sb.append("日");
  820. sb.append(" ");
  821. sb.append(DateTime.getHourOfTimestamp(timestamp));
  822. sb.append(":");
  823. if (DateTime.getMinuteOfTimestamp(timestamp) < 10) {
  824. sb.append(0);
  825. sb.append(DateTime.getMinuteOfTimestamp(timestamp));
  826. } else {
  827. sb.append(DateTime.getMinuteOfTimestamp(timestamp));
  828. }
  829. sb.append(":");
  830. if (DateTime.getSecondOfTimestamp(timestamp) < 10) {
  831. sb.append(0);
  832. sb.append(DateTime.getSecondOfTimestamp(timestamp));
  833. } else {
  834. sb.append(DateTime.getSecondOfTimestamp(timestamp));
  835. }
  836. }
  837. return sb.toString();
  838. }
  839. /**
  840. * 根据指定的java.sql.Timestamp类型的时间戳转换到指定格式的字符串
  841. *
  842. * @param dt
  843. * java.sql.Timestamp类型的时间戳
  844. * @param sFmt
  845. * 日期格式,DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME
  846. * @return 指定格式的日期字符串
  847. * @history 2004/04/29: 先转换为Date,然后利用Date ==> String
  848. * @see #toString(Date,String)
  849. */
  850. public static String toSqlTimestampString(java.sql.Timestamp dt, String sFmt) {
  851. if (dt == null) {
  852. return null;
  853. }
  854. return DateTime.toString(new Date(dt.getTime()), sFmt);
  855. }
  856. /**
  857. * 将java.sql.Timestamp类型的日期转换到HH24:MI格式的时间字符串
  858. *
  859. * @param dt
  860. * java.sql.Timestamp类型的字符串
  861. * @return HH24:MI格式的时间字符串
  862. */
  863. public static String toHourMinString(java.sql.Timestamp dt) {
  864. String temp = null;
  865. temp = dt.toString();
  866. // int len = 0;
  867. // len = DateTime.DATE_FORMAT_DATETIME.length ();
  868. temp = temp.substring(11, 16);
  869. return temp;
  870. }
  871. /*****************************************************
  872. * java.sql.Timestamp +/- 几天的计算函数
  873. *****************************************************/
  874. /**
  875. * 判断指定的日期是否是一个月的最后一天
  876. *
  877. * @param obj
  878. * GregorianCalendar object
  879. */
  880. private static boolean isLastDayOfMonth(GregorianCalendar obj) {
  881. int year = obj.get(GregorianCalendar.YEAR);
  882. int month = obj.get(GregorianCalendar.MONTH) + 1;
  883. int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
  884. if (obj.isLeapYear(year)) {
  885. // Modified by ChenJP, 2004/03/30, 2月份没有30号
  886. // if (day == DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]){
  887. if (day >= DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]) {
  888. return true;
  889. }
  890. } else {
  891. // Modified by ChenJP, 2004/03/30, 2月份没有30号
  892. // if (day == DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1])
  893. if (day >= DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]) {
  894. return true;
  895. }
  896. }
  897. return false;
  898. }
  899. /**
  900. * 在java.sql.Timestamp类型时间戳上增加/减少几个月得到的新的时间戳
  901. *
  902. * @param timestamp
  903. * java.sql.Timestamp类型的时间戳
  904. * @param mon
  905. * 增加/减少的月数
  906. * @return java.sql.Timestamp类型的时间戳
  907. */
  908. public static java.sql.Timestamp addMonths(java.sql.Timestamp timestamp, int mon) {
  909. java.sql.Timestamp out = null;
  910. GregorianCalendar obj = DateTime.convertToCalendar(timestamp);
  911. // month : 0 -- 11
  912. int year = obj.get(GregorianCalendar.YEAR);
  913. int month = obj.get(GregorianCalendar.MONTH) + 1;
  914. int day = obj.get(GregorianCalendar.DAY_OF_MONTH);
  915. month += mon;
  916. if (month < 1) {
  917. month += 12;
  918. year--;
  919. } else if (month > 12) {
  920. month -= 12;
  921. year++;
  922. }
  923. // 对应通沙2004/03/30出现的Bug
  924. boolean laterThanLastMonth = false;
  925. if (obj.isLeapYear(year)) {
  926. laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1]);
  927. } else {
  928. laterThanLastMonth = (day > DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1]);
  929. }
  930. // 是否是一个月的最后一天
  931. if (isLastDayOfMonth(obj) || laterThanLastMonth) {
  932. if (obj.isLeapYear(year)) {
  933. day = DateTime.DAY_OF_MONTH_LEAP_YEAR[month - 1];
  934. } else {
  935. day = DateTime.DAY_OF_MONTH_NON_LEAP_YEAR[month - 1];
  936. }
  937. }
  938. // Modified by SJNS/zq at 2009.10.29 支持HH:MM:SS
  939. String temp = DateTime.formatYMD(year, month, day);
  940. temp = temp + " " + DateTime.toHourMinString(timestamp) + ":00";
  941. out = DateTime.toSqlTimestamp(temp, DateTime.DATE_FORMAT_DATETIME);
  942. return out;
  943. }
  944. /**
  945. * 在java.sql.Timestamp类型时间戳上增加/减少几天得到的新的时间戳
  946. *
  947. * @param timestamp
  948. * java.sql.Timestamp类型的时间戳
  949. * @param days
  950. * 增加/减少的天数
  951. * @return java.sql.Timestamp类型的时间戳
  952. */
  953. public static java.sql.Timestamp addDays(java.sql.Timestamp timestamp, int days) {
  954. java.util.Date date = DateTime.convertTimestampToDate(timestamp);
  955. long temp = date.getTime();
  956. return new java.sql.Timestamp(temp + DateTime.DAY_MILLI * days);
  957. }
  958. // ***********************************************************************
  959. // Framework DateTime.java 原有的method
  960. // ********************************************************************
  961. /**
  962. * Return the current date as a formatted string
  963. *
  964. * @return String The current date
  965. * @deprecated
  966. */
  967. public synchronized static String getDateString() {
  968. return DateTime.toString(new Date(System.currentTimeMillis()));
  969. } /* getDateString() */
  970. /**
  971. *
  972. *
  973. * @param now
  974. * @return
  975. * @deprecated
  976. */
  977. public synchronized static String getDateString(Calendar now) {
  978. return DateTime.toString(new Date(now.getTimeInMillis()), DateTime.DATE_FORMAT_DATETIME);
  979. } /* getDateString(Calendar) */
  980. /**
  981. * Get a date/time field formatted for insertion into a database Value of
  982. * the date/time is the current date and time
  983. *
  984. * @return String The formatted date time field
  985. * @deprecated
  986. */
  987. public synchronized static String getDateTimeForDB() {
  988. return getDateTimeForDB(new Date());
  989. } /* getDateTimeForDB() */
  990. /**
  991. * Get a date/time field formatted for insertion into a database Value of
  992. * the date/time is the current date and time
  993. *
  994. * @param year
  995. * @param month
  996. * @param day
  997. * @param hour
  998. * @param min
  999. * @param sec
  1000. * @return String The formatted date time field
  1001. * @deprecated
  1002. */
  1003. public synchronized static String getDateTimeForDB(int year, int month, int day, int hour, int min, int sec) {
  1004. Calendar cal = new GregorianCalendar(year, month, day, hour, min, sec);
  1005. return getDateTimeForDB(cal.getTime()).trim();
  1006. } /* getDateTimeForDB(int, int, int, int, int, int) */
  1007. /**
  1008. * Get a date/time field formatted for insertion into a database
  1009. *
  1010. * @param date
  1011. * @return String The formatted date time field
  1012. * @deprecated
  1013. */
  1014. public synchronized static String getDateTimeForDB(Date date) {
  1015. // Format the current time. SimpleDateFormat formatter
  1016. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
  1017. Date currentTime_1 = date;
  1018. String dateString = formatter.format(currentTime_1);
  1019. return dateString.trim();
  1020. } /* getDateTimeForDB(Date) */
  1021. /**
  1022. * Get a date/time string containing the current date and time, formatted
  1023. * for user readability
  1024. *
  1025. * @return String The formatted current date/time
  1026. * @deprecated
  1027. */
  1028. public synchronized static String getDateTimeString() {
  1029. /*
  1030. * Calendar rightNow = Calendar.getInstance(); return new String("" +
  1031. * (rightNow.get(Calendar.MONTH) + 1)+ "/" +
  1032. * rightNow.get(Calendar.DAY_OF_MONTH) + "/" +
  1033. * rightNow.get(Calendar.YEAR) + " " + rightNow.get(Calendar.HOUR) + ":"
  1034. * + rightNow.get(Calendar.MINUTE) + ":" +
  1035. * rightNow.get(Calendar.SECOND)).trim();
  1036. */
  1037. // Format the current time. SimpleDateFormat formatter
  1038. SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
  1039. Date currentTime_1 = new Date();
  1040. String dateString = formatter.format(currentTime_1);
  1041. return dateString.trim();
  1042. } /* getDateTimeString() */
  1043. /**
  1044. * Get a date/time string for a given date and time, formatted for user
  1045. * readability
  1046. *
  1047. * @return String The formatted current date/time
  1048. * @deprecated
  1049. */
  1050. public synchronized static String getDateTimeString(Date date) {
  1051. // Format the current time. SimpleDateFormat formatter
  1052. SimpleDateFormat formatter = new SimpleDateFormat("E',' MMM d yyyy 'at' hh:mm:ss a");
  1053. String dateString = formatter.format(date);
  1054. return dateString.trim();
  1055. } /* getDateTimeString() */
  1056. /**
  1057. * get the <b>min</b> dateTime just as "0001/01/01" or "0001/01/01 00:00:00"
  1058. *
  1059. * @param sFmt
  1060. * date format by user's setting
  1061. * @return String the
  1062. */
  1063. public synchronized static String getMinDateTime(String sFmt) {
  1064. Calendar cal = new GregorianCalendar(1, 0, 1, 0, 0, 0);
  1065. Date dt = cal.getTime();
  1066. return toString(dt, sFmt);
  1067. } /* getMinDateTime(String) */
  1068. /**
  1069. * added by huangzz on 2007/01/05<br>
  1070. * get the <b>max</b> dateTime just as "9999/12/31" or "9999/12/31 23:59:59"
  1071. *
  1072. * @param sFmt
  1073. * date format by user's setting
  1074. * @return String
  1075. */
  1076. public synchronized static String getMaxDateTime(String sFmt) {
  1077. Calendar cal = new GregorianCalendar(9999, 11, 31, 23, 59, 59);
  1078. Date dt = cal.getTime();
  1079. return toString(dt, sFmt);
  1080. } /* getMaxDateTime(String) */
  1081. } /* DateTime */