根据ip和时间戳生成唯一字符串
国庆快乐
要上班了
不快乐了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package tool; import java.text.SimpleDateFormat; import java.util.Date ; import java.util.Random ;
public class IPTimeStamp {
private static SimpleDateFormat sdf = null ; public IPTimeStamp(){}
public static String getIPTimeRand(String ip){ StringBuffer buf = new StringBuffer() ; if(ip != null){ String s[] = ip.split("\\.") ; for(int i=0;i<s.length;i++){ buf.append(addZero(s[i],3)) ; } } buf.append(getTimeStamp()) ; Random r = new Random() ; for(int i=0;i<3;i++){ buf.append(r.nextInt(10)) ; } return buf.toString() ; }
public String getDate(){ this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ; return this.sdf.format(new Date()) ; }
public static String getTimeStamp(){ sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ; return sdf.format(new Date()) ; }
private static String addZero(String str,int len){ StringBuffer s = new StringBuffer() ; s.append(str) ; while(s.length() < len){ s.insert(0,"0") ; } return s.toString() ; }
public static void main(String args[]){ System.out.println(IPTimeStamp.getIPTimeRand("192.168.1.1")) ; } }
|