Chapter 9. Handling Common Data Formats

Most of programming is handling data in various formats. In this chapter, we will introduce Java’s support for handling two big classes of data—text and numbers. The second half of the chapter will focus on handling date and time information. This is of particular interest as Java 8 ships a completely new API for handling date and time. We cover this new interface in some depth, before finishing the chapter by briefly discussing Java’s original date and time API.

Many applications are still using the legacy APIs, so developers need to be aware of the old way of doing things, but the new APIs are so much better that we recommend converting as soon as possible. Before we get to those more complex formats, let’s get underway by talking about textual data and strings.


We have already met Java’s strings on many occasions. They consist of sequences of Unicode characters, and are represented as instances of the String class. Strings are one of the most common types of data that Java programs process (a claim you can investigate for yourself by using the jmap tool that we’ll meet in Chapter 13).

In this section, we’ll meet the String class in some more depth, and understand why it is in a rather unique position within the Java language. Later in the section, we’ll introduce regular expressions, a very common abstraction for searching text for patterns (and a classic tool in the programmer’s arsenal).

Special Syntax for Strings

The String ...

Get Java in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.