Localising dates in Java using DateTimeFormatter


In this post we saw how to format dates according to some ISO and RCF standards. They can help you to quickly format a date in a standardised way. However, if you’re looking for date localisation then you’ll need something else.

By localising dates we mean that we want to show dates in an application according to the user’s region. A Japanese user will want to see the dates according to the Japanese date convention. You can store UTC dates internally according to an ISO standard but follow some local convention when presenting it on the screen.


A Locale represents a region and one or more corresponding cultures, most often with a country and one or more languages. You can easily list all available Locales:

Locale[] locales = Locale.getAvailableLocales();
        for (Locale locale : locales)

You’ll see values such as…

United Kingdom

Some locales are stored as static properties of the Locale object, e.g.:


We’ll need to use the ZonedDateTime object to format a date according to a Locale. The following code will format the UTC date according to the US standard:

ZonedDateTime utcDateZoned = ZonedDateTime.now(ZoneId.of("Etc/UTC"));
DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.US);

The output will be Friday, November 21, 2014 1:45:14 PM UTC.

Let’s see the UTC dates in France and Japan:

DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);

… vendredi 21 novembre 2014 13 h 50 UTC

DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.JAPAN);

2014年11月21日 13時51分34秒 UTC

View all posts related to Java here.


Replacing a value in a Map in Java

The Java 8 SDK has a couple of interesting new default “replace” methods available on the Map interface.

Consider the following HashMap:

Map<String, String> sizes = new HashMap<>();
sizes.put("XS", "Extra small");
sizes.put("S", "Small");
sizes.put("M", "Medium");
sizes.put("L", "Large");
sizes.put("XL", "Extra large");
sizes.put("XXL", "Extra extra large");

Say we’d like to replace the value of key “S”:

String replacedValue = sizes.replace("S", "Small size");

The replace method returns the value of the replaced string. In the above case the key “S” will have a new value “Small size” and “replace” returns “Small” as it was the value of “S” before the replace operation.

Read more of this post

Extract information about the current method in Java

Say you wish to get some simple information about the currently running function in your Java program. The stacktrace of the current thread can help you find that.

Here’s a simple snippet to print the class name, the file name, the line number and the method name:

Read more of this post

Create a List using Arrays.asList in Java

Java 8 has a number of new methods on Collections. One such utility method is the static asList method with which you can quickly create a List of T.

Here’s how it works for a List of integers:

List<Integer> asList = Arrays.asList(1,2,3,4);

…and for a List of strings:

List<String> asList = Arrays.asList("hello", "my", "dear", "world");

View all posts related to Java here.

Adjusting the date in Java Date and Time API


We saw a couple of new concepts in the Java 8 Date and Time API on this blog:

All the above classes expose methods called “with” with a couple of overloads. LocalDate, LocalTime and LocalDateTime come with other methods whose names start with “with”, such as withSeconds or withMonth depending on the supported level of time unit. The “with” methods adjust some value of the date-related instances and return a new instance.

Read more of this post

Insert a non-existent value into a Map in Java

Consider the following Employee class:

public class Employee
    private UUID id;
    private String name;
    private int age;

    public Employee(UUID id, String name, int age)
        this.id = id;
        this.name = name;
        this.age = age;
    public UUID getId()
        return id;

    public void setId(UUID id)
        this.id = id;

    public String getName()
        return name;

    public void setName(String name)
        this.name = name;
    public int getAge()
        return age;

    public void setAge(int age)
        this.age = age;

Let’s put some Employee objects into a hash map:

Read more of this post

Check available number of bytes in an input stream in Java

In this post we saw how to read the bytes contained in an input stream. The most common way to achieve it is by way of one of the read methods. The overloaded version where we provide a target byte array, an offset and a total byte count to be read is probably used most often.

It can happen in real-life situations that we provide the total number of bytes to be extracted but those bytes have not yet “arrived”, i.e. are not yet available in the input stream. This can occur when reading the bytes from a slow network connection. The bytes will eventually be available. The read method will block the thread it’s running in while it is waiting for the bytes to be loaded.

Read more of this post


A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: