Search code examples
javaeclipselocale

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:

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

Solution

  • 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.