Stream IO

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.

Using Java"s XMLStreamReader PULL XML Parser

By dave on February 17, 2014

In this entry I show how to use the inbuilt Java XMLStreamReader PULL parser class to read an XML file. The XML stream libraries are PULL based XML parsers that do not load the whole document into a memory structure, so therefore are more suited to large volumes of XML. Below is an example XML file for a zoo, it contains Animal data types that have both attributes and data. It is kept simple for the sake of example.

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:

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.