Parsing a SOAP Web Service Date from a String Returning d-1

I use org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDate(StringDate)to parse input dates from XML to JAVA date. But I have Date objects with a day-1 value.

for instance

<birth>1939-11-19+01:00</birth>

I want to ignore the offset. I only want a date, so no matter what the offset is, I have to get

Sun Nov 19 00:00:00 CET 1939

But I get:

Sat Nov 18 23:00:00 CET 1939

The problem is at the date offset level. I tried to fix this by ignoring the time offset, but that didn't work. I am using java 6 (draft constraint). Any ideas?

My best attempt at a minimal, complete, and verifiable example:

public static Date parse(final String str) {
    Calendar c = DatatypeConverter.parseDateTime(str);
    System.out.println(str + "\t" + c.getTime().getTime() / 1000);
    return c.getTime();
}
0
source share
1 answer

java.time

    String birth = "1939-11-19+01:00";
    LocalDate date = LocalDate.parse(birth, DateTimeFormatter.ISO_OFFSET_DATE);
    System.out.println(date);

Will print

1939-11-19

A java.time.LocalDate - , . java.util.Date . java.time, API Java, . - .

Date, , API, , , , . , . , , : parseDate parseDateTime 00:00 UTC . , , Date, , toString, . , 0:00 (CET, , , , , 0 +01 1939 ; , , /, /, /, / /, 0). :

    Date oldfashionedDateObject 
            = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
    System.out.println(oldfashionedDateObject);

Sun Nov 19 00:00:00 CET 1939

, America/New_York /, Sun Nov 19 00:00:00, , EST MSK.

Java 6 ThreeTen Backport

, Java 6. java.time Java 6 7 ThreeTen Backport. , , , :

    Date oldfashionedDateObject
            = DateTimeUtils.toDate(date.atStartOfDay(ZoneId.systemDefault()).toInstant());

ThreeTen Backport, , API, , . , .

, , , Date, DatatypeConverter Calendar:

    Calendar calWithParsedOffset = DatatypeConverter.parseDate(birth);
    Calendar calWithMyTimeZone = Calendar.getInstance(TimeZone.getDefault());
    calWithMyTimeZone.clear();
    calWithMyTimeZone.set(calWithParsedOffset.get(Calendar.YEAR),
            calWithParsedOffset.get(Calendar.MONTH),
            calWithParsedOffset.get(Calendar.DAY_OF_MONTH));
    Date oldfashionedDateObject = calWithMyTimeZone.getTime();
    System.out.println(oldfashionedDateObject);

Calendar, , , , , Date, . Calendar , Calendar. clear() , , 0. JVMs, America/New_York /.

+1

Source: https://habr.com/ru/post/1693352/


All Articles