Formatting Dates and Times depending of the location

Introduction

We already saw the DateFormat. This example shows how to set the format depending on the location.

// Format
Locale locale = Locale.ITALIAN;
Date date = new Date();

String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
// 22.33

s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(date);
// 22.33.03

s = DateFormat.getTimeInstance(DateFormat.LONG, locale).format(date);
// 22.33.03 PST

s = DateFormat.getTimeInstance(DateFormat.FULL, locale).format(date);
// 22.33.03 PST

s = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
// 22.33.03

// Parse
try {
    date = DateFormat.getTimeInstance(
        DateFormat.DEFAULT, locale).parse("22.33.03");
} catch (ParseException e) {
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.