[英]issue with date and time zone how to get date without time zone?
我试图在Tics中的日期之间转换为UTC日期时间格式-'YYYY-MM-DDThh:mm:ss'
public static DateFormat getFormat() {
String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$
Locale locale = new Locale(systemLocale);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
return dateFormat;
}
public static Object getFormatedValue(Object value) {
String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(updated));
return getFormat().format(new Date(calendar.getTimeInMillis()));
}
public static Object getOdataValue(Object value) throws ParseException {
DateFormat dateFormat = getFormat();
Date parse = dateFormat.parse(value.toString());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(parse.getTime());
return "\"/Date(" + calendar.getTimeInMillis() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$
}
例如,我使用UTC获得dtae时间的结果的问题-
1393358368000 = 2014年2月25日,星期二19:59:28 GMT
您的时区:2014年2月25日9:59:28 GMT + 2
该代码的结果是2014/2/25下午21:59:28
没有时区的结果如何显示? 在这种情况下,我希望结果为ue,2014年2月25日格林尼治标准时间(GMT)在有和没有UTC的情况下,我能否打勾并得到不同的结果?
目前尚不清楚您要问什么,但是如果您只想让DateFormat
使用UTC,那很简单:
public static DateFormat getFormat() {
String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$
Locale locale = new Locale(systemLocale);
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
return dateFormat;
}
(顺便说一句,由于某种原因,您没有将Locale.getDefault()
用作默认语言环境?还是让DateFormat
自己选择?)
另请注意,您根本没有理由创建日历。 您的getFormattedValue
和getOdataValue
方法可以简化为:
public static Object getFormattedValue(Object value) {
String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
long millis = Long.parseLong(updated);
return getFormat().format(new Date(millis));
}
public static Object getOdataValue(Object value) throws ParseException {
DateFormat dateFormat = getFormat();
Date parsedDate = dateFormat.parse(value.toString());
return "\"/Date(" + date.getTime() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.