String2LongUtil

时间:2019-09-18
本文章向大家介绍String2LongUtil,主要包括String2LongUtil使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 public class String2LongUtil {
 2     /**
 3      * String类型转换成date类型
 4      * strTime: 要转换的string类型的时间,
 5      * formatType: 要转换的格式yyyy-MM-dd HH:mm:ss
 6      * //yyyy年MM月dd日 HH时mm分ss秒,
 7      * strTime的时间格式必须要与formatType的时间格式相同
 8      */
 9     public static Date stringToDate(String strTime, String formatType) {
10         KLog.d("进入stringToDate");
11         try {
12             SimpleDateFormat formatter = new SimpleDateFormat(formatType);
13             Date date;
14             date = formatter.parse(strTime);
15             return date;
16         } catch (Exception e) {
17             return null;
18         }
19     }
20 
21     /**
22      * String类型转换为long类型
23      * .............................
24      * strTime为要转换的String类型时间
25      * formatType时间格式
26      * formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
27      * strTime的时间格式和formatType的时间格式必须相同
28      */
29     public static long stringToLong(String strTime, String formatType) {
30         KLog.d("进入stringToLong");
31         try {
32             //String类型转换为date类型
33             Date date = stringToDate(strTime, formatType);
34             KLog.d("调用stringToDate()获得的date:" + date);
35             if (date == null) {
36                 return 0;
37             } else {
38                 //date类型转成long类型
39                 long Hour = date.getHours();
40                 long Min = date.getMinutes();
41                 long TimeLong = Hour * 60 * 60 * 1000 + Min * 60 * 1000;
42                 KLog.d( "stringToLong()获得的Hour:" + Hour + " h");
43                 KLog.d( "stringToLong()获得的Min:" + Min + " min");
44                 KLog.d( "通过stringToLong()转换获得的long类型的时长 TimeLong:" + TimeLong + " ms");
45                 return TimeLong;
46             }
47         } catch (Exception e) {
48             return 0;
49         }
50     }
51 }

原文地址:https://www.cnblogs.com/ken9527just/p/11540069.html