All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.converter.OAConverterOATime Maven / Gradle / Ivy

The newest version!
/*  Copyright 1999 Vince Via [email protected]
    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.viaoa.converter;

import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import com.viaoa.util.OAConverter;
import com.viaoa.util.OADateTime;
import com.viaoa.util.OATime;

/**
 * Convert to/from a OATime value. 
* Converts the following to an OATime *
    *
  • String, using optional format string. *
  • Time *
  • Date *
  • OADateTime *
  • All others value will return null. *
*
* Converts an OATime to any of the following *
    *
  • String, using an optional format. *
* See OADateTime for format definitions. * * @see OAConverter * @see OADateTime * @see OATime */ public class OAConverterOATime implements OAConverterInterface { /** * Convert to/from a OATime value. * * @return Object of type clazz if conversion can be done, else null. */ public Object convert(Class clazz, Object value, String fmt) { if (clazz == null) { return null; } if (clazz.equals(OATime.class)) { return convertToOATime(value, fmt); } if (value != null && value instanceof OATime) { return convertFromOATime(clazz, (OATime) value, fmt); } return null; } protected OATime convertToOATime(Object value, String fmt) { if (value == null) { return null; } if (value instanceof OATime) { return (OATime) value; } if (value instanceof String) { return (OATime) OATime.valueOf((String) value, fmt); } if (value instanceof Date) { return new OATime((Date) value); } if (value instanceof OADateTime) { return new OATime((OADateTime) value); } if (value instanceof byte[]) { return new OATime(new java.math.BigInteger((byte[]) value).longValue()); } if (value instanceof Number) { return new OATime(((Number) value).longValue()); } if (value instanceof Instant) { OATime out = new OATime(new java.sql.Date(Date.from((Instant) value).getTime())); return out; } if (value instanceof LocalDate) { LocalDate ld = (LocalDate) value; OATime out = new OATime(new Date(ld.getYear() - 1900, (ld.getMonth().getValue()) - 1, ld.getDayOfMonth())); return out; } if (value instanceof LocalTime) { LocalTime lt = (LocalTime) value; int ms = (int) (lt.getNano() / Math.pow(10, 6)); OATime out = new OATime(lt.getHour(), lt.getMinute(), lt.getSecond(), ms); return out; } if (value instanceof LocalDateTime) { LocalDateTime ldt = (LocalDateTime) value; Date out = new java.sql.Date(Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()).getTime()); return new OATime(out); } if (value instanceof ZonedDateTime) { ZonedDateTime zdt = (ZonedDateTime) value; Date out = new java.sql.Date(Date.from(zdt.toInstant()).getTime()); return new OATime(out); } return null; } protected Object convertFromOATime(Class toClass, OATime timeValue, String fmt) { if (toClass.equals(String.class)) { return (timeValue).toString(fmt); } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy