format Date String to append 0 if necessary - Android java.util

Android examples for java.util:Date Format

Description

format Date String to append 0 if necessary

Demo Code

import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String formatDateString(int _year, int _month, int _day,
            int _hour, int _minute) {
        String value = String.valueOf(_year);
        if (_month < 10) {
            value += "0" + _month;
        } else {/*from w w w. j a v a2  s  .  c o m*/
            value += _month;
        }

        if (_day < 10) {
            value += "0" + _day;
        } else {
            value += _day;
        }

        if (_hour < 10) {
            value += "0" + _hour;
        } else {
            value += _hour;
        }

        if (_minute < 10) {
            value += "0" + _minute;
        } else {
            value += _minute;
        }

        value += "00";
        return value;
    }

    public static String formatDateString(int year, int month, int day) {
        String value = String.valueOf(year);
        while (value.length() < 4) {
            value = "0" + value;
        }
        if (month < 10) {
            value += "0" + month;
        } else {
            value += month;
        }

        if (day < 10) {
            value += "0" + day;
        } else {
            value += day;
        }

        return value;
    }

}

Related Tutorials