date string is showing question marks

折月煮酒 提交于 2019-12-13 00:57:41

问题


I am using Eclipse 4.5.1 Mars. I have a very simple program which just use Hindi as locale and print out the date in a format:

But when run it, the console print out question marks. But if I remove the Hindi locale, it prints out correct date string. Why? How to fix the question mark problem?

====== CODE BELOW ========

public static void main(String[] args) {
        Locale.setDefault(new Locale("hi", "IN"));
        Calendar calendar = new GregorianCalendar(TimeZone.getDefault(),  Locale.getDefault());
        // print out date string in console
        System.out.println(getDateStr(calendar.getTime()));

    }

    public static String getDateStr(Date date) {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getDefault());
        return sdf.format(date);
    }

回答1:


It's just the Eclipse console not handling the Indian numbering system. When I run that same code on Linux in a shell, I get:

२०१६-०३-१६

As noted by Alexandar, changing the Eclipse console encoding to one which includes all the required characters fixes this - but it's unclear to me whether a format of yyyy-MM-dd is appropriate in that locale anyway. Usually that format is used for machine-readable dates, for which you should specify Locale.ROOT or Locale.US as the locale to use for formatting.




回答2:


You need to change the encoding for the console output of eclipse. By default it is Cp1252 (in my case), change it to UTF-8 which contains the Hindi characters.

Open your run configuration and go to Common tab. You'll find the Encoding settings there.




回答3:


In Eclipse default text file encoding is Cp1252, update that to UTF-8.
Go to preferences -> General -> Workspace update Text file encoding to UTF-8



来源:https://stackoverflow.com/questions/36030836/date-string-is-showing-question-marks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!