简体   繁体   中英

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'

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 -

1393358368000 = Tue, 25 Feb 2014 19:59:28 GMT

Your time zone: 2/25/2014 9:59:28 PM GMT+2

The result from this code is 2/25/2014 21:59:28 PM

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 ?

It's not entirely clear what you're asking, but if all you want is to make your DateFormat use UTC, that's easy:

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?)

Also note that you're creating calendars for no reason at all. Your getFormattedValue and getOdataValue methods can be simplified to:

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$
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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