Android DateFormatter 打印东部夏令时而不是 EDT
Android DateFormatter print Eastern Daylight Time instead of EDT
所以我试图打印字符串 "Eastern Daylight Time" 而不是 EDT 。这应该是动态的而不是硬编码的。查看 DateFormatter class 并没有让我找到有效的答案。
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)
输出模式应更改为 "M/d/yyyy hh:mm a zzzz"(注意 z 符号的数量以强制使用完整的区域名称)。根据日期和基础时区,格式化程序 SimpleDateFormat
将自动确定是使用夏令时还是标准名称。
案例2)
使用TimeZone.getDefault().getDisplayName(true, TimeZone.LONG)
强制使用长夏令时名称。如果您的默认时区是 "America/New_York",那么这样的表达式应该打印 "Eastern Daylight Time"。请注意,布尔参数已更改为 true
.
所以我试图打印字符串 "Eastern Daylight Time" 而不是 EDT 。这应该是动态的而不是硬编码的。查看 DateFormatter class 并没有让我找到有效的答案。
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)
输出模式应更改为 "M/d/yyyy hh:mm a zzzz"(注意 z 符号的数量以强制使用完整的区域名称)。根据日期和基础时区,格式化程序 SimpleDateFormat
将自动确定是使用夏令时还是标准名称。
案例2)
使用TimeZone.getDefault().getDisplayName(true, TimeZone.LONG)
强制使用长夏令时名称。如果您的默认时区是 "America/New_York",那么这样的表达式应该打印 "Eastern Daylight Time"。请注意,布尔参数已更改为 true
.