Java Long Number Create toLongObject(Object o)

Here you can find the source of toLongObject(Object o)

Description

to Long Object

License

MIT License

Declaration

public static Long toLongObject(Object o) 

Method Source Code

//package com.java2s;
/*!/*  ww w .j a  v  a2  s. c o m*/
  * mifmi-commons4j
  * https://github.com/mifmi/mifmi-commons4j
  *
  * Copyright (c) 2015 mifmi.org and other contributors
  * Released under the MIT license
  * https://opensource.org/licenses/MIT
  */

import java.text.DecimalFormat;

public class Main {
    public static Long toLongObject(Object o) {
        if (o == null) {
            return null;
        }

        Long num;
        if (o instanceof Long) {
            num = (Long) o;
        } else if (o instanceof Number) {
            num = Long.valueOf(((Number) o).longValue());
        } else if (o instanceof Character) {
            num = Long.valueOf((long) ((Character) o).charValue());
        } else if (o instanceof String) {
            String s = (String) o;
            if (s.isEmpty()) {
                num = null;
            } else {
                num = Long.valueOf(s);
            }
        } else if (o instanceof Boolean) {
            num = ((Boolean) o).booleanValue() ? Long.valueOf(1L) : Long.valueOf(0L);
        } else if (o instanceof Enum) {
            num = Long.valueOf((long) ((Enum<?>) o).ordinal());
        } else {
            num = Long.valueOf(o.toString());
        }

        return num;
    }

    public static String toString(Number value, String pattern) {
        if (value == null) {
            return null;
        }

        DecimalFormat df = new DecimalFormat(pattern);

        return df.format(value);
    }
}

Related

  1. toLong(String value, long def)
  2. toLong(String value, Long defaultValue)
  3. toLong(String value, long defaultValue)
  4. toLong(String value, Long defaultValue)
  5. toLong(T value)