Java时间日期格式转换Date转String和String转Date

红太狼 2021-03-26 13:29 781阅读 0赞

Java时间格式转换大全

  1. import java.text.*;
  2. import java.util.Calendar;
  3. public class VeDate {
  4. /**
  5. * 获取现在时间
  6. *
  7. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  8. */
  9. public static Date getNowDate() {
  10. Date currentTime = new Date();
  11. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  12. String dateString = formatter.format(currentTime);
  13. ParsePosition pos = new ParsePosition(8);
  14. Date currentTime_2 = formatter.parse(dateString, pos);
  15. return currentTime_2;
  16. }
  17. /**
  18. * 获取现在时间
  19. *
  20. * @return返回短时间格式 yyyy-MM-dd
  21. */
  22. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
  23. DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
  24. Date date = null;
  25. String str = null;
  26. // String转Date
  27. str = "2007-1-18";
  28. try {
  29. date = format1.parse(str);
  30. data = format2.parse(str);
  31. } catch (ParseException e) {
  32. e.printStackTrace();
  33. }
  34. /**
  35. * 获取现在时间
  36. *
  37. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  38. */
  39. public static String getStringDate() {
  40. Date currentTime = new Date();
  41. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  42. String dateString = formatter.format(currentTime);
  43. return dateString;
  44. }
  45. /**
  46. * 获取现在时间
  47. *
  48. * @return 返回短时间字符串格式yyyy-MM-dd
  49. */
  50. public static String getStringDateShort() {
  51. Date currentTime = new Date();
  52. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  53. String dateString = formatter.format(currentTime);
  54. return dateString;
  55. }
  56. /**
  57. * 获取时间 小时:分;秒 HH:mm:ss
  58. *
  59. * @return
  60. */
  61. public static String getTimeShort() {
  62. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  63. Date currentTime = new Date();
  64. String dateString = formatter.format(currentTime);
  65. return dateString;
  66. }
  67. /**
  68. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  69. *
  70. * @param strDate
  71. * @return
  72. */
  73. public static Date strToDateLong(String strDate) {
  74. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  75. ParsePosition pos = new ParsePosition(0);
  76. Date strtodate = formatter.parse(strDate, pos);
  77. return strtodate;
  78. }
  79. /**
  80. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  81. *
  82. * @param dateDate
  83. * @return
  84. */
  85. public static String dateToStrLong(java.util.Date dateDate) {
  86. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  87. String dateString = formatter.format(dateDate);
  88. return dateString;
  89. }
  90. /**
  91. * 将短时间格式时间转换为字符串 yyyy-MM-dd
  92. *
  93. * @param dateDate
  94. * @param k
  95. * @return
  96. */
  97. public static String dateToStr(java.util.Date dateDate) {
  98. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  99. String dateString = formatter.format(dateDate);
  100. return dateString;
  101. }
  102. /**
  103. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  104. *
  105. * @param strDate
  106. * @return
  107. */
  108. public static Date strToDate(String strDate) {
  109. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  110. ParsePosition pos = new ParsePosition(0);
  111. Date strtodate = formatter.parse(strDate, pos);
  112. return strtodate;
  113. }
  114. /**
  115. * 得到现在时间
  116. *
  117. * @return
  118. */
  119. public static Date getNow() {
  120. Date currentTime = new Date();
  121. return currentTime;
  122. }
  123. /**
  124. * 提取一个月中的最后一天
  125. *
  126. * @param day
  127. * @return
  128. */
  129. public static Date getLastDate(long day) {
  130. Date date = new Date();
  131. long date_3_hm = date.getTime() - 3600000 * 34 * day;
  132. Date date_3_hm_date = new Date(date_3_hm);
  133. return date_3_hm_date;
  134. }
  135. /**
  136. * 得到现在时间
  137. *
  138. * @return 字符串 yyyyMMdd HHmmss
  139. */
  140. public static String getStringToday() {
  141. Date currentTime = new Date();
  142. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  143. String dateString = formatter.format(currentTime);
  144. return dateString;
  145. }
  146. /**
  147. * 得到现在小时
  148. */
  149. public static String getHour() {
  150. Date currentTime = new Date();
  151. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  152. String dateString = formatter.format(currentTime);
  153. String hour;
  154. hour = dateString.substring(11, 13);
  155. return hour;
  156. }
  157. /**
  158. * 得到现在分钟
  159. *
  160. * @return
  161. */
  162. public static String getTime() {
  163. Date currentTime = new Date();
  164. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  165. String dateString = formatter.format(currentTime);
  166. String min;
  167. min = dateString.substring(14, 16);
  168. return min;
  169. }
  170. /**
  171. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  172. *
  173. * @param sformat
  174. * yyyyMMddhhmmss
  175. * @return
  176. */
  177. public static String getUserDate(String sformat) {
  178. Date currentTime = new Date();
  179. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  180. String dateString = formatter.format(currentTime);
  181. return dateString;
  182. }

做成方法

  1. import java.util.*;
  2. import java.text.*;
  3. import java.util.Calendar;
  4. public class VeDate {
  5. /**
  6. * 获取现在时间
  7. *
  8. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  9. */
  10. public static Date getNowDate() {
  11. Date currentTime = new Date();
  12. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. String dateString = formatter.format(currentTime);
  14. ParsePosition pos = new ParsePosition(8);
  15. Date currentTime_2 = formatter.parse(dateString, pos);
  16. return currentTime_2;
  17. }
  18. /**
  19. * 获取现在时间
  20. *
  21. * @return返回短时间格式 yyyy-MM-dd
  22. */
  23. public static Date getNowDateShort() {
  24. Date currentTime = new Date();
  25. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  26. String dateString = formatter.format(currentTime);
  27. ParsePosition pos = new ParsePosition(8);
  28. Date currentTime_2 = formatter.parse(dateString, pos);
  29. return currentTime_2;
  30. }
  31. /**
  32. * 获取现在时间
  33. *
  34. * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  35. */
  36. public static String getStringDate() {
  37. Date currentTime = new Date();
  38. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  39. String dateString = formatter.format(currentTime);
  40. return dateString;
  41. }
  42. /**
  43. * 获取现在时间
  44. *
  45. * @return 返回短时间字符串格式yyyy-MM-dd
  46. */
  47. public static String getStringDateShort() {
  48. Date currentTime = new Date();
  49. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  50. String dateString = formatter.format(currentTime);
  51. return dateString;
  52. }
  53. /**
  54. * 获取时间 小时:分;秒 HH:mm:ss
  55. *
  56. * @return
  57. */
  58. public static String getTimeShort() {
  59. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  60. Date currentTime = new Date();
  61. String dateString = formatter.format(currentTime);
  62. return dateString;
  63. }
  64. /**
  65. * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  66. *
  67. * @param strDate
  68. * @return
  69. */
  70. public static Date strToDateLong(String strDate) {
  71. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  72. ParsePosition pos = new ParsePosition(0);
  73. Date strtodate = formatter.parse(strDate, pos);
  74. return strtodate;
  75. }
  76. /**
  77. * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  78. *
  79. * @param dateDate
  80. * @return
  81. */
  82. public static String dateToStrLong(java.util.Date dateDate) {
  83. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  84. String dateString = formatter.format(dateDate);
  85. return dateString;
  86. }
  87. /**
  88. * 将短时间格式时间转换为字符串 yyyy-MM-dd
  89. *
  90. * @param dateDate
  91. * @param k
  92. * @return
  93. */
  94. public static String dateToStr(java.util.Date dateDate) {
  95. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  96. String dateString = formatter.format(dateDate);
  97. return dateString;
  98. }
  99. /**
  100. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  101. *
  102. * @param strDate
  103. * @return
  104. */
  105. public static Date strToDate(String strDate) {
  106. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  107. ParsePosition pos = new ParsePosition(0);
  108. Date strtodate = formatter.parse(strDate, pos);
  109. return strtodate;
  110. }
  111. /**
  112. * 得到现在时间
  113. *
  114. * @return
  115. */
  116. public static Date getNow() {
  117. Date currentTime = new Date();
  118. return currentTime;
  119. }
  120. /**
  121. * 提取一个月中的最后一天
  122. *
  123. * @param day
  124. * @return
  125. */
  126. public static Date getLastDate(long day) {
  127. Date date = new Date();
  128. long date_3_hm = date.getTime() - 3600000 * 34 * day;
  129. Date date_3_hm_date = new Date(date_3_hm);
  130. return date_3_hm_date;
  131. }
  132. /**
  133. * 得到现在时间
  134. *
  135. * @return 字符串 yyyyMMdd HHmmss
  136. */
  137. public static String getStringToday() {
  138. Date currentTime = new Date();
  139. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  140. String dateString = formatter.format(currentTime);
  141. return dateString;
  142. }
  143. /**
  144. * 得到现在小时
  145. */
  146. public static String getHour() {
  147. Date currentTime = new Date();
  148. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  149. String dateString = formatter.format(currentTime);
  150. String hour;
  151. hour = dateString.substring(11, 13);
  152. return hour;
  153. }
  154. /**
  155. * 得到现在分钟
  156. *
  157. * @return
  158. */
  159. public static String getTime() {
  160. Date currentTime = new Date();
  161. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  162. String dateString = formatter.format(currentTime);
  163. String min;
  164. min = dateString.substring(14, 16);
  165. return min;
  166. }
  167. /**
  168. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  169. *
  170. * @param sformat
  171. * yyyyMMddhhmmss
  172. * @return
  173. */
  174. public static String getUserDate(String sformat) {
  175. Date currentTime = new Date();
  176. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  177. String dateString = formatter.format(currentTime);
  178. return dateString;
  179. }
  180. /**
  181. * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  182. */
  183. public static String getTwoHour(String st1, String st2) {
  184. String[] kk = null;
  185. String[] jj = null;
  186. kk = st1.split(":");
  187. jj = st2.split(":");
  188. if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  189. return "0";
  190. else {
  191. double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  192. double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  193. if ((y - u) > 0)
  194. return y - u + "";
  195. else
  196. return "0";
  197. }
  198. }
  199. /**
  200. * 得到二个日期间的间隔天数
  201. */
  202. public static String getTwoDay(String sj1, String sj2) {
  203. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  204. long day = 0;
  205. try {
  206. java.util.Date date = myFormatter.parse(sj1);
  207. java.util.Date mydate = myFormatter.parse(sj2);
  208. day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  209. } catch (Exception e) {
  210. return "";
  211. }
  212. return day + "";
  213. }
  214. /**
  215. * 时间前推或后推分钟,其中JJ表示分钟.
  216. */
  217. public static String getPreTime(String sj1, String jj) {
  218. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  219. String mydate1 = "";
  220. try {
  221. Date date1 = format.parse(sj1);
  222. long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  223. date1.setTime(Time * 1000);
  224. mydate1 = format.format(date1);
  225. } catch (Exception e) {
  226. }
  227. return mydate1;
  228. }
  229. /**
  230. * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  231. */
  232. public static String getNextDay(String nowdate, String delay) {
  233. try{
  234. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  235. String mdate = "";
  236. Date d = strToDate(nowdate);
  237. long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  238. d.setTime(myTime * 1000);
  239. mdate = format.format(d);
  240. return mdate;
  241. }catch(Exception e){
  242. return "";
  243. }
  244. }
  245. /**
  246. * 判断是否润年
  247. *
  248. * @param ddate
  249. * @return
  250. */
  251. public static boolean isLeapYear(String ddate) {
  252. /**
  253. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  254. * 3.能被4整除同时能被100整除则不是闰年
  255. */
  256. Date d = strToDate(ddate);
  257. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  258. gc.setTime(d);
  259. int year = gc.get(Calendar.YEAR);
  260. if ((year % 400) == 0)
  261. return true;
  262. else if ((year % 4) == 0) {
  263. if ((year % 100) == 0)
  264. return false;
  265. else
  266. return true;
  267. } else
  268. return false;
  269. }
  270. /**
  271. * 返回美国时间格式 26 Apr 2006
  272. *
  273. * @param str
  274. * @return
  275. */
  276. public static String getEDate(String str) {
  277. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  278. ParsePosition pos = new ParsePosition(0);
  279. Date strtodate = formatter.parse(str, pos);
  280. String j = strtodate.toString();
  281. String[] k = j.split(" ");
  282. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  283. }
  284. /**
  285. * 获取一个月的最后一天
  286. *
  287. * @param dat
  288. * @return
  289. */
  290. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  291. String str = dat.substring(0, 8);
  292. String month = dat.substring(5, 7);
  293. int mon = Integer.parseInt(month);
  294. if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  295. str += "31";
  296. } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  297. str += "30";
  298. } else {
  299. if (isLeapYear(dat)) {
  300. str += "29";
  301. } else {
  302. str += "28";
  303. }
  304. }
  305. return str;
  306. }
  307. /**
  308. * 判断二个时间是否在同一个周
  309. *
  310. * @param date1
  311. * @param date2
  312. * @return
  313. */
  314. public static boolean isSameWeekDates(Date date1, Date date2) {
  315. Calendar cal1 = Calendar.getInstance();
  316. Calendar cal2 = Calendar.getInstance();
  317. cal1.setTime(date1);
  318. cal2.setTime(date2);
  319. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  320. if (0 == subYear) {
  321. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  322. return true;
  323. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  324. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  325. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  326. return true;
  327. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  328. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  329. return true;
  330. }
  331. return false;
  332. }
  333. /**
  334. * 产生周序列,即得到当前时间所在的年度是第几周
  335. *
  336. * @return
  337. */
  338. public static String getSeqWeek() {
  339. Calendar c = Calendar.getInstance(Locale.CHINA);
  340. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  341. if (week.length() == 1)
  342. week = "0" + week;
  343. String year = Integer.toString(c.get(Calendar.YEAR));
  344. return year + week;
  345. }
  346. /**
  347. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  348. *
  349. * @param sdate
  350. * @param num
  351. * @return
  352. */
  353. public static String getWeek(String sdate, String num) {
  354. // 再转换为时间
  355. Date dd = VeDate.strToDate(sdate);
  356. Calendar c = Calendar.getInstance();
  357. c.setTime(dd);
  358. if (num.equals("1")) // 返回星期一所在的日期
  359. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  360. else if (num.equals("2")) // 返回星期二所在的日期
  361. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  362. else if (num.equals("3")) // 返回星期三所在的日期
  363. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  364. else if (num.equals("4")) // 返回星期四所在的日期
  365. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  366. else if (num.equals("5")) // 返回星期五所在的日期
  367. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  368. else if (num.equals("6")) // 返回星期六所在的日期
  369. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  370. else if (num.equals("0")) // 返回星期日所在的日期
  371. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  372. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  373. }
  374. /**
  375. * 根据一个日期,返回是星期几的字符串
  376. *
  377. * @param sdate
  378. * @return
  379. */
  380. public static String getWeek(String sdate) {
  381. // 再转换为时间
  382. Date date = VeDate.strToDate(sdate);
  383. Calendar c = Calendar.getInstance();
  384. c.setTime(date);
  385. // int hour=c.get(Calendar.DAY_OF_WEEK);
  386. // hour中存的就是星期几了,其范围 1~7
  387. // 1=星期日 7=星期六,其他类推
  388. return new SimpleDateFormat("EEEE").format(c.getTime());
  389. }
  390. public static String getWeekStr(String sdate){
  391. String str = "";
  392. str = VeDate.getWeek(sdate);
  393. if("1".equals(str)){
  394. str = "星期日";
  395. }else if("2".equals(str)){
  396. str = "星期一";
  397. }else if("3".equals(str)){
  398. str = "星期二";
  399. }else if("4".equals(str)){
  400. str = "星期三";
  401. }else if("5".equals(str)){
  402. str = "星期四";
  403. }else if("6".equals(str)){
  404. str = "星期五";
  405. }else if("7".equals(str)){
  406. str = "星期六";
  407. }
  408. return str;
  409. }
  410. /**
  411. * 两个时间之间的天数
  412. *
  413. * @param date1
  414. * @param date2
  415. * @return
  416. */
  417. public static long getDays(String date1, String date2) {
  418. if (date1 == null || date1.equals(""))
  419. return 0;
  420. if (date2 == null || date2.equals(""))
  421. return 0;
  422. // 转换为标准时间
  423. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  424. java.util.Date date = null;
  425. java.util.Date mydate = null;
  426. try {
  427. date = myFormatter.parse(date1);
  428. mydate = myFormatter.parse(date2);
  429. } catch (Exception e) {
  430. }
  431. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  432. return day;
  433. }
  434. /**
  435. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  436. * 此函数返回该日历第一行星期日所在的日期
  437. *
  438. * @param sdate
  439. * @return
  440. */
  441. public static String getNowMonth(String sdate) {
  442. // 取该时间所在月的一号
  443. sdate = sdate.substring(0, 8) + "01";
  444. // 得到这个月的1号是星期几
  445. Date date = VeDate.strToDate(sdate);
  446. Calendar c = Calendar.getInstance();
  447. c.setTime(date);
  448. int u = c.get(Calendar.DAY_OF_WEEK);
  449. String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  450. return newday;
  451. }
  452. /**
  453. * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  454. *
  455. * @param k
  456. * 表示是取几位随机数,可以自己定
  457. */
  458. public static String getNo(int k) {
  459. return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  460. }
  461. /**
  462. * 返回一个随机数
  463. *
  464. * @param i
  465. * @return
  466. */
  467. public static String getRandom(int i) {
  468. Random jjj = new Random();
  469. // int suiJiShu = jjj.nextInt(9);
  470. if (i == 0)
  471. return "";
  472. String jj = "";
  473. for (int k = 0; k < i; k++) {
  474. jj = jj + jjj.nextInt(9);
  475. }
  476. return jj;
  477. }
  478. /**
  479. *
  480. * @param args
  481. */
  482. public static boolean RightDate(String date) {
  483. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  484. ;
  485. if (date == null)
  486. return false;
  487. if (date.length() > 10) {
  488. sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  489. } else {
  490. sdf = new SimpleDateFormat("yyyy-MM-dd");
  491. }
  492. try {
  493. sdf.parse(date);
  494. } catch (ParseException pe) {
  495. return false;
  496. }
  497. return true;
  498. }
  499. /***************************************************************************
  500. * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  501. * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  502. **************************************************************************/
  503. public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  504. Date currentTime = new Date();
  505. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  506. String dateString = formatter.format(currentTime);
  507. String s_nd = dateString.substring(0, 4); // 年份
  508. String s_yf = dateString.substring(5, 7); // 月份
  509. String s_rq = dateString.substring(8, 10); // 日期
  510. String sreturn = "";
  511. roc.util.MyChar mc = new roc.util.MyChar();
  512. if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
  513. if (nd.equals("1")) {
  514. sreturn = s_nd;
  515. // 处理间隔符
  516. if (format.equals("1"))
  517. sreturn = sreturn + "年";
  518. else if (format.equals("2"))
  519. sreturn = sreturn + "-";
  520. else if (format.equals("3"))
  521. sreturn = sreturn + "/";
  522. else if (format.equals("5"))
  523. sreturn = sreturn + ".";
  524. }
  525. // 处理月份
  526. if (yf.equals("1")) {
  527. sreturn = sreturn + s_yf;
  528. if (format.equals("1"))
  529. sreturn = sreturn + "月";
  530. else if (format.equals("2"))
  531. sreturn = sreturn + "-";
  532. else if (format.equals("3"))
  533. sreturn = sreturn + "/";
  534. else if (format.equals("5"))
  535. sreturn = sreturn + ".";
  536. }
  537. // 处理日期
  538. if (rq.equals("1")) {
  539. sreturn = sreturn + s_rq;
  540. if (format.equals("1"))
  541. sreturn = sreturn + "日";
  542. }
  543. } else {
  544. // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
  545. sdate = roc.util.RocDate.getOKDate(sdate);
  546. s_nd = sdate.substring(0, 4); // 年份
  547. s_yf = sdate.substring(5, 7); // 月份
  548. s_rq = sdate.substring(8, 10); // 日期
  549. if (nd.equals("1")) {
  550. sreturn = s_nd;
  551. // 处理间隔符
  552. if (format.equals("1"))
  553. sreturn = sreturn + "年";
  554. else if (format.equals("2"))
  555. sreturn = sreturn + "-";
  556. else if (format.equals("3"))
  557. sreturn = sreturn + "/";
  558. else if (format.equals("5"))
  559. sreturn = sreturn + ".";
  560. }
  561. // 处理月份
  562. if (yf.equals("1")) {
  563. sreturn = sreturn + s_yf;
  564. if (format.equals("1"))
  565. sreturn = sreturn + "月";
  566. else if (format.equals("2"))
  567. sreturn = sreturn + "-";
  568. else if (format.equals("3"))
  569. sreturn = sreturn + "/";
  570. else if (format.equals("5"))
  571. sreturn = sreturn + ".";
  572. }
  573. // 处理日期
  574. if (rq.equals("1")) {
  575. sreturn = sreturn + s_rq;
  576. if (format.equals("1"))
  577. sreturn = sreturn + "日";
  578. }
  579. }
  580. return sreturn;
  581. }
  582. public static String getNextMonthDay(String sdate, int m) {
  583. sdate = getOKDate(sdate);
  584. int year = Integer.parseInt(sdate.substring(0, 4));
  585. int month = Integer.parseInt(sdate.substring(5, 7));
  586. month = month + m;
  587. if (month < 0) {
  588. month = month + 12;
  589. year = year - 1;
  590. } else if (month > 12) {
  591. month = month - 12;
  592. year = year + 1;
  593. }
  594. String smonth = "";
  595. if (month < 10)
  596. smonth = "0" + month;
  597. else
  598. smonth = "" + month;
  599. return year + "-" + smonth + "-10";
  600. }
  601. public static String getOKDate(String sdate) {
  602. if (sdate == null || sdate.equals(""))
  603. return getStringDateShort();
  604. if (!VeStr.Isdate(sdate)) {
  605. sdate = getStringDateShort();
  606. }
  607. // 将“/”转换为“-”
  608. sdate = VeStr.Replace(sdate, "/", "-");
  609. // 如果只有8位长度,则要进行转换
  610. if (sdate.length() == 8)
  611. sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  612. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  613. ParsePosition pos = new ParsePosition(0);
  614. Date strtodate = formatter.parse(sdate, pos);
  615. String dateString = formatter.format(strtodate);
  616. return dateString;
  617. }
  618. public static void main(String[] args) throws Exception {
  619. try {
  620. //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  621. } catch (Exception e) {
  622. throw new Exception();
  623. }
  624. //System.out.println("sss");
  625. }

发表评论

表情:
评论列表 (有 0 条评论,781人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java StringDate

    接口之间数据的传输都是字符串,现在需要把数据存储在数据库中,刚好我们使用了JPA,定义对象的时候将日期定义为了Date,所以不得不把String转为Date对象。具体如下: