1

Java Dates としてランダムな日付を生成しようとしていますが、日付は同じままです。日付ではなく、時間をランダムにしたい。

例: テスト ケース 1: 1/1/2013 9:08:52

テスト ケース 2: 2013 年 1 月 1 日 15:01: 42 など。

何か案は?

やあみんな私はうまくいくものを見つけました。助けてくれてありがとう!

import java.util.Random;
import java.sql.Time;

public class Clock2 {
public static void main(String[] args) {

    for (int i=0; i< 100; i++){ 
        final Random random = new Random();
        final int millisInDay = 24*60*60*1000;
        Time time = new Time((long)random.nextInt(millisInDay));
        System.out.println(time);
    }

}
4

2 に答える 2

1

これを使用して、希望する日付の開始と終了の Unix タイムスタンプを見つけます: http://www.epochconverter.com/

これら 2 つの値の間で乱数を生成しlongます。最終的にhttp://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmllongを使用してフォーマットしますSimpleDateFormat

于 2013-04-17T03:24:46.650 に答える
0

試す

    Random r = new Random();
    GregorianCalendar c = new GregorianCalendar(r.nextInt(100) + 2000, r.nextInt(11), r.nextInt(31));
    long t0 = c.getTimeInMillis();
    for(int i = 0; i < 10; i++) {
        Date d = new Date(t0 + r.nextInt(24 * 3600 * 1000));
        System.out.println(d);
    }
于 2013-04-17T04:32:38.030 に答える