zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Java多线程之监控Java线程池运行状态
    JS自学笔记02
    JS自学笔记01
    JAVA自学笔记09
    前端自学笔记07
    前端自学笔记06
    前端自学笔记05
    前端自学笔记04
    前端自学笔记03
    前端自学笔记02
  • 原文地址:https://www.cnblogs.com/ken9527just/p/11540069.html
Copyright © 2011-2022 走看看