hive udf获取当前月最后一天

package com.hive.udf;


import org.apache.hadoop.hive.ql.exec.UDF;

import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*
 * 
 * 
 */
public class LastDay extends UDF {  
	  
	  public LastDay() {  
	  }  
	  
	  public String evaluate(String thisdate) {  
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
			if (thisdate.length()==8)
				format = new SimpleDateFormat("yyyyMMdd");
		      //获取当前月第一天:
	        Calendar c = Calendar.getInstance();    
	        Date   thisb=null;
	  	  try {
	  		    thisb=format.parse(thisdate);
	  		  
	  		  } catch (ParseException e) {
	  		   e.printStackTrace();
	  		  }
	  	  c.setTime(thisb);
	        c.add(Calendar.MONTH, 0);
	 //       c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
	  //      String first = format.format(c.getTime());
	  //      System.out.println("===============first:"+first);
	        
	        //获取当前月最后一天
	     //   Calendar ca = Calendar.getInstance();    
	        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
	        String last = format.format(c.getTime());
	        return last;
	  }  
	 
		}  

你可能感兴趣的:(Hive)