Is there a Util to convert US State Name to State Code. eg. Arizona to AZ?

I know this doesn't answer the question, but just in case someone needs the opposite: to use state/province abbreviations as keys and names as values. It took me a few minutes to reformat the map. Also, the abbreviation for Quebec in the accepted answer is wrong.

public static final Map<String, String> STATE_MAP;
static {
    STATE_MAP = new HashMap<String, String>();
    STATE_MAP.put("AL", "Alabama");
    STATE_MAP.put("AK", "Alaska");
    STATE_MAP.put("AB", "Alberta");
    STATE_MAP.put("AZ", "Arizona");
    STATE_MAP.put("AR", "Arkansas");
    STATE_MAP.put("BC", "British Columbia");
    STATE_MAP.put("CA", "California");
    STATE_MAP.put("CO", "Colorado");
    STATE_MAP.put("CT", "Connecticut");
    STATE_MAP.put("DE", "Delaware");
    STATE_MAP.put("DC", "District Of Columbia");
    STATE_MAP.put("FL", "Florida");
    STATE_MAP.put("GA", "Georgia");
    STATE_MAP.put("GU", "Guam");
    STATE_MAP.put("HI", "Hawaii");
    STATE_MAP.put("ID", "Idaho");
    STATE_MAP.put("IL", "Illinois");
    STATE_MAP.put("IN", "Indiana");
    STATE_MAP.put("IA", "Iowa");
    STATE_MAP.put("KS", "Kansas");
    STATE_MAP.put("KY", "Kentucky");
    STATE_MAP.put("LA", "Louisiana");
    STATE_MAP.put("ME", "Maine");
    STATE_MAP.put("MB", "Manitoba");
    STATE_MAP.put("MD", "Maryland");
    STATE_MAP.put("MA", "Massachusetts");
    STATE_MAP.put("MI", "Michigan");
    STATE_MAP.put("MN", "Minnesota");
    STATE_MAP.put("MS", "Mississippi");
    STATE_MAP.put("MO", "Missouri");
    STATE_MAP.put("MT", "Montana");
    STATE_MAP.put("NE", "Nebraska");
    STATE_MAP.put("NV", "Nevada");
    STATE_MAP.put("NB", "New Brunswick");
    STATE_MAP.put("NH", "New Hampshire");
    STATE_MAP.put("NJ", "New Jersey");
    STATE_MAP.put("NM", "New Mexico");
    STATE_MAP.put("NY", "New York");
    STATE_MAP.put("NF", "Newfoundland");
    STATE_MAP.put("NC", "North Carolina");
    STATE_MAP.put("ND", "North Dakota");
    STATE_MAP.put("NT", "Northwest Territories");
    STATE_MAP.put("NS", "Nova Scotia");
    STATE_MAP.put("NU", "Nunavut");
    STATE_MAP.put("OH", "Ohio");
    STATE_MAP.put("OK", "Oklahoma");
    STATE_MAP.put("ON", "Ontario");
    STATE_MAP.put("OR", "Oregon");
    STATE_MAP.put("PA", "Pennsylvania");
    STATE_MAP.put("PE", "Prince Edward Island");
    STATE_MAP.put("PR", "Puerto Rico");
    STATE_MAP.put("QC", "Quebec");
    STATE_MAP.put("RI", "Rhode Island");
    STATE_MAP.put("SK", "Saskatchewan");
    STATE_MAP.put("SC", "South Carolina");
    STATE_MAP.put("SD", "South Dakota");
    STATE_MAP.put("TN", "Tennessee");
    STATE_MAP.put("TX", "Texas");
    STATE_MAP.put("UT", "Utah");
    STATE_MAP.put("VT", "Vermont");
    STATE_MAP.put("VI", "Virgin Islands");
    STATE_MAP.put("VA", "Virginia");
    STATE_MAP.put("WA", "Washington");
    STATE_MAP.put("WV", "West Virginia");
    STATE_MAP.put("WI", "Wisconsin");
    STATE_MAP.put("WY", "Wyoming");
    STATE_MAP.put("YT", "Yukon Territory");
}

I have it ready-made - you can use it:

Map<String, String> states = new HashMap<String, String>();
states.put("Alabama","AL");
states.put("Alaska","AK");
states.put("Alberta","AB");
states.put("American Samoa","AS");
states.put("Arizona","AZ");
states.put("Arkansas","AR");
states.put("Armed Forces (AE)","AE");
states.put("Armed Forces Americas","AA");
states.put("Armed Forces Pacific","AP");
states.put("British Columbia","BC");
states.put("California","CA");
states.put("Colorado","CO");
states.put("Connecticut","CT");
states.put("Delaware","DE");
states.put("District Of Columbia","DC");
states.put("Florida","FL");
states.put("Georgia","GA");
states.put("Guam","GU");
states.put("Hawaii","HI");
states.put("Idaho","ID");
states.put("Illinois","IL");
states.put("Indiana","IN");
states.put("Iowa","IA");
states.put("Kansas","KS");
states.put("Kentucky","KY");
states.put("Louisiana","LA");
states.put("Maine","ME");
states.put("Manitoba","MB");
states.put("Maryland","MD");
states.put("Massachusetts","MA");
states.put("Michigan","MI");
states.put("Minnesota","MN");
states.put("Mississippi","MS");
states.put("Missouri","MO");
states.put("Montana","MT");
states.put("Nebraska","NE");
states.put("Nevada","NV");
states.put("New Brunswick","NB");
states.put("New Hampshire","NH");
states.put("New Jersey","NJ");
states.put("New Mexico","NM");
states.put("New York","NY");
states.put("Newfoundland","NF");
states.put("North Carolina","NC");
states.put("North Dakota","ND");
states.put("Northwest Territories","NT");
states.put("Nova Scotia","NS");
states.put("Nunavut","NU");
states.put("Ohio","OH");
states.put("Oklahoma","OK");
states.put("Ontario","ON");
states.put("Oregon","OR");
states.put("Pennsylvania","PA");
states.put("Prince Edward Island","PE");
states.put("Puerto Rico","PR");
states.put("Quebec","QC");
states.put("Rhode Island","RI");
states.put("Saskatchewan","SK");
states.put("South Carolina","SC");
states.put("South Dakota","SD");
states.put("Tennessee","TN");
states.put("Texas","TX");
states.put("Utah","UT");
states.put("Vermont","VT");
states.put("Virgin Islands","VI");
states.put("Virginia","VA");
states.put("Washington","WA");
states.put("West Virginia","WV");
states.put("Wisconsin","WI");
states.put("Wyoming","WY");
states.put("Yukon Territory","YT");

Tags:

Java

Android