|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The MegaMek Team. All Rights Reserved. |
| 3 | + * |
| 4 | + * This file is part of MegaMek. |
| 5 | + * |
| 6 | + * MegaMek is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License (GPL), |
| 8 | + * version 3 or (at your option) any later version, |
| 9 | + * as published by the Free Software Foundation. |
| 10 | + * |
| 11 | + * MegaMek is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty |
| 13 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU General Public License for more details. |
| 15 | + * |
| 16 | + * A copy of the GPL should have been included with this project; |
| 17 | + * if not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * NOTICE: The MegaMek organization is a non-profit group of volunteers |
| 20 | + * creating free software for the BattleTech community. |
| 21 | + * |
| 22 | + * MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks |
| 23 | + * of The Topps Company, Inc. All Rights Reserved. |
| 24 | + * |
| 25 | + * Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of |
| 26 | + * InMediaRes Productions, LLC. |
| 27 | + * |
| 28 | + * MechWarrior Copyright Microsoft Corporation. MegaMek was created under |
| 29 | + * Microsoft's "Game Content Usage Rules" |
| 30 | + * <https://www.xbox.com/en-US/developers/rules> and it is not endorsed by or |
| 31 | + * affiliated with Microsoft. |
| 32 | + */ |
| 33 | +package megamek.common.util; |
| 34 | + |
| 35 | +import java.time.LocalDate; |
| 36 | +import java.util.concurrent.ThreadLocalRandom; |
| 37 | + |
| 38 | +public class DateUtilities { |
| 39 | + /** |
| 40 | + * Returns a random {@link LocalDate} between the given start (inclusive) and end (inclusive) dates. |
| 41 | + * |
| 42 | + * <p>This method converts both start and end dates to their epoch day representations, selects a random day |
| 43 | + * within that range, and constructs a {@code LocalDate} from the random epoch day.</p> |
| 44 | + * |
| 45 | + * @param start the earliest date to select (inclusive) |
| 46 | + * @param end the latest date to select (inclusive) |
| 47 | + * |
| 48 | + * @return a randomly selected {@link LocalDate} between {@code start} and {@code end}, inclusive |
| 49 | + * |
| 50 | + * @throws IllegalArgumentException if {@code start} is after {@code end} |
| 51 | + */ |
| 52 | + public static LocalDate getRandomDateBetween(LocalDate start, LocalDate end) { |
| 53 | + if (start.isAfter(end)) { |
| 54 | + throw new IllegalArgumentException("Start date must be before end date"); |
| 55 | + } |
| 56 | + |
| 57 | + long startEpochDay = start.toEpochDay(); |
| 58 | + long endEpochDay = end.toEpochDay(); |
| 59 | + long randomDay = ThreadLocalRandom.current().nextLong(startEpochDay, endEpochDay + 1); |
| 60 | + return LocalDate.ofEpochDay(randomDay); |
| 61 | + } |
| 62 | +} |
0 commit comments