public static long dateInSeconds(String timeZoneId, int year, int month, int day, int hour, int minute, int second) { TimeZone timeZone = TimeZone.getTimeZone(timeZoneId); GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute, second); calendar.setTimeZone(timeZone); return calendar.getTimeInMillis() / 1000; }
protected XMLGregorianCalendar _gregorianFromDate(DeserializationContext ctxt, Date d) { if (d == null) { return null; } GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(d); TimeZone tz = ctxt.getTimeZone(); if (tz != null) { calendar.setTimeZone(tz); } return _dataTypeFactory.newXMLGregorianCalendar(calendar); } }
protected XMLGregorianCalendar _gregorianFromDate(DeserializationContext ctxt, Date d) { if (d == null) { return null; } GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(d); TimeZone tz = ctxt.getTimeZone(); if (tz != null) { calendar.setTimeZone(tz); } return _dataTypeFactory.newXMLGregorianCalendar(calendar); } }
private GregorianCalendar getNowUTC() { final GregorianCalendar now = new GregorianCalendar(); now.setTimeZone(TimeZone.getTimeZone("UTC")); now.setTime(new Date(clock.getTime())); return now; }
public GregorianCalendar get(TimeZone tz, Date date) { if (calendar == null) { calendar = new GregorianCalendar(tz, Locale.US); calendar.setGregorianChange(new Date(Long.MIN_VALUE)); // never use Julian calendar } else { // At least on Java 6, calendar.getTimeZone is slow due to a bug, so we need lastlySetTimeZone. if (lastlySetTimeZone != tz) { // Deliberately `!=` instead of `!<...>.equals()` calendar.setTimeZone(tz); lastlySetTimeZone = tz; } } calendar.setTime(date); return calendar; }
protected XMLGregorianCalendar _gregorianFromDate(DeserializationContext ctxt, Date d) { if (d == null) { return null; } GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(d); TimeZone tz = ctxt.getTimeZone(); if (tz != null) { calendar.setTimeZone(tz); } return _dataTypeFactory.newXMLGregorianCalendar(calendar); } }
/** * Returns the millisecond value of the specified date and time in GMT. * * @param year * the year, 0 is 1900. * @param month * the month, 0 - 11. * @param day * the day of the month, 1 - 31. * @param hour * the hour of day, 0 - 23. * @param minute * the minute of the hour, 0 - 59. * @param second * the second of the minute, 0 - 59. * @return the date and time in GMT in milliseconds. * * @deprecated Use code like this instead:<code> * Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); * cal.set(year + 1900, month, day, hour, minute, second); * cal.getTime().getTime();</code> */ @Deprecated public static long UTC(int year, int month, int day, int hour, int minute, int second) { GregorianCalendar cal = new GregorianCalendar(false); cal.setTimeZone(TimeZone.getTimeZone("GMT")); cal.set(1900 + year, month, day, hour, minute, second); return cal.getTimeInMillis(); }
public Date calculate(int era, int year, int month, int day, int hours, int minutes, int secs, int millisecs, boolean addOneDay, TimeZone tz) { if (calendar == null) { calendar = new GregorianCalendar(tz, Locale.US); calendar.setLenient(false); calendar.setGregorianChange(new Date(Long.MIN_VALUE)); // never use Julian calendar } else { // At least on Java 6, calendar.getTimeZone is slow due to a bug, so we need lastlySetTimeZone. if (lastlySetTimeZone != tz) { // Deliberately `!=` instead of `!<...>.equals()` calendar.setTimeZone(tz); lastlySetTimeZone = tz; } } calendar.set(Calendar.ERA, era); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hours); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.SECOND, secs); calendar.set(Calendar.MILLISECOND, millisecs); if (addOneDay) { calendar.add(Calendar.DAY_OF_MONTH, 1); } return calendar.getTime(); }
gc.setTimeZone(GMT); return gc.getTimeInMillis();
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { reader.moveDown(); long timeInMillis = Long.parseLong(reader.getValue()); reader.moveUp(); final String timeZone; if (reader.hasMoreChildren()) { reader.moveDown(); timeZone = reader.getValue(); reader.moveUp(); } else { // backward compatibility to XStream 1.1.2 and below timeZone = TimeZone.getDefault().getID(); } GregorianCalendar result = new GregorianCalendar(); result.setTimeZone(TimeZone.getTimeZone(timeZone)); result.setTime(new Date(timeInMillis)); // calendar.setTimeInMillis() not available under JDK 1.3 return result; }
public void testNegativeYearCompliance() throws Exception { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); // base assertion - 0001 is year 1 DateRange date = (DateRange) ((List) timeKvpParser.parse("01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.AD, cal.get(Calendar.ERA)); date = (DateRange) ((List) timeKvpParser.parse("00-06-01")).get(0); cal.setTime(date.getMinValue()); // calendar calls it year 1, ISO spec means it's year 0 // but we're just parsing here... assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // so, the next year should be 2 date = (DateRange) ((List) timeKvpParser.parse("-01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(2, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // now, big negative year compliance (see the spec, appendix D 2.2, pp 57-58) date = (DateRange) ((List) timeKvpParser.parse("-18000-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(18001, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); }
private static void adjustTimeZoneNicely(GregorianCalendar cal, TimeZone tz) { cal.setTimeZone(tz); int offset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / MILLIS_PER_MINUTE; cal.add(Calendar.MINUTE, -offset); }
GregorianCalendar gregCal = xmlGregCal.toGregorianCalendar(); if (xmlGregCal.getTimezone() == DatatypeConstants.FIELD_UNDEFINED) { gregCal.setTimeZone(tz);
@Test public void checkResultsParse() throws Exception { Message message = PacketParserUtils.parseStanza(exampleResultMessage); MamResultExtension mamResultExtension = MamResultExtension.from(message); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message forwardedMessage = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(forwardedMessage.getFrom().toString(), "witch@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getTo().toString(), "macbeth@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getBody(), "Hail to thee"); }
static GregorianCalendar newGreg() { GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH); retCal.setTimeZone(new SimpleTimeZone(0, "UTC")); retCal.setLenient(false); retCal.set(Calendar.MILLISECOND, 0); return retCal; }
@Test public void checkMamResultProvider() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(exampleMamResultXml); MamResultExtension mamResultExtension = new MamResultProvider().parse(parser); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message message = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(message.getFrom().toString(), "romeo@montague.lit/orchard"); Assert.assertEquals(message.getTo().toString(), "juliet@capulet.lit/balcony"); Assert.assertEquals(message.getBody(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); }
GregorianCalendar date = new GregorianCalendar(); date.clear(); //Use Gregorian calendar for all values date.setGregorianChange(new Date(Long.MIN_VALUE)); date.setTimeZone( TimeZone.getTimeZone("UTC")); date.setTime(new Date(-62135596800000L)); System.out.println( date.get(GregorianCalendar.YEAR) + "-" + (date.get(GregorianCalendar.MONTH) + 1) + "-" + date.get(GregorianCalendar.DAY_OF_YEAR) + " " + date.get(GregorianCalendar.HOUR_OF_DAY) + ":" + date.get(GregorianCalendar.MINUTE) + ":" + date.get(GregorianCalendar.SECOND) + "." + date.get(GregorianCalendar.MILLISECOND) ); //Prints 1-1-1 0:0:0.0
String control; GregorianCalendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime();
now.get(Calendar.DAY_OF_MONTH), 23,0); midnight.setTimeZone(zone); midnight.add(Calendar.HOUR,1); __rollover.scheduleAtFixedRate(_rollTask,midnight.getTime(),1000L*60*60*24);
@Test public void checkMamQueryResults() throws Exception { Message message = new Message(); message.setStanzaId("iasd207"); message.setFrom(JidCreate.from("coven@chat.shakespeare.lit")); message.setTo(JidCreate.from("hag66@shakespeare.lit/pda")); GregorianCalendar calendar = new GregorianCalendar(2002, 10 - 1, 13, 23, 58, 37); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); DelayInformation delay = new DelayInformation(date); Message forwardedMessage = new Message(); forwardedMessage.setFrom(JidCreate.from("coven@chat.shakespeare.lit/firstwitch")); forwardedMessage.setStanzaId("162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2"); forwardedMessage.setType(Type.chat); forwardedMessage.setBody("Thrice the brinded cat hath mew."); Forwarded forwarded = new Forwarded(delay, forwardedMessage); message.addExtension(new MamResultExtension("g27", "34482-21985-73620", forwarded)); // FIXME: The order of assertEquals is reversed, fix it by switching it. Assert.assertEquals(message.toXML(StreamOpen.CLIENT_NAMESPACE).toString(), mamQueryResultExample); MamResultExtension mamResultExtension = MamResultExtension.from(message); Assert.assertEquals(mamResultExtension.getId(), "34482-21985-73620"); Assert.assertEquals(mamResultExtension.getForwarded().getDelayInformation().getStamp(), date); Message resultMessage = (Message) mamResultExtension.getForwarded().getForwardedStanza(); Assert.assertEquals(resultMessage.getFrom(), JidCreate.from("coven@chat.shakespeare.lit/firstwitch")); Assert.assertEquals(resultMessage.getStanzaId(), "162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2"); Assert.assertEquals(resultMessage.getType(), Type.chat); Assert.assertEquals(resultMessage.getBody(), "Thrice the brinded cat hath mew."); }