List of usage examples for javax.xml.datatype XMLGregorianCalendar getTimezone
public abstract int getTimezone();
From source file:Main.java
/** * Converts an XMLGregorianCalendar to a Date. * * @param xmlDate//from www .j a v a 2s. c o m * XMLGregorianCalendar to convert. * @return corresponding date object. */ public static Date getDate(final XMLGregorianCalendar xmlDate) { // TODO: is this equivalent to getDate(String) processing above?? // start with UTC, i.e. no daylight savings time. TimeZone timezone = TimeZone.getTimeZone("GMT"); // adjust timezone to match xmldate int offsetMinutes = xmlDate.getTimezone(); if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) { timezone.setRawOffset( // convert minutes to milliseconds offsetMinutes * 60 // seconds per minute * 1000 // milliseconds per second ); } // use calendar so parsed date will be UTC Calendar calendar = Calendar.getInstance(timezone); calendar.clear(); calendar.set(xmlDate.getYear(), // xmlcalendar is 1 based, calender is 0 based xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(), xmlDate.getSecond()); Date date = calendar.getTime(); int millis = xmlDate.getMillisecond(); if (millis != DatatypeConstants.FIELD_UNDEFINED) { calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); } return date; }
From source file:com.microsoft.exchange.DateHelpTest.java
/** * Computes the time zone offset for a given {@link XMLGregorianCalendar} and compares to the specified {@link TimeZone} * //from ww w .j a v a 2 s.c o m * * * @param xmlGregorianCalendar * @param timeZone */ public boolean xmlGregorianCalendareMatchesTimeZone(XMLGregorianCalendar xmlGregorianCalendar, TimeZone timeZone) { int xmlTimeZoneOffsetMinutes = xmlGregorianCalendar.getTimezone(); TimeZone xmlTimeZone = xmlGregorianCalendar.getTimeZone(xmlTimeZoneOffsetMinutes); int jvmRawOffsetMinutes = (timeZone.getRawOffset() / 1000 / 60); int jvmDstOffsetMinutes = (timeZone.getDSTSavings() / 1000 / 60); int xmlRawOffsetMinutes = (xmlTimeZone.getRawOffset() / 1000 / 60); int xmlDstOffsetMinutes = (xmlTimeZone.getDSTSavings() / 1000 / 60); //XMLGregorianCalendar only stores an Int for offset, no DST information. //as a result the xmlTimeZone and jvmTimeZone almost never follow the same rules if (timeZone.hasSameRules(xmlTimeZone)) { log.debug("xmlTimeZoneId=" + xmlTimeZone.getID() + " hasSameRules as jvmTimeZone=" + timeZone.getID()); return true; } if (timeZone.useDaylightTime()) { //they definately do not when the jvmTimeZone uses DST. assertFalse(xmlTimeZone.hasSameRules(timeZone)); jvmRawOffsetMinutes += jvmDstOffsetMinutes; } if (xmlTimeZone.useDaylightTime()) { xmlRawOffsetMinutes += xmlDstOffsetMinutes; } if (xmlTimeZoneOffsetMinutes != xmlRawOffsetMinutes) { log.info("xmlTimeZoneId=" + xmlTimeZone.getID() + " has weird rules"); } return (jvmRawOffsetMinutes == xmlRawOffsetMinutes); }
From source file:com.microsoft.exchange.DateHelpTest.java
/** * Gets an {@link XMLGregorianCalendar} for the current date time using the default {@link TimeZone} * Test ensures the generated {@link XMLGregorianCalendar} has an offset which is equivalant to the default timezones rawOffSet + dstSavings * @throws DatatypeConfigurationException *//*from www. j av a 2s .c o m*/ @Test public void getXMLGregorianCalendarNow() throws DatatypeConfigurationException { XMLGregorianCalendar xmlGregorianCalendarNow = DateHelp.getXMLGregorianCalendarNow(); assertNotNull(xmlGregorianCalendarNow); int xmlTimeZoneOffsetMinutes = xmlGregorianCalendarNow.getTimezone(); TimeZone xmlTimeZone = xmlGregorianCalendarNow.getTimeZone(xmlTimeZoneOffsetMinutes); assertNotNull(xmlTimeZone); TimeZone jvmTimeZone = TimeZone.getDefault(); xmlGregorianCalendareMatchesTimeZone(xmlGregorianCalendarNow, jvmTimeZone); }
From source file:com.microsoft.exchange.integration.TimeZoneIntegrationTest.java
@Test public void createGetDeleteCalendarItem() throws DatatypeConfigurationException { TimeZone utcTimeZone = TimeZone.getTimeZone(RequestServerTimeZoneInterceptor.FALLBACK_TIMEZONE_ID); TimeZone.setDefault(utcTimeZone); ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:test-contexts/exchangeContext.xml"); RequestServerTimeZoneInterceptor timeZoneInterceptor = context .getBean(RequestServerTimeZoneInterceptor.class); ExchangeWebServices ews = context.getBean(ExchangeWebServices.class); BaseExchangeCalendarDataDao exchangeCalendarDao = context.getBean(BaseExchangeCalendarDataDao.class); exchangeCalendarDao.setWebServices(ews); assertEquals(TimeZone.getDefault(), utcTimeZone); //XMLGregorianCalendar is sortof backed by a gregorian calendar, date/times should reflect default jvm timezone XMLGregorianCalendar xmlStart = DateHelp.getXMLGregorianCalendarNow(); CalendarItemType calendarItem = new CalendarItemType(); calendarItem.setStart(xmlStart);//from ww w. j a v a 2s . c o m ItemIdType itemId = exchangeCalendarDao.createCalendarItem(upn, calendarItem); assertNotNull(itemId); Set<ItemIdType> itemIds = Collections.singleton(itemId); Set<CalendarItemType> calendarItems = exchangeCalendarDao.getCalendarItems(upn, itemIds); assertNotNull(calendarItems); CalendarItemType createdCalendarItem = DataAccessUtils.singleResult(calendarItems); assertNotNull(createdCalendarItem); XMLGregorianCalendar createdCalendarItemStart = createdCalendarItem.getStart(); assertNotNull(createdCalendarItemStart); assertEquals(xmlStart.getTimezone(), createdCalendarItemStart.getTimezone()); //nope! tzDisplayName = createdCalendarItem.getTimeZone() //assertEquals(RequestServerTimeZoneInterceptor.FALLBACK_TIMEZONE_ID, createdCalendarItem.getTimeZone()); assertEquals(xmlStart.getEon(), createdCalendarItemStart.getEon()); assertEquals(xmlStart.getEonAndYear(), createdCalendarItemStart.getEonAndYear()); assertEquals(xmlStart.getYear(), createdCalendarItemStart.getYear()); assertEquals(xmlStart.getMonth(), createdCalendarItemStart.getMonth()); assertEquals(xmlStart.getDay(), createdCalendarItemStart.getDay()); assertEquals(xmlStart.getHour(), createdCalendarItemStart.getHour()); assertEquals(xmlStart.getMinute(), createdCalendarItemStart.getMinute()); assertEquals(xmlStart.getSecond(), createdCalendarItemStart.getSecond()); //nope! always seems to be a slight variation //assertEquals(xmlStart.toGregorianCalendar().getTimeInMillis(), createdCalendarItemStart.toGregorianCalendar().getTimeInMillis()); //assertEquals(xmlStart.getMillisecond(), createdCalendarItemStart.getMillisecond()); //assertEquals(xmlStart.getFractionalSecond(), createdCalendarItemStart.getFractionalSecond()); assertTrue(DateHelp.withinOneSecond(xmlStart, createdCalendarItemStart)); assertTrue(exchangeCalendarDao.deleteCalendarItems(upn, itemIds)); }
From source file:com.microsoft.exchange.integration.TimeZoneIntegrationTest.java
@Test public void createGetDeleteCalendarItemBadTimeZone() throws DatatypeConfigurationException { ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:test-contexts/exchangeContext.xml"); RequestServerTimeZoneInterceptor timeZoneInterceptor = context .getBean(RequestServerTimeZoneInterceptor.class); ExchangeWebServices ews = context.getBean(ExchangeWebServices.class); BaseExchangeCalendarDataDao exchangeCalendarDao = context.getBean(BaseExchangeCalendarDataDao.class); exchangeCalendarDao.setWebServices(ews); //XMLGregorianCalendar is sortof backed by a gregorian calendar, date/times should reflect default jvm timezone XMLGregorianCalendar xmlStart = DateHelp .getXMLGregorianCalendarNow(java.util.TimeZone.getTimeZone("Pacific/Palau")); CalendarItemType calendarItem = new CalendarItemType(); calendarItem.setStart(xmlStart);//www . ja va 2 s.c om ItemIdType itemId = exchangeCalendarDao.createCalendarItem(upn, calendarItem); assertNotNull(itemId); Set<ItemIdType> itemIds = Collections.singleton(itemId); Set<CalendarItemType> calendarItems = exchangeCalendarDao.getCalendarItems(upn, itemIds); assertNotNull(calendarItems); CalendarItemType createdCalendarItem = DataAccessUtils.singleResult(calendarItems); assertNotNull(createdCalendarItem); XMLGregorianCalendar createdCalendarItemStart = createdCalendarItem.getStart(); assertNotNull(createdCalendarItemStart); //because the XMLGregorian calnedar was created with a time zone other than system default assertFalse(xmlStart.getTimezone() == createdCalendarItemStart.getTimezone()); assertTrue(DateHelp.withinOneSecond(xmlStart, createdCalendarItemStart)); assertTrue(exchangeCalendarDao.deleteCalendarItems(upn, itemIds)); }
From source file:org.openanzo.rdf.utils.StatementUtils.java
/** * Convert an xsd:dateTime Literal to its long representation. Note that xsd:dateTime allows the time zone to be omitted. However, to convert a literal to a * millisecond value, requires a time zone. That's because the millisecond long value is supposed to represent an absolute time. that is, milliseconds since * January 1, 1970, 00:00:00 GMT. Without a time zone in the xsd:dateTime literal, that number cannot be derived. * //from ww w . j ava2s . c o m * Note that this may cause information loss. xsd:dateTime support arbitrary precision fractional seconds. This method reduces the precision to * milliseconds. * * @param dateTime * xsd:dateTime Literal to convert * @return long representation of literal. null if the literal couldn't be parsed into a long such as for lack of a time zone being specified in the * literal. */ protected static Long convertToMilliseconds(TypedLiteral dateTime) { Long ret = null; Object nativeValue = dateTime.getNativeValue(); if (nativeValue instanceof XMLGregorianCalendar) { XMLGregorianCalendar xmlCal = (XMLGregorianCalendar) nativeValue; try { if (xmlCal.getXMLSchemaType().equals(DatatypeConstants.DATETIME) && xmlCal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) { ret = xmlCal.toGregorianCalendar().getTimeInMillis(); } } catch (IllegalStateException e) { // XMLGregorianCalendar#getXMLSchemaType() will throw an IllegalStateException if the XMLGregorianCalendar is invalid. log.debug(LogUtils.GLITTER_MARKER, "Error parsing dateTime literal into millisecond representation: {}", dateTime.toString()); } } return ret; }
From source file:org.ow2.aspirerfid.beg.capture.CaptureReport.java
private String EventTimeZoneOffset(XMLGregorianCalendar now) { // get the current time zone and set the eventTimeZoneOffset if (now != null) { int timezone = now.getTimezone(); int h = Math.abs(timezone / 60); int m = Math.abs(timezone % 60); DecimalFormat format = new DecimalFormat("00"); String sign = (timezone < 0) ? "-" : "+"; return sign + format.format(h) + ":" + format.format(m); }//from w ww. j a v a 2 s. c o m return null; }