View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-6-20 下午08:06:35
4    */
5   package gboat2.web.util;
6   
7   import java.text.SimpleDateFormat;
8   import java.util.Date;
9   import java.util.Random;
10  
11  /**
12   * @author wangsr
13   * @since jdk1.6
14   * @date 2012-6-20
15   */
16  public class IPTimeStamp {
17  
18  	private SimpleDateFormat sdf = null;
19  
20  	private String ip;
21  
22  	public IPTimeStamp() {
23  	}
24  
25  	public IPTimeStamp(String ip) {
26  		this.ip = ip;
27  	}
28  
29  	public String getTimeStamp() {
30  		String str = null;
31  		this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
32  		str = this.sdf.format(new Date());
33  		Random rand = new Random();
34  		for (int x = 0; x < 3; x++) {
35  			str += rand.nextInt(10);
36  		}
37  		return str;
38  	}
39  
40  	public String getDateTime() {
41  		String str = null;
42  		this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
43  		str = this.sdf.format(new Date());
44  		return str;
45  	}
46  
47  	public String getIPTimeStampRand() {
48  		StringBuffer buf = new StringBuffer();
49  		if (this.ip != null) {
50  			String []s = this.ip.split("\\.");
51  			for (int x = 0; x < s.length; x++) {
52  				buf.append(this.addZero(s[x], 3));
53  			}
54  		}
55  		buf.append(this.getTimeStamp());
56  		Random rand = new Random();
57  		for (int x = 0; x < 3; x++) {
58  			buf.append(rand.nextInt(10));
59  		}
60  		return buf.toString();
61  	}
62  
63  	private String addZero(String str, int len) {
64  		StringBuffer buf = new StringBuffer();
65  		buf.append(str);
66  		while (buf.length() < len) {
67  			buf.insert(0, 0);
68  		}
69  		return buf.toString();
70  	}
71  }