Question

    try {
         SimpleDateFormat strTemp = new SimpleDateFormat("ddMMMyy", Locale.US);
          Date reasult = strTemp.parse(input);
         String   checkDate = reasult.toString().substring(8, 10) + reasult.toString().substring(4, 7) + reasult.toString().substring(26);
         if (!checkDate.toUpperCase().equals(input))
             return false;

         else
          return true;
       } catch (Exception ex) {
            return false;
     }

As the API tells me java.util.Date.toString() is like format "dow mon dd hh:mm:ss zzz yyyy", I just want to know if the content format of toString() will be change while the windows locale changed? Such as "Russian","English", suggest the input is same, the checkDate value is same? Thanks for you help.

Was it helpful?

Solution

Date.toString() is locale independent. The most significant change that you can ever observe is the timezone, which depends on the timezone settings of your computer. This is by design.

Converts this Date object to a String of the form:

   dow mon dd hh:mm:ss zzz yyyy

where:

  • dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
  • mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
  • dd is the day of the month (01 through 31), as two decimal digits.
  • hh is the hour of the day (00 through 23), as two decimal digits.
  • mm is the minute within the hour (00 through 59), as two decimal digits.
  • ss is the second within the minute (00 through 61, as two decimal digits.
  • zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.
  • yyyy is the year, as four decimal digits.

OTHER TIPS

It will not change as its locale independent. There used to be toLocaleString() but its now deprecated.

You can see the code -

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();
}

Carefull with the time zone! Aside from that, no, it will not change according to the locale. Check the source code (that's always a good idea!) for the java.util.Date.toString() method:

 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(' ');            
    ...

And that wtb is a private final static string:

private final static String wtb[] = {
    "am", "pm",
    "monday", "tuesday", "wednesday", "thursday", "friday",
    "saturday", "sunday",
    "january", "february", "march", "april", "may", "june",
    "july", "august", "september", "october", "november", "december",
    "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
    "mst", "mdt", "pst", "pdt"
};

So, nope, it won't change, it'll always be English names.

UPDATE: as @nhahtdh pointed out, the Time Zone will change depending on where the computer regional config is set

Normally toString() is responsible for convert to String. It is not matter what is your locale changes on local system.

    //Now my local date time (US & CANADA)
    Date dt1=new Date();    
    System.out.println("Date is ::"+dt1.toString());

It works with your locale automatically.This above code is also working fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top