09-06
24
java 随机生成时间
作者:Java伴侣 日期:2009-06-24
复制内容到剪贴板 程序代码
private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
getRandColor(200, 250)
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
getRandColor(200, 250)
随机生成时间:
复制内容到剪贴板 程序代码
Random rand = new Random();
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd ");
Calendar cal = Calendar.getInstance();
cal.set(1900, 0, 1);
long start = cal.getTimeInMillis();
cal.set(2008, 0, 1);
long end = cal.getTimeInMillis();
for(int i = 0; i < 10; i++) {
Date d = new Date(start + (long)(rand.nextDouble() * (end - start)));
System.out.println(format.format(d));
}
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd ");
Calendar cal = Calendar.getInstance();
cal.set(1900, 0, 1);
long start = cal.getTimeInMillis();
cal.set(2008, 0, 1);
long end = cal.getTimeInMillis();
for(int i = 0; i < 10; i++) {
Date d = new Date(start + (long)(rand.nextDouble() * (end - start)));
System.out.println(format.format(d));
}
评论: 0 | 引用: 0 | 查看次数: 655
发表评论