"; */ ?>

Convert Date to String in Java


Just an example on how “java.text.SimpleDateFormat” can be used to convert a java date object (java.util.Date) to a string (String, StringBuffer, StringBuilder, etc..).
The whole magic is done by SimpleDateFormat, the child of “java.text.DateFormat”, which, as its name suggests, formats the date by a provided template.

This template is very flexible and provided to the formatter as a string. Here are the values which are used by SimpleDateFormat’s template:

Letter

Date or Time Component

Presentation

Examples

G

Era designator

Text

AD

y

Year

Year

1996; 96

M

Month in year

Month

July; Jul; 07

w

Week in year

Number

27

W

Week in month

Number

2

D

Day in year

Number

189

d

Day in month

Number

10

F

Day of week in month

Number

2

E

Day in week

Text

Tuesday; Tue

a

Am/pm marker

Text

PM

H

Hour in day (0-23)

Number

0

k

Hour in day (1-24)

Number

24

K

Hour in am/pm (0-11)

Number

0

h

Hour in am/pm (1-12)

Number

12

m

Minute in hour

Number

30

s

Second in minute

Number

55

S

Millisecond

Number

978

z

Time zone

General time zone

Pacific Standard Time; PST; GMT-08:00

Z

Time zone

RFC 822 time zone

-0800

Below, I wrote an example on how to implement the conversion. Here I used two different templates: “yyyyMMdd” and “MMddyyyy” to show that several letters from the table above can be used in different sequence for the desired format:

import java.util.Date;
import java.text.SimpleDateFormat;
... ... ... ...
 
    public void testConvertDateToString()    {
 
        //   Allocates a Date object and initializes it so that it represents the time
        // at which it was allocated, measured to the nearest millisecond.
        Date dateNow = new Date ();
 
        SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy");
 
        StringBuilder nowYYYYMMDD = new StringBuilder( dateformatYYYYMMDD.format( dateNow ) );
        StringBuilder nowMMDDYYYY = new StringBuilder( dateformatMMDDYYYY.format( dateNow ) );
 
        System.out.println( "DEBUG: Today in YYYYMMDD: '" + nowYYYYMMDD + "'");
        System.out.println( "DEBUG: Today in MMDDYYYY: '" + nowMMDDYYYY + "'");
 
    }

Results:

DEBUG: Today in YYYYMMDD: ‘20070609’
DEBUG: Today in MMDDYYYY: ‘06092007’

You may also notice that in the example I used StringBuilder, and not String or StringBuffer. The reason is simple – “best practice – use the right tool for the right task”.

“String” is immutable, meaning it cannot be changed, every time you try to change it, new String object is created and the old one is released for garbage collection, therefore String can be perfect for something like constants e.g. { private static final String FORTY_TWO = “42”; }.

StringBuilder and StringBuffer can be changed (modified), and, in fact, they have exactly the same functionality with one distinct difference – StringBuffer is synchronized and StringBuilder is not. Therefore if the segment of code we are working on is not designed to be multi threaded (used by different threads in a same time), it is better to use StringBuilder, since it will work faster.

StringBuilder is available in Java 1.5.0 (Java 2 SE 5.0) and up.

KISS – Keep It Simple Stupid ;)