[java]代码库
public class IptimeStamp
{
private SimpleDateFormat sdf = null;//定义simpleDateFormat对象
private String ip = null;//接受ip地址
public IptimeStamp(String ip)
{
try
{
InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress();
this.ip = ip;
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
}
public String getIptimeRand()//得到IP地址+时间+三位随机数
{
StringBuffer buf = new StringBuffer();
if(this.ip!=null)
{
String s[] = this.ip.split("\\.");
for(int i=0;i<s.length;i++)
{
buf.append(this.addZero(s[i],3));
}
}
buf.append(this.getTimeStamp());
Random r = new Random();
for(int i=0;i<3;i++)
{
buf.append(r.nextInt(10));
}
return buf.toString();
}
public 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 String getDate()
{
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return this.sdf.format(new Date());
}
public String getTimeStamp()
{
this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return this.sdf.format(new Date());
}
}