View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons;
20  
21  import java.io.Serializable;
22  import java.time.OffsetDateTime;
23  import java.time.ZoneId;
24  import java.time.ZoneOffset;
25  import java.time.ZonedDateTime;
26  import java.util.Date;
27  import java.util.Optional;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.commons.lang3.time.FastDateFormat;
30  import org.apache.wicket.model.IModel;
31  
32  public final class DateOps {
33  
34      public static class Format implements Serializable {
35  
36          private static final long serialVersionUID = 27103019852866L;
37  
38          private final FastDateFormat fdf;
39  
40          public Format(final FastDateFormat fdf) {
41              this.fdf = fdf;
42          }
43  
44          public String format(final Date date) {
45              return Optional.ofNullable(date).map(fdf::format).orElse(StringUtils.EMPTY);
46          }
47  
48          public String format(final OffsetDateTime date) {
49              return Optional.ofNullable(date).map(v -> fdf.format(convert(date))).orElse(StringUtils.EMPTY);
50          }
51  
52          public String format(final ZonedDateTime date) {
53              return Optional.ofNullable(date).map(v -> fdf.format(convert(date))).orElse(StringUtils.EMPTY);
54          }
55      }
56  
57      public static final class WrappedDateModel implements IModel<Date>, Serializable {
58  
59          private static final long serialVersionUID = 31027882183172L;
60  
61          public static WrappedDateModel ofOffset(final IModel<OffsetDateTime> offset) {
62              WrappedDateModel instance = new WrappedDateModel();
63              instance.offset = offset;
64              return instance;
65          }
66  
67          public static WrappedDateModel ofZoned(final IModel<ZonedDateTime> zoned) {
68              WrappedDateModel instance = new WrappedDateModel();
69              instance.zoned = zoned;
70              return instance;
71          }
72  
73          private IModel<OffsetDateTime> offset;
74  
75          private IModel<ZonedDateTime> zoned;
76  
77          private WrappedDateModel() {
78              // private constructor for static utility class
79          }
80  
81          @Override
82          public Date getObject() {
83              return offset == null ? convert(zoned.getObject()) : convert(offset.getObject());
84          }
85  
86          @Override
87          public void setObject(final Date object) {
88              if (offset == null) {
89                  zoned.setObject(toZonedDateTime(object));
90              } else {
91                  offset.setObject(toOffsetDateTime(object));
92              }
93          }
94      }
95  
96      public static final ZoneOffset DEFAULT_OFFSET = OffsetDateTime.now().getOffset();
97  
98      public static final ZoneId DEFAULT_ZONE = ZonedDateTime.now().getZone();
99  
100     public static Date convert(final OffsetDateTime date) {
101         return Optional.ofNullable(date).map(v -> new Date(v.toInstant().toEpochMilli())).orElse(null);
102     }
103 
104     public static Date convert(final ZonedDateTime date) {
105         return Optional.ofNullable(date).map(v -> new Date(v.toInstant().toEpochMilli())).orElse(null);
106     }
107 
108     public static OffsetDateTime toOffsetDateTime(final Date date) {
109         return Optional.ofNullable(date).map(v -> v.toInstant().atOffset(DEFAULT_OFFSET)).orElse(null);
110     }
111 
112     public static ZonedDateTime toZonedDateTime(final Date date) {
113         return Optional.ofNullable(date).map(v -> ZonedDateTime.ofInstant(v.toInstant(), DEFAULT_ZONE)).orElse(null);
114     }
115 
116     private DateOps() {
117         // private constructor for static utility class
118     }
119 }