I have a question about Java date function

Asked 2 years ago, Updated 2 years ago, 127 views

public static void main(String[] args) {

    HashMap<String, String> map = new HashMap<String, String>();

    try {
        File file = new File("C:\\Users\\kimsuhee\\workspace\\Capstone\\src\\capstonedata.txt");
        // Add file to BufferedReader variable
        BufferedReader reader = new BufferedReader(new FileReader(file));

        // Variable line to read files one line at a time
        String line = null;

        String[] splitStr = null; // array splitStr to split one line

        while(((line = reader.readLine())!=null) { // read one line at a time and put it in line
            If (!line.contains("\t") // if there is no tab,
                Continue; // Just let it slide
            splitStr = line.split("\t"); // one line separated by tabs
            map.put(splitStr[0],splitStr[1]); // receive the first item as a key in the array
                                                    // The second item on the 0th is value
                                                    // Accepted and placed at the first of the array

        }
        TreeMap<String, String> tm = new TreeMap<String, String>(map); // HashMap
                                                                        // With TreeMap
                                                                        // change
        Iterator<String> iteratorKey = tm.keySet();iterator(); // ascending key values
                                                                // Sort (default)

        while (iteratorKey.hasNext()) {
            String key = iteratorKey.next();
            String value = tm.get(key);
        }

        Map.Entry<String, String>[]  kv = tm.entrySet().toArray(newMap.Entry[0]); //entrySet() is used when both key and value are required, creating a new Map.Entry object kv as an array

        Random = new Random(); //Create Random Object

        Map.Entry<String, String> keyValue = kv[rand.nextInt(kv.length)]; //Create a new Map.Entry object keyValue is an object that randomly loaded kv

        for(int i = 0; i<5; i++){
            System.out.println(kv[rand.nextInt(kv.length)]);
        }

        reader.close();

You can call up the stored value randomly, but I want to make it change only when the date passes by a day and not on the same date, but I don't know how to use the function. Is there a function that can play this role in the date function?

java random date

2022-09-22 20:31

1 Answers

We plan to implement the GUI. When a program is running, an event occurs whenever a button is pressed, and in a running situation, I want to make the same screen output every time it is executed on the same day

I think you can use the date as a seed value.

You can pass the seed value through the Random(long seed) constructor. Pseudorandom numbers are generated in the same order if the seed values are the same.

Therefore, you can calculate the date as the seed value. There are many ways, but there are simple ways as follows.

DateTime now = DateTime.now();
long seed = now.getYear() * 1000 + now. getMonthOfYear() ;

getYear() returns the year. getMonthOfYear() returns yearly date [1, 365].

getMonthOfYear() is less than 1000, so adding getYear() by 1000 calculates some integer value that does not conflict year and date

This value will be calculated as the same value on the same day, so if you create Random as the seed value as shown below, you will do the desired action.

DateTime now = DateTime.now();
long seed = now.getYear() * 1000 + now. getMonthOfYear() ;
Random = new Random(seed); //Create Random Object

Map.Entry<String, String> keyValue = kv[rand.nextInt(kv.length)]; //Create a new Map.Entry object keyValue is an object that randomly loaded kv

for(int i = 0; i<5; i++){
    System.out.println(kv[rand.nextInt(kv.length)]);
}

reader.close();


2022-09-22 20:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.