zoukankan      html  css  js  c++  java
  • 058 时间的转换

    1.格式

      * 输入:31/Aug/2015:00:04:37 +0800
      * 输出:2015-08-31 00:04:37

    2.程序

      

    3.源代码

     1 package org.apache.hadoop.hive_udf;
     2 
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 import java.util.Locale;
     6 
     7 import org.apache.commons.lang.StringUtils;
     8 import org.apache.hadoop.hive.ql.exec.UDF;
     9 import org.apache.hadoop.io.Text;
    10 
    11 
    12 
    13 /**
    14  * 
    15  * 输入:31/Aug/2015:00:04:37 +0800
    16  * 输出:2015-08-31 00:04:37
    17  * 
    18  * 处理步骤:1、读取日期格式
    19  *           2、转换日期格式
    20  *
    21  */
    22 
    23 public class DateFormat extends UDF{
    24     public SimpleDateFormat inputdate = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
    25     public SimpleDateFormat outputdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    26     
    27     public Text evaluate (Text str){
    28         Text out = new Text();
    29         if(str == null){
    30             return null;
    31         }
    32         if (StringUtils.isBlank(str.toString())){
    33             return null;
    34         }
    35         
    36         try {
    37             
    38             Date parasedate = inputdate.parse(str.toString().trim());
    39             String output = outputdate.format(parasedate);
    40             out.set(output);
    41             
    42         } catch (Exception e) {
    43             // TODO: handle exception
    44             e.printStackTrace();
    45         }
    46         return out;
    47         
    48     }
    49     
    50     public static void main(String[] args) {
    51         System.out.println(new DateFormat().evaluate(new Text("31/Aug/2015:00:04:37 +0800")));
    52     }
    53 
    54 }
  • 相关阅读:
    2018 ACM 网络选拔赛 徐州赛区
    2018 ACM 网络选拔赛 焦作赛区
    2018 ACM 网络选拔赛 沈阳赛区
    poj 2289 网络流 and 二分查找
    poj 2446 二分图最大匹配
    poj 1469 二分图最大匹配
    poj 3249 拓扑排序 and 动态规划
    poj 3687 拓扑排序
    poj 2585 拓扑排序
    poj 1094 拓扑排序
  • 原文地址:https://www.cnblogs.com/juncaoit/p/6075137.html
Copyright © 2011-2022 走看看