parse Int Using Format - Java java.lang

Java examples for java.lang:String Parse

Description

parse Int Using Format

Demo Code

//package com.java2s;

import java.text.DecimalFormat;
import java.text.ParseException;

public class Main {
    public static void main(String[] argv) {
        String source = "123";
        String formatPattern = "###";
        System.out.println(parseIntUsingFormat(source, formatPattern));
    }/*  ww w. ja va 2s.  c  o  m*/

    public static int parseIntUsingFormat(String source,
            String formatPattern) {
        DecimalFormat df = new DecimalFormat(formatPattern);
        try {
            return df.parse(source).intValue();
        } catch (ParseException e) {
            return -1;
        }
    }
}

Related Tutorials