有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

是java中的toString()方法。util。日期与地区无关?

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

正如API告诉我的那样,java。util。日期toString()类似于格式“dow mon dd hh:mm:ss zzz yyyy”,我只想知道当windows区域设置更改时,toString()的内容格式是否会更改?如“俄语”、“英语”,建议输入相同,检查日期值是否相同?谢谢你的帮助


共 (4) 个答案

  1. # 1 楼答案

    通常toString()负责转换为字符串。本地系统上的区域设置更改是什么并不重要

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

    它会自动与您的区域设置一起工作。上述代码也可以正常工作

  2. # 2 楼答案

    Date.toString()与区域设置无关。您所能观察到的最重要的变化是时区,这取决于计算机的时区设置。这是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.
  3. # 3 楼答案

    它不会因其区域设置独立而更改。过去有toLocaleString(),但现在已经不推荐了

    你可以看到代码-

    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();
    }
    
  4. # 4 楼答案

    小心时区!除此之外,不,它不会根据区域设置而改变。检查源代码(这总是一个好主意!)对于java.util.Date.toString()方法:

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

    wtb是一个私有的最终静态字符串:

    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"
    };
    

    所以,不,它不会改变,它永远是英文名字

    更新:正如@nhahtdh指出的,时区将根据计算机区域配置的设置而变化