View Javadoc
1   package net.sf.mbus4j.dataframes.datablocks;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  import java.text.DateFormat;
31  import java.text.ParseException;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  import net.sf.json.JSONObject;
35  
36  import net.sf.mbus4j.dataframes.datablocks.dif.DataFieldCode;
37  import net.sf.mbus4j.dataframes.datablocks.vif.Vif;
38  import net.sf.mbus4j.dataframes.datablocks.vif.Vife;
39  
40  /**
41   *
42   * @author arnep@users.sourceforge.net
43   * @version $Id: DateDataBlock.java 162 2016-07-27 18:07:08Z arnep $
44   */
45  public class DateDataBlock extends DataBlock {
46      // SimpleDateFormat is not thread-safe, so give one to each thread
47      private static final ThreadLocal<SimpleDateFormat> ISO_8601 = new ThreadLocal<SimpleDateFormat>(){
48          @Override
49          protected SimpleDateFormat initialValue()
50          {
51              return new SimpleDateFormat("yyyy-MM-dd");
52          }
53      };
54  
55      private Date value;
56  
57      public DateDataBlock() {
58          super();
59      }
60  
61      @Deprecated
62      public DateDataBlock(Vif vif, Vife... vifes) {
63          super(DataFieldCode._16_BIT_INTEGER, vif, vifes);
64      }
65  
66      /**
67       * @return the value
68       */
69      public Date getValue() {
70          return value;
71      }
72  
73      @Override
74      public String getValueAsString() {
75          return ISO_8601.get().format(value);
76      }
77  
78      /**
79       * @param value the value to set
80       */
81      public void setValue(Date value) {
82          this.value = value;
83      }
84  
85      @Override
86      protected void accumulateDatatoJSON(JSONObject json) {
87          JSONObject jsonValue = new JSONObject();
88          jsonValue.accumulate("date", ISO_8601.get().format(value));
89          json.accumulate("data", jsonValue);
90      }
91  
92      @Override
93      public void fromJSON(JSONObject json) {
94          super.fromJSON(json);
95          try {
96              JSONObject jsonValue = json.getJSONObject("data");
97              value = ISO_8601.get().parse(jsonValue.getString("date"));
98          } catch (ParseException ex) {
99              throw new RuntimeException(ex);
100         }
101     }
102 
103     @Override
104     public void setValue(String text) {
105         try {
106             value = DateFormat.getDateInstance().parse(text);
107         } catch (ParseException ex) {
108             throw new RuntimeException(ex);
109         }
110     }
111 }