Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package com.sap.core.odata.core.edm;
 
 import java.util.Date;
 

Implementation of the EDM simple type Time.

Arguably, this type is intended to represent a time of day, not an instance in time. The time value is interpreted and formatted as local time.

Formatting simply ignores the year, month, and day parts of time instances. Parsing returns a Calendar object where all unused fields have been cleared.

Author(s):
SAP AG
 
 public class EdmTime extends AbstractSimpleType {
 
   private static final Pattern PATTERN = Pattern.compile(
       "PT(?:(\\p{Digit}{1,2})H)?(?:(\\p{Digit}{1,4})M)?(?:(\\p{Digit}{1,5})(?:\\.(\\p{Digit}+?)0*)?S)?");
   private static final EdmTime instance = new EdmTime();
 
   public static EdmTime getInstance() {
     return ;
   }
 
   @Override
   public Class<?> getDefaultType() {
     return Calendar.class;
   }
 
   @Override
   protected <T> T internalValueOfString(final String valuefinal EdmLiteralKind literalKindfinal EdmFacets facetsfinal Class<T> returnTypethrows EdmSimpleTypeException {
     Calendar valueCalendar;
     if (literalKind == .) {
       if (value.length() > 6 && value.startsWith("time'") && value.endsWith("'")) {
         valueCalendar = parseLiteral(value.substring(5, value.length() - 1), facets);
       } else {
       }
     } else {
       valueCalendar = parseLiteral(valuefacets);
     }
 
     if (returnType.isAssignableFrom(Calendar.class)) {
       return returnType.cast(valueCalendar);
     } else if (returnType.isAssignableFrom(Long.class)) {
       return returnType.cast(valueCalendar.getTimeInMillis());
     } else if (returnType.isAssignableFrom(Date.class)) {
       return returnType.cast(valueCalendar.getTime());
     } else {
     }
   }
 
   private Calendar parseLiteral(final String literalfinal EdmFacets facetsthrows EdmSimpleTypeException {
     final Matcher matcher = .matcher(literal);
     if (!matcher.matches()
         || (matcher.group(1) == null && matcher.group(2) == null && matcher.group(3) == null)) {
     }
 
     Calendar dateTimeValue = Calendar.getInstance();
     dateTimeValue.clear();
 
     if (matcher.group(1) != null) {
       dateTimeValue.set(., Integer.parseInt(matcher.group(1)));
     }
     if (matcher.group(2) != null) {
       dateTimeValue.set(., Integer.parseInt(matcher.group(2)));
     }
     if (matcher.group(3) != null) {
       dateTimeValue.set(., Integer.parseInt(matcher.group(3)));
     }
 
     if (matcher.group(4) != null) {
       if (facets == null || facets.getPrecision() == null || facets.getPrecision() >= matcher.group(4).length()) {
         if (matcher.group(4).length() <= 3) {
          dateTimeValue.set(.,
              Short.parseShort(matcher.group(4) + "000".substring(0, 3 - matcher.group(4).length())));
        } else {
        }
      } else {
      }
    }
    if (dateTimeValue.get(.) == 1) {
      return dateTimeValue;
    } else {
    }
  }
  protected <T> String internalValueToString(final T valuefinal EdmLiteralKind literalKindfinal EdmFacets facetsthrows EdmSimpleTypeException {
    Calendar dateTimeValue;
    if (value instanceof Date) {
      dateTimeValue = Calendar.getInstance();
      dateTimeValue.clear();
      dateTimeValue.setTime((Datevalue);
    } else if (value instanceof Calendar) {
      dateTimeValue = (Calendar) ((Calendarvalue).clone();
    } else if (value instanceof Long) {
      dateTimeValue = Calendar.getInstance();
      dateTimeValue.clear();
      dateTimeValue.setTimeZone(TimeZone.getTimeZone("GMT"));
      dateTimeValue.setTimeInMillis((Longvalue);
    } else {
    }
    final String pattern = "'PT'H'H'm'M's.SSS";
    SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance();
    dateFormat.setTimeZone(dateTimeValue.getTimeZone());
    dateFormat.applyPattern(pattern);
    String result = dateFormat.format(dateTimeValue.getTime());
    if (facets == null || facets.getPrecision() == null) {
      while (result.endsWith("0")) {
        result = result.substring(0, result.length() - 1);
      }
    } else if (facets.getPrecision() <= 3) {
      if (result.endsWith("000".substring(0, 3 - facets.getPrecision()))) {
        result = result.substring(0, result.length() - (3 - facets.getPrecision()));
      } else {
      }
    } else {
      for (int i = 4; i <= facets.getPrecision(); i++) {
        result += "0";
      }
    }
    if (result.endsWith(".")) {
      result = result.substring(0, result.length() - 1);
    }
    result += "S";
    return result;
  }
  public String toUriLiteral(final String literal) {
    return "time'" + literal + "'";
  }
New to GrepCode? Check out our FAQ X