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 DateTimeOffset.

Author(s):
SAP AG
 
 public class EdmDateTimeOffset extends AbstractSimpleType {
 
   private static final Pattern PATTERN = Pattern.compile(
       "(\\p{Digit}{1,4})-(\\p{Digit}{1,2})-(\\p{Digit}{1,2})"
           + "T(\\p{Digit}{1,2}):(\\p{Digit}{1,2})(?::(\\p{Digit}{1,2})(\\.\\p{Digit}{1,7})?)?"
           + "(Z|([-+]\\p{Digit}{1,2}:\\p{Digit}{2}))?");
   private static final Pattern JSON_PATTERN = Pattern.compile(
       "/Date\\((-?\\p{Digit}+)(?:(\\+|-)(\\p{Digit}{1,4}))?\\)/");
   private static final EdmDateTimeOffset instance = new EdmDateTimeOffset();
 
   public static EdmDateTimeOffset 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 {
     if (literalKind == .) {
       if (value.length() > 16 && value.startsWith("datetimeoffset'") && value.endsWith("'")) {
         return internalValueOfString(value.substring(15, value.length() - 1), .facetsreturnType);
       } else {
       }
     }
 
     Calendar dateTimeValue = null;
 
     if (literalKind == .) {
       final Matcher matcher = .matcher(value);
       if (matcher.matches()) {
         dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
         dateTimeValue.clear();
         long millis;
         try {
           millis = Long.parseLong(matcher.group(1));
         } catch (final NumberFormatException e) {
         }
         dateTimeValue.setTimeInMillis(millis);
         if (matcher.group(2) != null) {
           final int offsetInMinutes = Integer.parseInt(matcher.group(3));
           if (offsetInMinutes >= 24 * 60) {
           }
           if (offsetInMinutes != 0) {
             dateTimeValue.setTimeZone(TimeZone.getTimeZone(
                 "GMT" + matcher.group(2) + String.valueOf(offsetInMinutes / 60)
                     + ":" + String.format("%02d"offsetInMinutes % 60)));
             // Subtract the time-zone offset to counter the automatic adjustment above
             // caused by the fact that the Calendar instance created above is in the
             // "GMT" timezone.
             dateTimeValue.add(., 0 - dateTimeValue.get(.));
           }
         }
       }
     }
 
     if (dateTimeValue == null) {
       final Matcher matcher = .matcher(value);
       if (!matcher.matches()) {
       }
 
       if (matcher.group(8) == null) {
        return EdmDateTime.getInstance().valueOfString(value.facetsreturnType);
      } else {
        dateTimeValue = EdmDateTime.getInstance().valueOfString(value.substring(0, matcher.start(8)), .facetsCalendar.class);
        if (matcher.group(9) != null && !matcher.group(9).matches("[-+]0+:0+")) {
          dateTimeValue.setTimeZone(TimeZone.getTimeZone("GMT" + matcher.group(9)));
          if (dateTimeValue.get(.) == 0) {
          }
          // Subtract the time-zone offset to counter the automatic adjustment above
          // caused by the fact that the Calendar instance returned from EdmDateTime
          // is in the "GMT" timezone.
          dateTimeValue.add(., 0 - dateTimeValue.get(.));
        }
      }
    }
    if (returnType.isAssignableFrom(Calendar.class)) {
      return returnType.cast(dateTimeValue);
    } else if (returnType.isAssignableFrom(Long.class)) {
      return returnType.cast(dateTimeValue.getTimeInMillis());
    } else if (returnType.isAssignableFrom(Date.class)) {
      return returnType.cast(dateTimeValue.getTime());
    } 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 int offset = dateTimeValue.get(.) + dateTimeValue.get(.);
    dateTimeValue.add(.offset); // to counter UTC output below
    final int offsetInMinutes = offset / 60 / 1000;
    if (literalKind == .) {
      return "/Date("
          + Long.toString(dateTimeValue.getTimeInMillis())
          + (offset == 0 ? "" : String.format("%+05d"offsetInMinutes))
          + ")/";
    } else {
      final String localTimeString = EdmDateTime.getInstance().valueToString(dateTimeValue.facets);
      final int offsetHours = offsetInMinutes / 60;
      final int offsetMinutes = Math.abs(offsetInMinutes % 60);
      final String offsetString = offset == 0 ? "Z" : String.format("%+03d:%02d"offsetHoursoffsetMinutes);
      return localTimeString + offsetString;
    }
  }
  public String toUriLiteral(final String literalthrows EdmSimpleTypeException {
    return "datetimeoffset'" + literal + "'";
  }
New to GrepCode? Check out our FAQ X