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

旧城等待, 2022-05-26 02:24 408阅读 0赞

转自:https://www.cnblogs.com/sharpest/p/7879377.html

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

发表评论

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

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

相关阅读

    相关 Java StringDate

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