简体   繁体   English

日期和时区问题如何获取没有时区的日期?

[英]issue with date and time zone how to get date without time zone?

I tried to do convert between date in Tics to UTC date time format - 'YYYY-MM-DDThh:mm:ss' 我试图在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$

}

The problem that I got the result of dtae time with UTC for example - 例如,我使用UTC获得dtae时间的结果的问题-

1393358368000 = Tue, 25 Feb 2014 19:59:28 GMT 1393358368000 = 2014年2月25日,星期二19:59:28 GMT

Your time zone: 2/25/2014 9:59:28 PM GMT+2 您的时区:2014年2月25日9:59:28 GMT + 2

The result from this code is 2/25/2014 21:59:28 PM 该代码的结果是2014/2/25下午21:59:28

How I can get the result without time zone ? 没有时区的结果如何显示? in this case I want that the result will be ue, 25 Feb 2014 19:59:28 GMT Can I have tick and got different result with and without UTC ? 在这种情况下,我希望结果为ue,2014年2月25日格林尼治标准时间(GMT)在有和没有UTC的情况下,我能否打勾并得到不同的结果?

It's not entirely clear what you're asking, but if all you want is to make your DateFormat use UTC, that's easy: 目前尚不清楚您要问什么,但是如果您只想让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;
}

(Any reason you're not using Locale.getDefault() for the default locale, by the way? Or letting the DateFormat pick it itself?) (顺便说一句,由于某种原因,您没有将Locale.getDefault()用作默认语言环境?还是让DateFormat自己选择?)

Also note that you're creating calendars for no reason at all. 另请注意,您根本没有理由创建日历。 Your getFormattedValue and getOdataValue methods can be simplified to: 您的getFormattedValuegetOdataValue方法可以简化为:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM