【问题标题】:Getting time in Germany - Android在德国打发时间 - Android
【发布时间】:2013-04-16 21:26:20
【问题描述】:

无论用户的位置和当前设备时间如何,如何获取德国的当前时间?

    Calendar buttoncal = Calendar.getInstance();
    String tdate = new SimpleDateFormat("dd.MM",Locale.GERMANY).format(buttoncal.getTime());
    String tday  = new SimpleDateFormat("EEEE",Locale.GERMANY).format(buttoncal.getTime());


    one_date.setText(tdate.toString());
    one_day.setText(tday);

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以指定需要使用的时区:

    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
    

    查看可用时区列表:TimeZone.getAvailableIDs()

    注意:SimpleDateFormat 中的语言环境用于读取/格式化该语言环境中的字符串 - 例如,使用德语语言环境将以德语打印月份/日期名称。但这与时区无关。

    【讨论】:

    • 我通过了 TimeZone.getTimeZone("Europe/Berlin") 但它仍然给出当前设备时间而不是柏林/德国的当前时间。
    • 我通过了 TimeZone.getTimeZone("Europe/Berlin") 但它仍然给出当前设备时间而不是柏林/德国的当前时间。
    【解决方案2】:

    试试这个:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.GERMANY); 
    Date date = new Date();
    dateFormat.format(date);
    

    您将获得格式为“yyyy-MM-dd HH:mm”的“日期”。然后您可以使用 .split() 以空格作为分隔符来拆分返回的字符串格式。比如:

    String[] str_array = date.toString().split(" ");
    String time = str_array[1];
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        相关资源
        最近更新 更多