Java - Write code to pad long with zero and return as a String

Requirements

Write code to pad long with zero and return as a String

Demo

//package com.book2s;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) {
        long val = 42;
        int len = 4;
        System.out.println(long2StringLeftZero(val, len));
    }// w w w  .  j  a  va2  s  .c o m

    public static String long2StringLeftZero(long val, int len) {
        String pattern = "";
        for (int i = 0; i < len; i++)
            pattern += '0';

        DecimalFormat fmt = new DecimalFormat(pattern);
        return fmt.format(val);
    }
}