首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

常用到的util类,记录一上

2012-11-01 
常用到的util类,记录一下package com.atmu.report.utilimport java.io.Fileimport java.io.FileInputStr

常用到的util类,记录一下

package com.atmu.report.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.List;import java.util.Properties;import org.apache.log4j.Logger;public class Util {private static Logger logger=Logger.getLogger(Util.class);/** * 返回子byte数组 */public static byte[] subByte(byte[] b,int begin,int end){byte[] ret=new byte[end-begin];for(int i=0;i<ret.length;i++)ret[i]=b[begin+i];return ret;}/** * 合并两个数组 * @param b1 * @param b2 * @return */public static byte[] mergeByte(byte[] b1,byte[] b2){if (b1==null)return b2;if (b2==null)return b1;byte[] ret=new byte[b1.length+b2.length];System.arraycopy(b1, 0, ret, 0, b1.length);System.arraycopy(b2, 0, ret, b1.length, b2.length);return ret;}/** *  * @param b * @param startpos * @param delimiter * @return 指定分隔符的数组 */public static byte[] subByteDelimiter(byte[] b,int startpos,char delimiter){byte[] ret=new byte[1024];int i=0;for(;i<1024;i++){if(b[startpos+i]==delimiter)break;else{ret[i]=b[startpos+i];}}ret=subByte(ret,0,i);return ret;}/** *  * @param s:#{A41} + #{A42} - #{A43} * @param delimiter1{ * @param delimiter2} * @return */public static String[] DemarkString(String s,char delimiter1,char delimiter2){String[] ret=null;String[] temp=s.split("#");ret=new String[temp.length-1];String item="";for(int i=1;i<=ret.length;i++){item=temp[i];//Log.info(item);ret[i-1]=item.substring(item.indexOf(delimiter1)+1, item.indexOf(delimiter2));}return ret;}/** *  * @param date1 * @param date2 * @return date2-date1 * @throws ParseException  */public static long gettimeDiff(String startDate,String endDate) throws ParseException{SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmss");Date d1=sd.parse(startDate);Date d2=sd.parse(endDate);return d2.getTime()-d1.getTime();}/** *  * @param date * @param format * @return 指定格式的日期字符串形式 * @throws Exception  */public static String format(String date,String format_source,String format_target) throws Exception{SimpleDateFormat sd=new SimpleDateFormat(format_source);Date d1=sd.parse(date);sd=new SimpleDateFormat(format_target);return sd.format(d1);}public static void main(String[] args){/*String s="abcdef|eaac";byte[] b=s.getBytes();String ss=new String(subByteDelimiter(b,1,'|'));String[] ssa=DemarkString("#{A41}",'{','}');for(String sss:ssa)System.out.println(sss);*/System.out.println(getDate("19960229",-1,2));/*String date1="20091207124900";String date2="20091207124901";try {logger.info(gettimeDiff(date1,getCurrentDate()+getCurrentTime()));} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}logger.info("["+FomatNumber(2,2)+"]");*/}/** *  * @return 系统日期 */public static String getCurrentDate(){SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd");return sd.format(new Date());}public static String getYesterday(){Calendar ca=new GregorianCalendar();ca.add(Calendar.DAY_OF_MONTH, -1);return getDateFromCa(ca);/*Date d = ca.getTime();SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd");return sd.format(d);*/}/** *  * @param referDate 20100120 * @param datediff * @return 根据指定日期与差别天数得到的新日期 */public static String getDate(String referDate,int datediff,int type){try{int day=Integer.parseInt(referDate.substring(6));int month=Integer.parseInt(referDate.substring(4,6));int year=Integer.parseInt(referDate.substring(0,4));Calendar ca=new GregorianCalendar(year,month-1,day);switch(type){case 0:ca.add(Calendar.DAY_OF_MONTH, datediff);break;case 1:ca.add(Calendar.MONTH, datediff);break;case 2:ca.add(Calendar.YEAR, datediff);case 3:ca.add(Calendar.WEEK_OF_YEAR, datediff);break;}return getDateFromCa(ca);}catch(Exception e){logger.warn("",e);}return null;}public static String getDateFromCa(Calendar ca){Date d = ca.getTime();SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd");return sd.format(d);}public static String getCurrentTime(){SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmss");return sd.format(new Date()).substring(8);}public static String getFullCurrentTime(){SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmssSSS");return sd.format(new Date());}public static long getlongtime(){return (new Date()).getTime();}/** * 格式化字符串,右补空格 * @param s * @param width * @return */public static String formateString(String s,int width){String format="%1$-"+width+"s";return String.format(format, s);}/** * 格式化字符串,左补空格 * @param s * @param width * @return */public static String RfillBlank(String s,int width){String format="%1$"+width+"s";return String.format(format, s);}public static String getPartitionKey(){SimpleDateFormat sd=new SimpleDateFormat("yyyyMM");return sd.format(new Date());}/** * 测试用 * @param map */public static void writeObject(Object request,String filename) {ObjectOutputStream out;try {out = new ObjectOutputStream(new FileOutputStream("data/"+filename+"-request.ser"));out.writeObject(request);out.close();} catch (Exception e) {e.printStackTrace();}}/** * 测试用 * @param map */public static Object readObject(String filename) {ObjectInputStream in;File f = new File("data/"+filename+"-request.ser");if (!f.exists())return null;try {in = new ObjectInputStream(new FileInputStream("data/"+filename+"-request.ser"));Object ret = in.readObject();in.close();return ret;} catch (Exception e) {e.printStackTrace();}return null;}/** * 从文本生成hashmap对象 * @return */public static  HashMap readFromText(){Properties p=new Properties();HashMap ret=new HashMap();try {p.load(new FileInputStream("test/hashmap.txt"));} catch (Exception e) {logger.warn("",e);}ret.putAll(p);return ret;}/** * /** * 填充数组到指定长度 * @param ret * @param len */public static byte[] fillByteLen(byte[] ret, int len) {byte[] subbyte=null;if(ret!=null&&len<ret.length){logger.warn("param:ret"+new String(ret));logger.warn("param:len"+len);return subByte(ret,0,len);}try{if(ret!=null){subbyte=new byte[len-ret.length];}else{subbyte=new byte[len];}for (int i = 0; i < subbyte.length; i++) {subbyte[i] = 0x20;}ret=mergeByte(ret,subbyte);}catch(Exception e){logger.warn("Error:",e);logger.warn("param:ret"+new String(ret));logger.warn("param:len"+len);}return ret;}//String.format("%03d",cmds[5].getBytes().length)/** * 格式化数字,左补0 */public static String FomatNumber(int width,int data){String format="%0"+width+"d";return String.format(format, data);}/** * 判断list为空 * @param l * @return */public static boolean AssertListEmpty(List l) {if(l==null||l.size()==0){return true;}else{return false;}}}

热点排行