001/* Copyright (C) 2014 konik.io
002 *
003 * This file is part of the Konik library.
004 *
005 * The Konik library is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * The Konik library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>.
017 */
018package io.konik.zugferd.qualified;
019
020import static io.konik.zugferd.unece.codes.DateTimeType._102;
021import io.konik.zugferd.unece.codes.DateTimeType;
022
023import java.util.Date;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAttribute;
028import javax.xml.bind.annotation.XmlType;
029import javax.xml.bind.annotation.XmlValue;
030
031/**
032 * = Date Time
033 * 
034 * Consist of the string date +value+ and a +code+ number. The +code+ number indicates how to interpret the string date.
035 * 
036 * @see http://www.unece.org/trade/untdid/d13b/tred/tred2379.htm[UN/EDIFACT 2379 Date or time or period format code^]
037 */
038@XmlAccessorType(XmlAccessType.NONE)
039@XmlType(name = "FormattedDateTimeType", propOrder = { "value" })
040public class DateTime {
041
042   @XmlAttribute(name = "format")
043   private final String code;
044   
045   @XmlValue
046   private final String value;
047   
048   /** Instantiates a new date time with todays value and code 102. */
049   public DateTime() {
050      this(_102);
051   }
052
053   /**
054    * Instantiates a new date time with the given date and the code 102.
055    * @param date the date as long
056    */
057   public DateTime(Date date) {
058      this(_102, date);
059   }
060   
061   /**
062    * Instantiates a new date time with the given date and the code 102.
063    * @param date the date as long
064    */
065   public DateTime(long date) {
066      this(_102, new Date(date));
067   }
068   
069   /**
070    * Instantiates a new date time with todays value and the given type.
071    * 
072    * @param type the {@link DateTimeType}
073    */
074   public DateTime(DateTimeType type) {
075      this(type, new Date());
076   }
077   
078   /**
079    * Instantiates a new date time with the given type and given date.
080    *
081    * @param type the type
082    * @param date the date
083    */
084   public DateTime(DateTimeType type, Date date) {
085      this(type.getCode(),type.format(date));
086   }
087   
088   /**
089    * Instantiates a new date time.
090    *
091    * @param code the code
092    * @param value the value
093    */
094   public DateTime(String code, String value) {
095      this.code = code;
096      this.value = value;
097   }
098
099   /**
100    * Gets the formatted date value.
101    * 
102    * @return the formatted value
103    */
104   public String getValue() {
105      return value;
106   }
107   
108   /**
109    * Gets the date code code.
110
111    * Example:: +102+
112    * 
113    * @return the code
114    */
115   public String getCode() {
116      return code;
117   }
118   
119   /**
120    * As date.
121    *
122    * @return the date
123    */
124   public Date asDate() {
125      return DateTimeType.getByCode(code).parse(value);
126   }
127}