Pure Java – 63 Dates, Numbers & Currency – Locale

Merhaba Arkadaslar,
Bu yazimda java.util.Locale sinifindan bahsedecegim. Locale sinifi , DateFormat , SimpleDateFormat , NumberFormat siniflari ile kullanilabilir. Bununla birlikte bir cok yerde Locale objesini olusturup kod yazmamiz gerekebilir. Java API’sine gore Locale ‘spesifik cografi, politik veya kulturel bolgedir’.

Locale sinifi abstract degildir dolayisiyla new anahtar kelimesi ile yeni bir obje olusturabiliriz. Locale sinifinin yapilandiricilari overloaded’tir. Ornegin ;

Locale(String language)
Locale(String language, String country)

Kullanabilecegimiz localleri getAvailableLocales() metodu ile gorebiliriz;

import java.util.Locale;

public class GetAvailableLocalesTest {

	public static void main(String[] args) {
		Locale[] locales = Locale.getAvailableLocales();

		int i = 1;
		for (Locale loc : locales) {
			System.out.println(i + " " + loc);
			i++;
		}

	}
}

Locale objesi olusturalim ve bir kac metodu kullanalim ;

import java.util.Locale;

public class LocaleTest {

	public static void main(String[] args) {
		Locale locale = new Locale("TURKISH", "tr");

		System.out.println(locale);
		System.out.println("Country Code: " + locale.getCountry());
		System.out.println("Country Name" + locale.getDisplayCountry());
		System.out.println("Language :" + locale.getLanguage());
		System.out.println("Language:" + locale.getDisplayLanguage());
		// A three-letter abbreviation of this locale's country.
		System.out.println("3 harfli  " + locale.getISO3Country());

	}
}

Language ve Country alanlari birbiriyle uyusmak zorunda degil, yani country TR icin Italyanca dilini kullanabiliriz.

import java.util.Locale;

/*
 * public Locale(String language) {
 * this(language, "", "");
 * }
 */

/*

 public Locale(String language, String country) {
 this(language, country, "");
 }

 */
public class LocaleTest2 {

	public static void main(String[] args) {

		System.out.println("Language");
		Locale locTR = new Locale("tr");
		System.out.println(locTR.getDisplayCountry());// bos doner
		// country degeri verilmedi this ile cagrilan yapilandirici country
		// alanina"" degerini atamaktadir
		System.out.println(locTR.getDisplayLanguage());
		System.out.println(locTR.getDisplayLanguage(locTR));

		System.out.println("Language & Country");
		Locale locTR2 = new Locale("tr", "TR");
		System.out.println(locTR2.getDisplayCountry());
		System.out.println(locTR2.getDisplayCountry(locTR2));
		System.out.println(locTR2.getDisplayLanguage());
		System.out.println(locTR2.getDisplayLanguage(locTR2));

		System.out.println("it & TR");
		Locale locIT = new Locale("it", "TR");
		
		//Country karsiligi varsayilan dilde yani Ingilizce olarak goster.
		System.out.println(locIT.getDisplayCountry()); 
		
		//Country karsiligi, locIT deki dili kullanarak goster. Italyanca
		System.out.println(locIT.getDisplayCountry(locIT));
		
		//Language karsiligi ingilizce olarak goster.
		System.out.println(locIT.getDisplayLanguage());
		// Language karsiligi italyanca olarak goster.
		System.out.println(locIT.getDisplayLanguage(locIT));

	}
}

DateFormat ve SimpleDateFormat siniflariyla Locale sinifini beraber kullanabiliriz. SimpleDateFormat sinifinin Locale tipinde parametre alan yapilandiricisi vardir. Benzer sekilde DateFormat sinifinda getDateInstance metodunun Locale tipinde parametre alan overloaded hali vardir.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class LocaleTest3 {

	public static void main(String[] args) {

		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();

		Locale locIT = new Locale("it", "IT");

		DateFormat defaultUSDateFormat = DateFormat.getDateInstance(DateFormat.FULL);
		System.out.println(defaultUSDateFormat.format(date));

		DateFormat italianDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locIT);
		System.out.println(italianDateFormat.format(date));

		Locale locTR = new Locale("tr", "TR");
		DateFormat turkishDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locTR);
		System.out.println(turkishDateFormat.format(date));

		SimpleDateFormat sdf = new SimpleDateFormat("dd.MMM.yyy", locTR);
		System.out.println(sdf.format(date));
	}
}

ciktimiz ;

Saturday, November 1, 2014
sabato 1 novembre 2014
01 Kasım 2014 Cumartesi
01.Kas.2014

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *