3

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:

enter image description here

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);
    }
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • 2
    What did you expect the result to be? My guess is that the font you're using doesn't support the Indian numbering system which would be used... – Jon Skeet Mar 16 '16 at 09:02
  • I don't know what I expected the result to be, that's why I create this program & want to see what kind of result would it produce. (But not question marks I'd expect) – Leem.fin Mar 16 '16 at 09:03

3 Answers3

4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

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.

Alexander
  • 1,356
  • 9
  • 16
2

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

Rahul
  • 81
  • 1
  • 3