Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DateFormat.getDateTimeInstance().format(date);

When running some tests I came across the following issue. When using:

private String printStandardDate(Date date) {
    return DateFormat.getDateTimeInstance(
        DateFormat.SHORT, DateFormat.SHORT).format(date);
}

I found this produced different formats of Date depending on the location the tests where run from. So locally in windows / eclipse I got a result: 04/02/12 18:18 but on the Linux box in America I get 2/4/12 6:18 PM

This causes my Tests/Build to fail:

expected:<[04/02/12 18:18]> but was:<[2/4/12 6:18 PM]>

Could anyone explain this behavior?

like image 671
Michael W Avatar asked Jul 03 '12 14:07

Michael W


People also ask

How will you format a date based on a locale after you have obtained a DateFormat object?

To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);

How do you change date format to MM DD YYYY in Java?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);

What is the format of new date () Java?

Creating A Simple Date Format String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.

What is the use of dateformat in Java?

Note: DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.

How do you format a date in Java?

Java Date Format. There are two classes for formatting date in java: DateFormat and SimpleDateFormat. The java.text.DateFormat class provides various methods to format and parse date and time in java in language independent manner. The DateFormat class is an abstract class.

Why is the getdatetimeinstance method overloaded?

The getDateTimeInstance method is overloaded to offer an alternative method that receives the locale that you want to use as parameter. If you use the same locale in both testing environments, the result should be the same. Thanks for contributing an answer to Stack Overflow!

What is the difference between formatting and parsing in date format?

In other words, formatting means date to string and parsing means string to date. converts given Date object into string. converts string into Date object. returns time formatter with default formatting style for the default locale.


1 Answers

That's not strange, that's exactly how it's supposed to work.

The API documentation of DateFormat.getDateTimeInstance says:

Gets the date/time formatter with the given date and time formatting styles for the default locale.

The default locale is different on your Windows system than on the Linux box in America.

If you want exact control over the date and time format, use SimpleDateFormat and specify the format yourself. For example:

private String printStandardDate(Date date) {
    return new SimpleDateFormat("dd/MM/yy HH:mm").format(date);
}

Even better would be to re-use the SimpleDateFormat object, but beware that it is not thread-safe (if the method might be called from multiple threads at the same time, things will get messed up if those threads use the same SimpleDateFormat object).

private static final DateFormat DATE_FORMAT =
    new SimpleDateFormat("dd/MM/yy HH:mm");

private String printStandardDate(Date date) {
    return DATE_FORMAT.format(date);
}
like image 158
Jesper Avatar answered Sep 23 '22 15:09

Jesper