Class Converter


  • public final class Converter
    extends Object
    Handy conversion utilities. Convert from primitive to other primitives, plus support for Date, TimeStamp SQL Date, and the Atomic's.
    Author:
    John DeRegnaucourt ([email protected])
    Copyright (c) Cedar Software LLC

    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.
    • Method Detail

      • convert

        public static <T> T convert​(Object fromInstance,
                                    Class<T> toType)
        Turn the passed in value to the class indicated. This will allow, for example, a String value to be passed in and have it coerced to a Long.
             Examples:
             Long x = convert("35", Long.class);
             Date d = convert("2015/01/01", Date.class)
             int y = convert(45.0, int.class)
             String date = convert(date, String.class)
             String date = convert(calendar, String.class)
             Short t = convert(true, short.class);     // returns (short) 1 or  (short) 0
             Long date = convert(calendar, long.class); // get calendar's time into long
         
        Parameters:
        fromInstance - A value used to create the targetType, even though it may not (most likely will not) be the same data type as the targetType
        toType - Class which indicates the targeted (final) data type. Please note that in addition to the 8 Java primitives, the targeted class can also be Date.class, String.class, BigInteger.class, BigDecimal.class, and the Atomic classes. The primitive class can be either primitive class or primitive wrapper class, however, the returned value will always [obviously] be a primitive wrapper.
        Returns:
        An instanceof targetType class, based upon the value passed in.
      • convertToString

        public static String convertToString​(Object fromInstance)
      • convertToBigDecimal

        public static BigDecimal convertToBigDecimal​(Object fromInstance)
      • convertToBigInteger

        public static BigInteger convertToBigInteger​(Object fromInstance)
      • convertToSqlDate

        public static Date convertToSqlDate​(Object fromInstance)
      • convertToTimestamp

        public static Timestamp convertToTimestamp​(Object fromInstance)
      • convertToDate

        public static Date convertToDate​(Object fromInstance)
      • convertToCalendar

        public static Calendar convertToCalendar​(Object fromInstance)
      • convertToByte

        public static Byte convertToByte​(Object fromInstance)
      • convertToShort

        public static Short convertToShort​(Object fromInstance)
      • convertToInteger

        public static Integer convertToInteger​(Object fromInstance)
      • convertToLong

        public static Long convertToLong​(Object fromInstance)
      • convertToFloat

        public static Float convertToFloat​(Object fromInstance)
      • convertToDouble

        public static Double convertToDouble​(Object fromInstance)
      • convertToBoolean

        public static Boolean convertToBoolean​(Object fromInstance)
      • convertToAtomicInteger

        public static AtomicInteger convertToAtomicInteger​(Object fromInstance)
      • convertToAtomicLong

        public static AtomicLong convertToAtomicLong​(Object fromInstance)
      • convertToAtomicBoolean

        public static AtomicBoolean convertToAtomicBoolean​(Object fromInstance)