Pi day by Skamradt d3bn1jb, Deviant Art.
Pi day by Skamradt d3bn1jb, Deviant Art.

March 14th, or should I say 3.14 is one of those fun days of the year where mathematicians can share private jokes. I am not categorizing myself as a mathematician, but I must admit I love maths and they fascinate me.

However, today is a special π day as it is in most of the world as we are 3/14/14 in the US and 14/3/14 in a lot of other countries around the world. An occasion to remind us about pi, this magimatical number. The number π is a mathematical constant, the ratio of a circle’s circumference to its diameter, approximately equal to 3.14159265358979323846264338327950 (ok, I must admit I only know by heart to 3.1415926).

Another good reason to remember about day formatters in Java. The easiest way to format a date is to use the SimpleDateFormat class. You simply specify a String pattern to format the day according to your needs. This is often a class I embed in my Java project:

public class DateUtils {

    public static String today() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("EEEEE, MMMMM d, ''yy");
        return sdf.format(date);
    }

    public static String now() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a (z)");
        return sdf.format(date);
    }
}

The syntax from pattern String is rather easy to define (you can check it fully on the Java website), and copied here:

Char Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
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 name in week Text Tuesday; Tue
u Day number of week (1 = Monday, …, 7 = Sunday) Number 1
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
X Time zone ISO 8601 time zone -08; -0800; -08:00

You use more or less characters to get more or less information:

  • “EEEEE, MMMMM d, ”yy” is Friday, March 14, ’14.
  • “h:mm a (z)” is 8:25 (EST).

Finally, remember that it will use your computer’s locale, so, if you are using a French local, you’ll get Vendredi, Mars 14, ’14. Which will sound weird to any French speaking person.