Example usage for java.lang Long parseLong

List of usage examples for java.lang Long parseLong

Introduction

In this page you can find the example usage for java.lang Long parseLong.

Prototype

public static long parseLong(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed decimal long .

Usage

From source file:Main.java

@Nullable
public static Object valueFromString(String newValue, Object existingValue) throws IllegalArgumentException {
    if (existingValue instanceof Integer) {
        return Integer.parseInt(newValue);
    } else if (existingValue instanceof Long) {
        return Long.parseLong(newValue);
    } else if (existingValue instanceof Float) {
        return Float.parseFloat(newValue);
    } else if (existingValue instanceof Boolean) {
        return parseBoolean(newValue);
    } else if (existingValue instanceof String) {
        return newValue;
    } else if (existingValue instanceof Set) {
        try {//from www.  j a va 2s  .c o  m
            JSONArray obj = new JSONArray(newValue);
            int objN = obj.length();
            HashSet<String> set = new HashSet<String>(objN);
            for (int i = 0; i < objN; i++) {
                set.add(obj.getString(i));
            }
            return set;
        } catch (JSONException e) {
            throw new IllegalArgumentException(e);
        }
    } else {
        throw new IllegalArgumentException("Unsupported type: " + existingValue.getClass().getName());
    }
}

From source file:Main.java

public static String milliToMinutes(String milliseconds) {
    Long millis = Long.parseLong(milliseconds);
    milliseconds = String.format("%dmin : %dsec", TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

    return milliseconds;
}

From source file:Main.java

/**
 * This method parses the given value into the specified primitive or wrapper class.
 * @param clazz - primitive or wrapper class used to parse
 * @param value - string to be parsed//from  w w  w.  j  a va2  s  .c o  m
 * @return object of type clazz parsed from the string
 * @author Trisan Bepler
 */
public static Object toObject(Class<?> clazz, String value) {
    if (Boolean.TYPE == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.TYPE == clazz)
        return Byte.parseByte(value);
    if (Short.TYPE == clazz)
        return Short.parseShort(value);
    if (Integer.TYPE == clazz)
        return Integer.parseInt(value);
    if (Long.TYPE == clazz)
        return Long.parseLong(value);
    if (Float.TYPE == clazz)
        return Float.parseFloat(value);
    if (Double.TYPE == clazz)
        return Double.parseDouble(value);
    if (Boolean.class == clazz)
        return Boolean.parseBoolean(value);
    if (Byte.class == clazz)
        return Byte.parseByte(value);
    if (Short.class == clazz)
        return Short.parseShort(value);
    if (Integer.class == clazz)
        return Integer.parseInt(value);
    if (Long.class == clazz)
        return Long.parseLong(value);
    if (Float.class == clazz)
        return Float.parseFloat(value);
    if (Double.class == clazz)
        return Double.parseDouble(value);
    if (Character.class == clazz)
        return value.charAt(0);
    if (Character.TYPE == clazz)
        return value.charAt(0);
    return value;
}

From source file:Main.java

public static String formatDateByLongTime(String longtime, String format) {
    try {/*  www .j av  a 2s  . c  om*/
        Long lon = Long.parseLong(longtime);
        Date date = new Date(lon);
        return formatDate(date, format);
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

}

From source file:Main.java

public static long getLongNodeValue(Node node, String nodeName) {
    return Long.parseLong(getStringNodeValue(node, nodeName));
}

From source file:Main.java

public static String formatDateCustom(String beginDate, String format) {
    return new SimpleDateFormat(format).format(new Date(Long.parseLong(beginDate)));
}

From source file:Main.java

public static long[] toLongArray(String... strings) {
    long[] longs = new long[strings.length];
    for (int i = 0; i < longs.length; i++) {
        longs[i] = Long.parseLong(strings[i]);
    }//from  w ww  . j a v  a2s .co  m
    return longs;
}

From source file:com.u2apple.rt.util.PartitionUtils.java

public static Partitions parsePartition(String partitionString) {
    Partitions partitions = new Partitions();
    if (StringUtils.isNotBlank(partitionString)) {
        String[] partitionArray = partitionString.split(";");
        for (String partitionStr : partitionArray) {
            String[] elements = partitionStr.split("\\|");
            String name = StringUtils.trim(elements[0]);
            Long size = Long.parseLong(StringUtils.trim(elements[1]));
            String path = StringUtils.trim(elements[2]);
            Partition partition = new Partition(name, size, path);
            if ("system".equalsIgnoreCase(name)) {
                partitions.setSystemPartition(partition);
            } else if ("cache".equalsIgnoreCase(name)) {
                partitions.setCachePartition(partition);
            } else if ("data".equalsIgnoreCase(name)) {
                partitions.setDataPartition(partition);
            }//from   w w  w .  ja va  2  s .  c o  m
        }
    }
    return partitions;
}

From source file:Main.java

public static Long getTwitterIdFromUrl(String sourceUrl) {
    String id = sourceUrl.replaceFirst("#!/", "");
    int index = id.lastIndexOf("/");
    if (index > 0) {
        index++;/* w  w  w. j av a2s .c o  m*/
        try {
            return Long.parseLong(id.substring(index));
        } catch (NumberFormatException ex) {
        }
    }
    return null;
}

From source file:Main.java

public static Date parseDate(String d) throws IllegalArgumentException {
    if (d == null || d.length() == 0)
        return null;
    if (!d.startsWith("@"))
        throw new IllegalArgumentException();
    return new Date(Long.parseLong(d.substring(1)));
}