机器人 - 随机生成的日期和时间机器人、日期、时间

2023-09-08 00:10:23 作者:幽香清远

要引发一些未来事件我试图创造一个算法,将做到以下几点:

to trigger some future events I'm trying to create an algorithm which would do the following:

生成一定量的随机日期格式 YYYY-MM-DD 的 生成时间格式每个日期 HH:MM:SS 时间应该是(24小时)之间的922小时 添加这些项目到字符串数组中。 1完整的数组项看起来像 2013年2月25日9时45分23秒 generate a certain amount of random dates in format "yyyy-mm-dd" generate time for each date in format "hh:mm:ss" Time should be (24h) between 9 and 22 hours Add those items to a String array. 1 complete array entry looks like "2013-02-25 09:45:23"

我还没有明确的想法如何执行此。有什么建议?

I have no clear ideas how to perform this. Any suggestions?

推荐答案

精确解你所需要的。

public class RandomDateTime {

    public static void main(String[] args) {

        SimpleDateFormat dfDateTime  = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss",Locale.getDefault());
        int year = RandomDateTime.randBetween(1900, 2013);// Here you can set Range of years you need
        int month = RandomDateTime.randBetween(0, 11);
        int hour = RandomDateTime.randBetween(9, 22); //Hours will be displayed in between 9 to 22
        int min = RandomDateTime.randBetween(0, 59);
        int sec = RandomDateTime.randBetween(0, 59);


        GregorianCalendar gc = new GregorianCalendar(year, month, 1);
        int day = RandomDateTime.randBetween(1, gc.getActualMaximum(gc.DAY_OF_MONTH));

        gc.set(year, month, day, hour, min,sec);

        System.out.println(dfDateTime.format(gc.getTime()));

    }


    public static int randBetween(int start, int end) {
        return start + (int)Math.round(Math.random() * (end - start));
    }
}