HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java – Get Current User Locale

Java – Get Current User Locale

If you are working in a java application and you are asked to make the application customized for international end users; then information present in simple tutorial will help you in achieving i18n (internationalization). I am parting the information into two: first for web application and second for a standalone java desktop application.

To customize your application for international audience, you will need to know the location of your user and then format certain display part of application based on this location information. Location information in java is represented by Locale class.

Locale information can be retrieved in two ways:

1) Get current locale in java web applications

In java web applications, locale information is retrieved from ServletRequest ( and HttpServletRequest ) object obtained in server side. Use below method call for getting the current locale information:

	Locale currentLocale = httpServletRequest.getLocale();
	
	System.out.println(currentLocale.getDisplayLanguage());
	System.out.println(currentLocale.getDisplayCountry());

	System.out.println(currentLocale.getLanguage());
	System.out.println(currentLocale.getCountry());
	
	//Output:
	
	English
	United States
	en
	US

2) Get current locale in java desktop applications

In java desktop applications, locale information is retrieved using Locale.getDefault(); method call. you can also use system properties "user.country" and "user.language" as well for this information.

Locale currentLocale = Locale.getDefault();

System.out.println(currentLocale.getDisplayLanguage());
System.out.println(currentLocale.getDisplayCountry());

System.out.println(currentLocale.getLanguage());
System.out.println(currentLocale.getCountry());

System.out.println(System.getProperty("user.country"));
System.out.println(System.getProperty("user.language"));

Output:

English
United States
en
US
US
en

That’s all regarding this simple, easy but important information regarding getting locale information in java.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. narendra pal

    July 12, 2018

    Hello Sir/Ma’am,
    My server in USA BUT MY USER hit my site all country so
    i know about when request received by client then i know about
    country detail its where country

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)