Android DateFormatter打印东部夏令时而不是EDT

时间:2016-10-21 15:09:32

标签: android date timezone date-format simpledateformat

所以我试图打印字符串" Eastern Daylight Time"而不是EDT。这应该是动态的而不是硬编码的。查看DateFormatter类并没有让我得到一个有效的答案。

Here was an example允许我格式化,但没有引导我得到我的具体答案。

我以下列格式获取日期 -

  

2013-06-08T00:00:00-04:00

以下是我尝试过的一些事情 -

1)

 String dateString = changeFormatDateStringWithDefaultTimeZone(paymentConfirmation.getTransactionDate(),
                "yyyy-MM-dd'T'HH:mm:ssZ",
                "M/d/yyyy hh:mm a zz");



 public static String changeFormatDateStringWithDefaultTimeZone(String value, String ip_format, String op_format) {
        if (value == null)
            return null;

        try {
            SimpleDateFormat opSDF = new SimpleDateFormat(op_format, Locale.US);
            opSDF.setTimeZone(TimeZone.getDefault());

            SimpleDateFormat inSDF = new SimpleDateFormat(ip_format, Locale.US);

            Date date = inSDF.parse(value);
            return(opSDF.format(date));

        } catch (Exception e) {
            Log.e("Err", "Failed to convert time "+value);
            e.printStackTrace();
        }
        return null;
    }

2)

 Date today = Calendar.getInstance().getTime();
 String todayString = DateUtils.convertDateToStringWithTimeZone(today);


 public static String convertDateToStringWithTimeZone(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String dateString = df.format(date);
        dateString += " " + TimeZone.getDefault().getDisplayName(false, TimeZone.LONG);
        return dateString;
    }

这些始终将时区打印为EDT,我想要字符串Eastern Daylight Time。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

好的,根据你上次编辑的问题,解决方案应该是这样的:

案例1)

输出模式应更改为" M / d / yyyy hh:mm a zzzz" (注意z符号的计数以强制执行完整的区域名称)。根据日期和基础时区,格式化程序SimpleDateFormat将自动确定是否使用日光或标准名称。

案例2)

使用TimeZone.getDefault().getDisplayName(true, TimeZone.LONG)强制执行长日光名称。如果你的默认时区是" America / New_York"那么这样的表达应该打印出东部夏令时"请注意,布尔参数已更改为true