Java date format is not compatible with xquery xs: date format, how to fix?

In java when using SimpleDateFormat with a template:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

date is displayed as:

"2002-02-01T18:18:42.703-0700"

In xquery, when using the xs: dateTime function, it throws an error:

"Invalid lexical value [err:FORG0001]"

with the specified date. For proper xquery analysis, the date should look like this:

"2002-02-01T18:18:42.703-07:00" - node the ':' 3rd position from end of string

which is based on ISO 8601, while the Java date is based on the RFC 822 standard.

I would like to be able to easily specify the timezone in Java so that it displays what xquery wants.

Thank!

+3
source share
4 answers

OK forum related DID help, thanks. However, I found a simpler solution, which I included below:

1) Use the Apache commons.lang java library
2) Use the following java code:

//NOTE: ZZ on end is not compatible with jdk, but allows for formatting  
//dates like so (note the : 3rd from last spot, which is iso8601 standard):  
//date=2008-10-03T10:29:40.046-04:00  
private static final String DATE_FORMAT_8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";  
DateFormatUtils.format(new Date(), DATE_FORMAT_8601)  
+4
source

, - ( ), - ISO, DateUtils ( apache commons lang) , !
. apache commons , ,

, JodaTime, , ISO8601 - :

public static void main(String[] args) {
    Date date = new Date();  
    DateTime dateTime = new DateTime(date);  
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();  
    String dateString = fmt.print(dateTime);  
    System.out.println("dateString=" + dateString);  
    DateTime dt = fmt.parseDateTime(dateString);  
    System.out.println("converted date=" + dt.toDate());  
} 
+1

Great find regarding commons.lang.java! You can even save yourself from creating your own format string by following these steps:

DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(new Date());
+1
source

Try the following:

static public String formatISO8601(Calendar cal) {
MessageFormat format = new MessageFormat("{0,time}{1,number,+00;-00}:{2,number,00}");

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(cal.getTimeZone());
format.setFormat(0, df);

long zoneOff = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET) / 60000L;
int zoneHrs = (int) (zoneOff / 60L);
int zoneMins = (int) (zoneOff % 60L);
if (zoneMins < 0)
    zoneMins = -zoneMins;

return (format.format(new Object[] { cal.getTime(), new Integer(zoneHrs), new Integer(zoneMins) }));
}
0
source

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


All Articles