871e29faa3cfbab51a3622f1bfda2cb00d76c783.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.sinosoft.cm.common;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import nl.justobjects.pushlet.util.Sys;
  8. /**
  9. * 时间格式转换类
  10. * @author Administrator
  11. *
  12. */
  13. public class DateUtil {
  14. private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  15. private static SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm");
  16. public DateUtil() {
  17. }
  18. /**
  19. * util格式date想sql格式date转换
  20. * @param date
  21. * @return
  22. */
  23. public static java.sql.Date toSqlDate(java.util.Date date){
  24. return new java.sql.Date(date.getTime());
  25. }
  26. /**
  27. * sql格式date向util格式date转换
  28. * @param date
  29. * @return
  30. */
  31. public static java.util.Date toUtildate(java.sql.Date date){
  32. return new java.util.Date(date.getTime());
  33. }
  34. /**
  35. * sql格式date转换为字符串
  36. * @param date
  37. * @return
  38. */
  39. public static String toString(java.sql.Date date){
  40. return toString(toUtildate(date));
  41. }
  42. /**
  43. * 字符串转换为sql格式date
  44. * @param dateString
  45. * @return
  46. */
  47. public static java.sql.Date toSqlDate(String dateString){
  48. return toSqlDate(toData(dateString));
  49. }
  50. /**
  51. * util格式date转换为字符串
  52. * @param date
  53. * @return
  54. */
  55. public static String toString(java.util.Date date){
  56. return formatter.format(date);
  57. }
  58. public static String toTimeString(java.util.Date date){
  59. return formatterTime.format(date);
  60. }
  61. /**
  62. * 字符串转换为util格式date
  63. * @param dateString
  64. * @return
  65. */
  66. public static java.util.Date toData(String dateString){
  67. try {
  68. return formatter.parse(dateString);
  69. } catch (ParseException e) {
  70. e.printStackTrace();
  71. }
  72. return null;
  73. }
  74. /**
  75. * 获取间隔时间集合
  76. * @param dateTime
  77. * @param n
  78. * @param timeInterval
  79. * @return
  80. */
  81. public static List<String> getUitlDates(String dateTime,int n,int timeInterval){
  82. List<String> dateString=new ArrayList<String>();
  83. for(int index=0;index<n;index++){
  84. Date temDate=DateUtil.toData(dateTime);
  85. temDate.setSeconds(DateUtil.toData(dateTime).getSeconds()-(n-index-1)*timeInterval);
  86. dateString.add(formatter.format(temDate));
  87. }
  88. System.out.println(dateString);
  89. return dateString;
  90. }
  91. public static List<String> dateStringToDateString(List<String> list){
  92. List<String> dateList=new ArrayList<String>();
  93. for(String item : list){
  94. Date temDate=DateUtil.toData(item);
  95. dateList.add(formatterTime.format(temDate));
  96. }
  97. return dateList;
  98. }
  99. /* public static void main(String[] args) {
  100. System.out.println(
  101. DateUtil.dateStringToDateString(DateUtil.getUitlDates("2012-12-12 13:12:12", 10, 3)));
  102. }*/
  103. }