日期字符串显示问号 [英] date string is showing question marks

查看:99
本文介绍了日期字符串显示问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse 4.5.1火星.我有一个非常简单的程序,仅使用印地语作为区域设置,并以以下格式打印日期:

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

推荐答案

这只是Eclipse控制台未处理印度编号系统.当我在外壳中的Linux上运行相同的代码时,我得到:

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:

२०१६-०३-१६

如Alexandar所述,将Eclipse控制台编码更改为包含所有必需字符的编码可以解决此问题-但我不清楚yyyy-MM-dd的格式是否适合该语言环境.通常,该格式用于可机读的日期,为此,您应指定Locale.ROOTLocale.US作为用于设置格式的语言环境.

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.

这篇关于日期字符串显示问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆