Formatting dates in Java with DateFormat

By dave on March 22, 2015

There are several ways to format dates in Java, but by far the easiest is to use DateFormat. Creating a DateFormat is very similar to NumberFormat that we saw on the previous page. Here are the static factory methods called directly on the DateFormat class: getDateInstance(..) getDateTimeInstance(..) getTimeInstance(..) There are several overloaded versions of each method above. We will not cover Locale here as that will be covered in another article on java timezone support.

The background, string formatting in java

By dave on March 22, 2015

Over the years there have been no shortage of ways to format a string in java. What with the + operator, StringBuffer, StringBuilder, String.format(..) and various specialised formatters for numbers and dates we sometimes feel a little spoilt for choice. But how do they all work and what are their advantanges / disadvantages?  StringBuffer - a hang up from times gone by! StringBuffer is a synchronized object! Yes, everything you do with it will cause synchronization.

Formatting numbers using NumberFormat

By dave on July 1, 2012

java.text.NumberFormat is the class used to convert numeric values such as int, long and double into Strings. It has been around since the early days of Java, and generally performs well. Especially if you cache an instance of the class for repeated use. IMHO the only down side to using NumberFormat is that it can look a little verbose. To get hold of a copy of the NumberFormat class, call one of the static factory methods on the NumberFormat object:

tcMenu for Arduino

We use cookies to analyse traffic and to personalise content. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Please see our privacy policy should you need more information or wish to adjust your settings.