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 }