• 将指定的时间字符串转换成TimeStamp类型


    第一种方式:

    看TimeStamp类的源码:

    1 public Timestamp(long time) {
    2         super((time/1000)*1000);
    3         nanos = (int)((time%1000) * 1000000);
    4         if (nanos < 0) {
    5             nanos = 1000000000 + nanos;
    6             super.setTime(((time/1000)-1)*1000);
    7         }
    8     }

    可以看出要传入一个时间戳,就可以构造。

    写了一个静态的方法:

     1 public static Timestamp getAppointTimestamp1(String time){
     2         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     3         Timestamp timestamp = null;
     4         try {
     5             Date date = simpleDateFormat.parse(time);
     6             timestamp = new Timestamp(date.getTime());  //date.getTime() 就是时间戳
     7         } catch (ParseException e) {
     8             e.printStackTrace();
     9         }
    10         return timestamp;
    11     }

    第二种方式:

    TimeStamp类的源码:

     1 public static Timestamp valueOf(String s) {
     2         final int YEAR_LENGTH = 4;
     3         final int MONTH_LENGTH = 2;
     4         final int DAY_LENGTH = 2;
     5         final int MAX_MONTH = 12;
     6         final int MAX_DAY = 31;
     7         String date_s;
     8         String time_s;
     9         String nanos_s;
    10         int year = 0;
    11         int month = 0;
    12         int day = 0;
    13         int hour;
    14         int minute;
    15         int second;
    16         int a_nanos = 0;
    17         int firstDash;
    18         int secondDash;
    19         int dividingSpace;
    20         int firstColon = 0;
    21         int secondColon = 0;
    22         int period = 0;
    23         String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]";
    24         String zeros = "000000000";
    25         String delimiterDate = "-";
    26         String delimiterTime = ":";
    27 
    28         if (s == null) throw new java.lang.IllegalArgumentException("null string");
    29 
    30         // Split the string into date and time components
    31         s = s.trim();
    32         dividingSpace = s.indexOf(' ');
    33         if (dividingSpace > 0) {
    34             date_s = s.substring(0,dividingSpace);
    35             time_s = s.substring(dividingSpace+1);
    36         } else {
    37             throw new java.lang.IllegalArgumentException(formatError);
    38         }
    39 
    40         // Parse the date
    41         firstDash = date_s.indexOf('-');
    42         secondDash = date_s.indexOf('-', firstDash+1);
    43 
    44         // Parse the time
    45         if (time_s == null)
    46             throw new java.lang.IllegalArgumentException(formatError);
    47         firstColon = time_s.indexOf(':');
    48         secondColon = time_s.indexOf(':', firstColon+1);
    49         period = time_s.indexOf('.', secondColon+1);
    50 
    51         // Convert the date
    52         boolean parsedDate = false;
    53         if ((firstDash > 0) && (secondDash > 0) && (secondDash < date_s.length() - 1)) {
    54             String yyyy = date_s.substring(0, firstDash);
    55             String mm = date_s.substring(firstDash + 1, secondDash);
    56             String dd = date_s.substring(secondDash + 1);
    57             if (yyyy.length() == YEAR_LENGTH &&
    58                     (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&
    59                     (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {
    60                  year = Integer.parseInt(yyyy);
    61                  month = Integer.parseInt(mm);
    62                  day = Integer.parseInt(dd);
    63 
    64                 if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
    65                     parsedDate = true;
    66                 }
    67             }
    68         }
    69         if (! parsedDate) {
    70             throw new java.lang.IllegalArgumentException(formatError);
    71         }
    72 
    73         // Convert the time; default missing nanos
    74         if ((firstColon > 0) & (secondColon > 0) &
    75             (secondColon < time_s.length()-1)) {
    76             hour = Integer.parseInt(time_s.substring(0, firstColon));
    77             minute =
    78                 Integer.parseInt(time_s.substring(firstColon+1, secondColon));
    79             if ((period > 0) & (period < time_s.length()-1)) {
    80                 second =
    81                     Integer.parseInt(time_s.substring(secondColon+1, period));
    82                 nanos_s = time_s.substring(period+1);
    83                 if (nanos_s.length() > 9)
    84                     throw new java.lang.IllegalArgumentException(formatError);
    85                 if (!Character.isDigit(nanos_s.charAt(0)))
    86                     throw new java.lang.IllegalArgumentException(formatError);
    87                 nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
    88                 a_nanos = Integer.parseInt(nanos_s);
    89             } else if (period > 0) {
    90                 throw new java.lang.IllegalArgumentException(formatError);
    91             } else {
    92                 second = Integer.parseInt(time_s.substring(secondColon+1));
    93             }
    94         } else {
    95             throw new java.lang.IllegalArgumentException(formatError);
    96         }
    97 
    98         return new Timestamp(year - 1900, month - 1, day, hour, minute, second, a_nanos);
    99     }

    通过TimeStamp类的valueof(String time)静态方法直接传入字符串参数就可以了。

    写了一个静态的方法:

    1 public static Timestamp getAppointTimestamp2(String time){
    2         Timestamp timestamp = Timestamp.valueOf(time);
    3         return timestamp;
    4     }
  • 相关阅读:
    Unity RigidBodyFPSController 鼠标不显示
    egret 列表滑动时 中间的item 放大效果实现
    egret 相关面试题
    egret 点击屏幕外时,缓动动画会停止
    游戏中 商城 每隔一段时间刷新次数增加一次
    egret 游戏优化文档
    typescript 中 let和var的区别
    egret微端, 非原生打包相关
    代码中根据不同的类型获取多个标签的中文翻译
    总结一些 egret项目接小程序时 遇到的问题及解决方法
  • 原文地址:https://www.cnblogs.com/yclss123/p/14177430.html
Copyright © 2020-2023  润新知