Java – Enum Example

public enum UserStatus {
    PENDING("P"), ACTIVE("A"), INACTIVE("I"), DELETED("D");

    private String statusCode;

    private UserStatus(String s) {
        statusCode = s;
    }

    public String getStatusCode() {
        return statusCode;
    }
}

public class EnumTest {

    public static void main(String[] args) {
        System.out.println(UserStatus.ACTIVE.getStatusCode());
    }

}

Java – Get Country List

The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166

import java.util.Locale;

public class ListCountry {

    public static void main(String[] args) {
        ListCountry obj = new ListCountry();
        obj.run();
    }

    public void run() {
        String[] locales = Locale.getISOCountries();

        for (String countryCode : locales) {
            Locale obj = new Locale("", countryCode);
            System.out.println("Country Code = " + obj.getCountry()
			+ ", Country Name = " + obj.getDisplayCountry());
        }

        System.out.println("Done");
    }
}

Output:

Country Code = AD, Country Name = Andorra
Country Code = AE, Country Name = United Arab Emirates
Country Code = AF, Country Name = Afghanistan
Country Code = AG, Country Name = Antigua and Barbuda
Country Code = AI, Country Name = Anguilla
Country Code = AL, Country Name = Albania
//skip ……

Java – Convert java.util.Date to JodaTime

java.util.Date date = new java.util.Date();

org.joda.time.DateTime dateTime = new org.joda.time.DateTime(date);

Make sure date isn’t null, though, otherwise it acts like new new DateTime()

Page 4 of 49« First...23456...102030...Last »