Package: DateTimeUtil
DateTimeUtil
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DateTimeUtil() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toDateTime(Instant) |
|
|
|
|
|
||||||||||||||||||||
toDateTime(LocalDateTime) |
|
|
|
|
|
||||||||||||||||||||
toTime(LocalTime) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.datatype;
19:
20: import java.time.*;
21: import java.util.Objects;
22:
23: /**
24: * Utility class for transformation between various date/time representations.
25: */
26: public class DateTimeUtil {
27:
28: /**
29: * System timezone offset used for transforming local datetime values to offset ones
30: */
31: public static final ZoneOffset SYSTEM_OFFSET = ZoneId.systemDefault().getRules().getOffset(LocalDateTime.now());
32:
33: private DateTimeUtil() {
34: throw new AssertionError();
35: }
36:
37: /**
38: * Transforms the specified {@link LocalDateTime} to an {@link OffsetDateTime}, using the system's default offset.
39: *
40: * @param value Datetime to transform
41: * @return Offset datetime
42: */
43: public static OffsetDateTime toDateTime(LocalDateTime value) {
44: Objects.requireNonNull(value);
45: return OffsetDateTime.of(value.toLocalDate(), value.toLocalTime(), SYSTEM_OFFSET);
46: }
47:
48: /**
49: * Transforms the specified {@link Instant} to an {@link OffsetDateTime}.
50: * <p>
51: * The instant is taken to be at the UTC timezone.
52: *
53: * @param value Instant value to transform
54: * @return Offset datetime
55: */
56: public static OffsetDateTime toDateTime(Instant value) {
57: return Objects.requireNonNull(value).atOffset(ZoneOffset.UTC);
58: }
59:
60: /**
61: * Transforms the specified {@link LocalTime} to an {@link OffsetTime}, using the system's default offset.
62: *
63: * @param value Time to transform
64: * @return Offset time
65: */
66: public static OffsetTime toTime(LocalTime value) {
67: return OffsetTime.of(Objects.requireNonNull(value), SYSTEM_OFFSET);
68: }
69: }