首页 > 代码库 > 各种String类型的时间转long型,long转String

各种String类型的时间转long型,long转String

唉!  有用的拿去吧。  直接可以用。     

  1 package com.tai.use;
  2 
  3 import java.text.SimpleDateFormat;
  4 
  5 public class DataUtil
  6 {
  7     /**
  8      * 时间格式转换,将字符串型时间转换为毫秒数
  9      * "yyyy-MM-dd HH:mm:ss" => "12345"    19位
 10      * "yyyyMMddHHmmss" => "12345"        14位
 11      * "yyyy-MM-dd" => "12345"        10位
 12      * 返回 0 是格式不对
 13      * @param dateStr
 14      * @return
 15      */
 16     public static long parseStringToLong(String dateStr) {
 17         dateStr = dateStr.trim();
 18         if (dateStr.length() == 19) {
 19             try {
 20                 java.util.Calendar cal = java.util.Calendar.getInstance();
 21                 cal.set(Integer.parseInt(dateStr.substring(0, 4)),
 22                         Integer.parseInt(dateStr.substring(5, 7)) - 1,
 23                         Integer.parseInt(dateStr.substring(8, 10)),
 24                         Integer.parseInt(dateStr.substring(11, 13)),
 25                         Integer.parseInt(dateStr.substring(14, 16)),
 26                         Integer.parseInt(dateStr.substring(17, 19)));
 27                 cal.set(java.util.Calendar.MILLISECOND, 0);
 28                 return (cal.getTime().getTime());
 29             } catch (Exception e) {
 30                 return 0;
 31             }
 32 
 33         } else if (dateStr.length() == 16) {
 34             try {
 35                 java.util.Calendar cal = java.util.Calendar.getInstance();
 36                 cal.set(Integer.parseInt(dateStr.substring(0, 4)),
 37                         Integer.parseInt(dateStr.substring(5, 7)) - 1,
 38                         Integer.parseInt(dateStr.substring(8, 10)),
 39                         Integer.parseInt(dateStr.substring(11, 13)),
 40                         Integer.parseInt(dateStr.substring(14, 16)));
 41                 cal.set(java.util.Calendar.MILLISECOND, 0);
 42                 return (cal.getTime().getTime());
 43             } catch (Exception e) {
 44                 return 0;
 45             }
 46         } else if (dateStr.length() == 14) {
 47             try {
 48                 java.util.Calendar cal = java.util.Calendar.getInstance();
 49                 cal.set(Integer.parseInt(dateStr.substring(0, 4)),
 50                         Integer.parseInt(dateStr.substring(4, 6)) - 1,
 51                         Integer.parseInt(dateStr.substring(6, 8)),
 52                         Integer.parseInt(dateStr.substring(8, 10)),
 53                         Integer.parseInt(dateStr.substring(10, 12)),
 54                         Integer.parseInt(dateStr.substring(12, 14)));
 55                 cal.set(java.util.Calendar.MILLISECOND, 0);
 56                 return (cal.getTime().getTime());
 57             } catch (Exception e) {
 58                 return 0;
 59             }
 60         } else if (dateStr.length() == 10) {
 61             try {
 62                 java.util.Calendar cal = java.util.Calendar.getInstance();
 63                 cal.set(Integer.parseInt(dateStr.substring(0, 4)),
 64                         Integer.parseInt(dateStr.substring(5, 7)) - 1,
 65                         Integer.parseInt(dateStr.substring(8, 10)), 0, 0, 0);
 66                 cal.set(java.util.Calendar.MILLISECOND, 0);
 67                 return (cal.getTime().getTime());
 68             } catch (Exception e) {
 69                 return 0;
 70             }
 71         } else {
 72             try {
 73                 return Long.parseLong(dateStr);
 74             } catch (Exception e) {
 75                 return 0;
 76             }
 77 
 78         }
 79 
 80     }
 81     public static String parseLongToString(long timestamp) {
 82         SimpleDateFormat bartDateFormat =
 83                 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 84         System.out.println(timestamp);
 85         String returnStr = null;
 86         try {
 87             java.util.Date date = new java.util.Date(timestamp);
 88             returnStr = bartDateFormat.format(date);
 89         } catch (Exception ex) {
 90             System.out.println(ex.getMessage());
 91         }
 92         return returnStr;
 93     }
 94     
 95     public static void main(String[] args)
 96     {
 97         // * "yyyyMMddHHmmss" => "12345"        14位
 98         /**
 99          * 5月24日 12:00
100          * 5月24日 14:30
101          * 5月24日 16:00
102          * 5月24日 17:30
103          * 5月25日 12:00
104          * 201405241200
105          */
106         System.out.println("print begin :");
107         System.out.println(parseLongToString(System.currentTimeMillis()));
108         System.out.println("--------------------------------------");
109         String[] strings = {"20140524120000","20140524143000","20140524160000","20140524173000","20140525120000"};
110         for (int i = 0; i < strings.length; i++)
111         {
112             long time = parseStringToLong(strings[i]);
113             System.out.println(parseLongToString(time));
114             System.out.println("--------------------------------------");
115         }
116     }
117 }