|
| 1 | +/* |
| 2 | + * =========================================================================== |
| 3 | + * (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved |
| 4 | + * =========================================================================== |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * IBM designates this particular file as subject to the "Classpath" exception |
| 10 | + * as provided by IBM in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, see <http://www.gnu.org/licenses/>. |
| 20 | + * =========================================================================== |
| 21 | + */ |
| 22 | +import java.time.Instant; |
| 23 | +import java.time.LocalDate; |
| 24 | +import java.time.LocalDateTime; |
| 25 | +import java.time.ZoneOffset; |
| 26 | +import java.time.format.DateTimeFormatter; |
| 27 | +import java.time.format.DateTimeParseException; |
| 28 | +import java.util.Locale; |
| 29 | + |
| 30 | +public class DateUtil { |
| 31 | + |
| 32 | + public static void main(String... args) { |
| 33 | + String date = ""; |
| 34 | + String format = ""; |
| 35 | + |
| 36 | + for (String arg : args) { |
| 37 | + if (arg.startsWith("--date=")) { |
| 38 | + date = arg.substring(7).trim(); |
| 39 | + } else if (arg.startsWith("--format=")) { |
| 40 | + format = arg.substring(9).trim(); |
| 41 | + } else { |
| 42 | + showUsageAndExit(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + LocalDateTime time = parseTime(date); |
| 47 | + |
| 48 | + if (format.isEmpty()) { |
| 49 | + System.out.println(time.toEpochSecond(ZoneOffset.UTC)); |
| 50 | + } else { |
| 51 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format, Locale.ROOT); |
| 52 | + |
| 53 | + System.out.println(formatter.format(time)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static LocalDateTime parseTime(String text) { |
| 58 | + if (text.isEmpty()) { |
| 59 | + return LocalDateTime.now(ZoneOffset.UTC); |
| 60 | + } |
| 61 | + |
| 62 | + if (text.matches("\\d+")) { |
| 63 | + return LocalDateTime.ofEpochSecond(Long.parseLong(text), 0, ZoneOffset.UTC); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + return LocalDateTime.ofInstant(Instant.parse(text), ZoneOffset.UTC); |
| 68 | + } catch (DateTimeParseException e) { |
| 69 | + // try next format |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + return LocalDateTime.parse(text); |
| 74 | + } catch (DateTimeParseException e) { |
| 75 | + // try next format |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + return LocalDate.parse(text).atStartOfDay(); |
| 80 | + } catch (DateTimeParseException e) { |
| 81 | + System.err.format("Cannot parse time: '%s'%n", text); |
| 82 | + System.exit(1); |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static void showUsageAndExit() { |
| 88 | + System.err.println("Usage: DateUtil [options]"); |
| 89 | + System.err.println(" --date=<time> time in epoch seconds, or in iso-8601 or yyyy-MM-dd format"); |
| 90 | + System.err.println(" --format=<format> output format"); |
| 91 | + System.exit(1); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments