Rollerball
Rollerball

Reputation: 13108

DateFormat: loses the locale settings after parsing

I see that I lose the locale specific setting while parsing a date with DateFormat..

 DateFormat date1 = DateFormat.getDateInstance(DateFormat.LONG,Locale.ITALY);// Long
    Date datE = date1.parse("13 settembre 2013", pp1);
    System.err.println("datE is: "+datE);

This is the output not localized that I get: date 34 is: Fri Sep 13 00:00:00 CEST 2013 is there any way to make it persistent? (would be logical expecting an Italian formatted date)

Upvotes: 0

Views: 50

Answers (2)

Eric Stein
Eric Stein

Reputation: 13672

Dates don't have a Locale. They're just a wrapper around a long - milliseconds since epoch.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

The Date class has an internal date format.

public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
    BaseCalendar.Date date = normalize();
    StringBuilder sb = new StringBuilder(28);
    int index = date.getDayOfWeek();
    if (index == gcal.SUNDAY) {
        index = 8;
    }
    convertToAbbr(sb, wtb[index]).append(' ');                        // EEE
    convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
    CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

    CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
    CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
    CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
    TimeZone zi = date.getZone();
    if (zi != null) {
        sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
    } else {
        sb.append("GMT");
    }
    sb.append(' ').append(date.getYear());  // yyyy
    return sb.toString();
}

Which DateFormat you parsed it with doesn't affect that.

The call

System.err.println("datE is: "+datE);

performs String concatenation which implicitly calls toString() on reference types, ie. datE.toString().

Upvotes: 1

Related Questions