List of usage examples for org.joda.time DateTime getSecondOfMinute
public int getSecondOfMinute()
From source file:app.rappla.calendar.Date.java
License:Open Source License
/** * Constructor//from w ww . j ava 2s . co m * * @param icalStr * One or more lines of iCalendar that specifies a date * @param parseMode * PARSE_STRICT or PARSE_LOOSE */ public Date(String icalStr) throws ParseException, BogusDataException { super(icalStr); year = month = day = 0; hour = minute = second = 0; for (int i = 0; i < attributeList.size(); i++) { Attribute a = attributeAt(i); String aname = a.name.toUpperCase(Locale.ENGLISH); String aval = a.value.toUpperCase(Locale.ENGLISH); // TODO: not sure if any attributes are allowed here... // Look for VALUE=DATE or VALUE=DATE-TIME // DATE means untimed for the event if (aname.equals("VALUE")) { if (aval.equals("DATE")) { dateOnly = true; } else if (aval.equals("DATE-TIME")) { dateOnly = false; } } else if (aname.equals("TZID")) { tzid = a.value; } else { // TODO: anything else allowed here? } } String inDate = value; if (inDate.length() < 8) { // Invalid format throw new ParseException("Invalid date format '" + inDate + "'", inDate); } // Make sure all parts of the year are numeric. for (int i = 0; i < 8; i++) { char ch = inDate.charAt(i); if (ch < '0' || ch > '9') { throw new ParseException("Invalid date format '" + inDate + "'", inDate); } } year = Integer.parseInt(inDate.substring(0, 4)); month = Integer.parseInt(inDate.substring(4, 6)); day = Integer.parseInt(inDate.substring(6, 8)); if (day < 1 || day > 31 || month < 1 || month > 12) throw new BogusDataException("Invalid date '" + inDate + "'", inDate); // Make sure day of month is valid for specified month if (year % 4 == 0) { // leap year if (day > leapMonthDays[month - 1]) { throw new BogusDataException("Invalid day of month '" + inDate + "'", inDate); } } else { if (day > monthDays[month - 1]) { throw new BogusDataException("Invalid day of month '" + inDate + "'", inDate); } } // TODO: parse time, handle localtime, handle timezone if (inDate.length() > 8) { // TODO make sure dateOnly == false if (inDate.charAt(8) == 'T') { try { hour = Integer.parseInt(inDate.substring(9, 11)); minute = Integer.parseInt(inDate.substring(11, 13)); second = Integer.parseInt(inDate.substring(13, 15)); if (hour > 23 || minute > 59 || second > 59) { throw new BogusDataException("Invalid time in date string '" + inDate + "'", inDate); } if (inDate.length() > 15) { isUTC = inDate.charAt(15) == 'Z'; } } catch (NumberFormatException nef) { throw new BogusDataException("Invalid time in date string '" + inDate + "' - " + nef, inDate); } } else { // Invalid format throw new ParseException("Invalid date format '" + inDate + "'", inDate); } } else { // Just date, no time dateOnly = true; } if (isUTC && !dateOnly) { // Use Joda Time to convert UTC to localtime DateTime utcDateTime = new DateTime(DateTimeZone.UTC); utcDateTime = utcDateTime.withDate(year, month, day).withTime(hour, minute, second, 0); DateTime localDateTime = utcDateTime.withZone(DateTimeZone.getDefault()); year = localDateTime.getYear(); month = localDateTime.getMonthOfYear(); day = localDateTime.getDayOfMonth(); hour = localDateTime.getHourOfDay(); minute = localDateTime.getMinuteOfHour(); second = localDateTime.getSecondOfMinute(); } else if (tzid != null) { DateTimeZone tz = DateTimeZone.forID(tzid); if (tz != null) { // Convert to localtime DateTime utcDateTime = new DateTime(tz); utcDateTime = utcDateTime.withDate(year, month, day).withTime(hour, minute, second, 0); DateTime localDateTime = utcDateTime.withZone(DateTimeZone.getDefault()); year = localDateTime.getYear(); month = localDateTime.getMonthOfYear(); day = localDateTime.getDayOfMonth(); hour = localDateTime.getHourOfDay(); minute = localDateTime.getMinuteOfHour(); second = localDateTime.getSecondOfMinute(); // Since we have converted to localtime, remove the TZID // attribute this.removeNamedAttribute("TZID"); } } isUTC = false; // Add attribute that says date-only or date with time if (dateOnly) addAttribute("VALUE", "DATE"); else addAttribute("VALUE", "DATE-TIME"); }
From source file:ch.emad.business.schuetu.zeit.Countdown.java
License:Apache License
public int getSecondsPlus2() { final DateTime t = this.ablauf.minus(this.letzte.getMillis()); final int minutes = t.getMinuteOfHour(); final int seconds = t.getSecondOfMinute(); return (minutes * 60 * 1000) + (seconds * 1000) + 2000; }
From source file:cherry.goods.util.JodaTimeUtil.java
License:Apache License
/** * @param dtm ???{@link DateTime}//from w w w .j av a 2 s .c o m * @return ?????{@link Calendar}(?{@link DateTime}??)???????????? */ public static Calendar getCalendar(DateTime dtm) { Calendar cal = Calendar.getInstance(dtm.getZone().toTimeZone()); cal.set(dtm.getYear(), dtm.getMonthOfYear() - 1, dtm.getDayOfMonth(), dtm.getHourOfDay(), dtm.getMinuteOfHour(), dtm.getSecondOfMinute()); cal.set(MILLISECOND, dtm.getMillisOfSecond()); return cal; }
From source file:com.almende.eve.agent.google.GoogleCalendarAgent.java
License:Apache License
/** * Quick create an event./* www . jav a 2s . c o m*/ * * @param start * the start * @param end * the end * @param summary * the summary * @param location * the location * @param calendarId * the calendar id * @return the object node * @throws Exception * the exception */ public ObjectNode createEventQuick(@Optional @Name("start") String start, @Optional @Name("end") String end, @Optional @Name("summary") final String summary, @Optional @Name("location") final String location, @Optional @Name("calendarId") final String calendarId) throws Exception { final ObjectNode event = JOM.createObjectNode(); if (start == null) { // set start to current time, rounded to hours DateTime startDate = DateTime.now(); startDate = startDate.plusHours(1); startDate = startDate.minusMinutes(startDate.getMinuteOfHour()); startDate = startDate.minusSeconds(startDate.getSecondOfMinute()); startDate = startDate.minusMillis(startDate.getMillisOfSecond()); start = startDate.toString(); } final ObjectNode startObj = JOM.createObjectNode(); startObj.put("dateTime", start); event.put("start", startObj); if (end == null) { // set end to start +1 hour final DateTime startDate = new DateTime(start); final DateTime endDate = startDate.plusHours(1); end = endDate.toString(); } final ObjectNode endObj = JOM.createObjectNode(); endObj.put("dateTime", end); event.put("end", endObj); if (summary != null) { event.put("summary", summary); } if (location != null) { event.put("location", location); } return createEvent(event, calendarId); }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Get the timestamp rounded to the next half hour * // w ww .j a v a 2 s . co m * @return */ private DateTime getNextHalfHour() { DateTime next = DateTime.now(); next = next.minusMillis(next.getMillisOfSecond()); next = next.minusSeconds(next.getSecondOfMinute()); if (next.getMinuteOfHour() > 30) { next = next.minusMinutes(next.getMinuteOfHour()); next = next.plusMinutes(60); } else { next = next.minusMinutes(next.getMinuteOfHour()); next = next.plusMinutes(30); } return next; }
From source file:com.almende.pi5.common.agents.GraphAgent.java
License:Apache License
/** * Update time./*from ww w . j a v a 2 s . c om*/ */ @Access(AccessType.PUBLIC) public void updateTime() { DateTime now = DateTime.now(); now = now.plus((TIMESTEP - (now.getMinuteOfHour() % TIMESTEP)) * 60000 - (now.getSecondOfMinute() * 1000) - now.getMillisOfSecond()); if (!this.currentTimeslot.equals(now)) { this.currentTimeslot = now; LOG.fine(getId() + ": updateTime to: " + now); } }
From source file:com.barchart.feed.ddf.util.HelperDDF.java
License:BSD License
/** * from millisUTC into ddf "20100616124807". * //from w ww .java 2 s. c o m * @param millisUTC * the millis utc * @param zone * the zone * @return the long */ public static final long timeEncode(final long millisUTC, final DateTimeZone zone) { final DateTime dateTime = new DateTime(millisUTC, zone); long value = 0; final int year = dateTime.getYearOfEra(); value += year; value *= 100; final int month = dateTime.getMonthOfYear(); value += month; value *= 100; final int day = dateTime.getDayOfMonth(); value += day; value *= 100; final int hour = dateTime.getHourOfDay(); value += hour; value *= 100; final int minute = dateTime.getMinuteOfHour(); value += minute; value *= 100; final int second = dateTime.getSecondOfMinute(); value += second; return value; }
From source file:com.barchart.feed.test.replay.DDFLogDeframer.java
License:BSD License
static final void encodeTimeStamp(final String timestamp, final ByteBuffer buffer) { DateTime dateTime = timeParser.parseDateTime(timestamp); // base fields buffer.put(DDF_CENTURY); // century buffer.put(encodeTimeStampByte(dateTime.getYearOfCentury())); // year buffer.put(encodeTimeStampByte(dateTime.getMonthOfYear())); // month buffer.put(encodeTimeStampByte(dateTime.getDayOfMonth())); // day buffer.put(encodeTimeStampByte(dateTime.getHourOfDay())); // hours buffer.put(encodeTimeStampByte(dateTime.getMinuteOfHour())); // minutes buffer.put(encodeTimeStampByte(dateTime.getSecondOfMinute())); // seconds // milliseconds final int millisOfSecond = dateTime.getMillisOfSecond(); buffer.put((byte) (millisOfSecond & 0xFF)); // low byte buffer.put((byte) ((millisOfSecond >>> 8) & 0xFF)); // high byte }
From source file:com.clevercloud.bianca.lib.date.DateModule.java
License:Open Source License
/** * Returns the parsed date.//from w w w.j a va 2 s .co m */ public Value strptime(Env env, String date, String format) { ArrayValueImpl array = new ArrayValueImpl(); DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder(); int length = format.length(); for (int i = 0; i < length; i++) { char ch = format.charAt(i); if (ch != '%') { fb.appendLiteral(ch); continue; } switch (format.charAt(++i)) { case 'a': fb.appendDayOfWeekShortText(); break; case 'A': fb.appendDayOfWeekText(); break; case 'h': case 'b': fb.appendMonthOfYearShortText(); ; break; case 'B': fb.appendMonthOfYearText(); break; // TODO: case 'c' case 'C': fb.appendCenturyOfEra(2, 2); break; case 'd': fb.appendDayOfMonth(2); break; case 'D': fb.appendMonthOfYear(2); fb.appendLiteral('/'); fb.appendDayOfMonth(2); fb.appendLiteral('/'); fb.appendYear(2, 2); break; // TODO: case 'e' case 'F': fb.appendYear(4, 4); fb.appendLiteral('-'); fb.appendMonthOfYear(2); fb.appendLiteral('-'); fb.appendDayOfMonth(2); break; // TODO: case 'g' // TODO: case 'G' case 'H': fb.appendHourOfDay(2); break; case 'I': fb.appendHourOfHalfday(2); break; case 'j': fb.appendDayOfYear(3); break; // TODO: case 'l' case 'm': fb.appendMonthOfYear(2); break; case 'M': fb.appendMinuteOfHour(2); break; case 'n': fb.appendLiteral("\n"); break; case 'p': case 'P': fb.appendHalfdayOfDayText(); break; case 'r': fb.appendHourOfHalfday(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); fb.appendLiteral(':'); fb.appendSecondOfMinute(2); fb.appendLiteral(' '); fb.appendHalfdayOfDayText(); break; case 'R': fb.appendHourOfDay(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); break; // TODO: case 's' case 'S': fb.appendSecondOfMinute(2); break; case 't': fb.appendLiteral("\t"); break; case 'T': fb.appendHourOfDay(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); fb.appendLiteral(':'); fb.appendSecondOfMinute(2); break; // TODO: case 'u' // TODO: case 'U' // TODO: case 'V' // TODO: case 'w' // TODO: case 'W' // TODO: case 'x' // TODO: case 'X' case 'y': fb.appendYear(2, 2); break; case 'Y': fb.appendYear(4, 4); break; case 'z': fb.appendTimeZoneOffset(null, true, 2, 2); break; case 'Z': fb.appendTimeZoneName(); break; case '%': fb.appendLiteral('%'); break; default: fb.appendLiteral(ch); } } DateTimeFormatter dtf = fb.toFormatter().withLocale(Locale.getDefault()).withOffsetParsed(); org.joda.time.DateTime dt = new org.joda.time.DateTime(); String unparsed = ""; try { dt = dtf.parseDateTime(date); } catch (IllegalArgumentException e) { String delims = "[\"]+"; String[] splits = e.getMessage().split(delims); unparsed = unparsed.concat(splits[3]); } // According to manual strptime(3) if (dt.getCenturyOfEra() == 0) { if (dt.getYear() > 68) { dt = dt.withCenturyOfEra(19); } else { dt = dt.withCenturyOfEra(20); } } array.put("tm_sec", dt.getSecondOfMinute()); array.put("tm_min", dt.getMinuteOfHour()); array.put("tm_hour", dt.getHourOfDay()); array.put("tm_mday", dt.getDayOfMonth()); array.put("tm_mon", dt.getMonthOfYear() - 1); array.put("tm_year", dt.getYearOfCentury() + ((dt.getCenturyOfEra() - 19) * 100)); // Years since 1900 array.put("tm_wday", dt.getDayOfWeek() % 7); array.put("tm_yday", dt.getDayOfYear() - 1); array.put("unparsed", unparsed); return array; }
From source file:com.cloudhopper.commons.util.DateTimeUtil.java
License:Apache License
/** * Null-safe method that returns a new instance of a DateTime object rounded * downwards to the nearest second. The time zone of the returned DateTime * instance will be the same as the argument. Similar to a floor() function * on a float.<br>/* w w w . j a v a2s .co m*/ * Examples: * <ul> * <li>null -> null * <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:24:51.000 -8:00" * </ul> * @param value The DateTime value to round downward * @return Null if the argument is null or a new instance of the DateTime * value rounded downwards to the nearest second. */ public static DateTime floorToSecond(DateTime value) { if (value == null) { return null; } return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), value.getMinuteOfHour(), value.getSecondOfMinute(), 0, value.getZone()); }