简体   繁体   中英

date string is showing question marks

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM