List of usage examples for org.joda.time DateTime withMonthOfYear
public DateTime withMonthOfYear(int monthOfYear)
From source file:org.jruby.CompatVersion.java
License:LGPL
protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) { Ruby runtime = recv.getRuntime(); DateTime dt = new DateTime(DateTimeZone.UTC); byte[] fromAsBytes = null; fromAsBytes = from.convertToString().getBytes(); if (fromAsBytes.length != 8) { throw runtime.newTypeError("marshaled time format differ"); }/* ww w . j ava2s. co m*/ int p = 0; int s = 0; for (int i = 0; i < 4; i++) { p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i); } for (int i = 4; i < 8; i++) { s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4)); } if ((p & (1 << 31)) == 0) { dt = dt.withMillis(p * 1000L + s); } else { p &= ~(1 << 31); dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900); dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1); dt = dt.withDayOfMonth(((p >>> 5) & 0x1F)); dt = dt.withHourOfDay((p & 0x1F)); dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F)); dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F)); // marsaling dumps usec, not msec dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000); dt = dt.withZone(getLocalTimeZone(runtime)); time.setUSec((s & 0xFFFFF) % 1000); } time.setDateTime(dt); return time; }
From source file:org.jruby.RubyTime.java
License:LGPL
protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) { Ruby runtime = recv.getRuntime();//from ww w .java 2s . c o m DateTime dt = new DateTime(DateTimeZone.UTC); byte[] fromAsBytes; fromAsBytes = from.convertToString().getBytes(); if (fromAsBytes.length != 8) { throw runtime.newTypeError("marshaled time format differ"); } int p = 0; int s = 0; for (int i = 0; i < 4; i++) { p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i); } for (int i = 4; i < 8; i++) { s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4)); } boolean utc = false; if ((p & (1 << 31)) == 0) { dt = dt.withMillis(p * 1000L); time.setUSec((s & 0xFFFFF) % 1000); } else { p &= ~(1 << 31); utc = ((p >>> 30 & 0x1) == 0x1); dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900); dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1); dt = dt.withDayOfMonth(((p >>> 5) & 0x1F)); dt = dt.withHourOfDay((p & 0x1F)); dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F)); dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F)); // marsaling dumps usec, not msec dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000); time.setUSec((s & 0xFFFFF) % 1000); } time.setDateTime(dt); if (!utc) time.localtime(); from.getInstanceVariables().copyInstanceVariablesInto(time); // pull out nanos, offset, zone IRubyObject nano_num = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_num"); IRubyObject nano_den = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_den"); IRubyObject offsetVar = (IRubyObject) from.getInternalVariables().getInternalVariable("offset"); IRubyObject zoneVar = (IRubyObject) from.getInternalVariables().getInternalVariable("zone"); if (nano_num != null && nano_den != null) { long nanos = nano_num.convertToInteger().getLongValue() / nano_den.convertToInteger().getLongValue(); time.nsec += nanos; } int offset = 0; if (offsetVar != null && offsetVar.respondsTo("to_int")) { IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! try { offset = offsetVar.convertToInteger().getIntValue() * 1000; } catch (RaiseException typeError) { runtime.getGlobalVariables().set("$!", oldExc); // Restore $! } } String zone = ""; if (zoneVar != null && zoneVar.respondsTo("to_str")) { IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! try { zone = zoneVar.convertToString().toString(); } catch (RaiseException typeError) { runtime.getGlobalVariables().set("$!", oldExc); // Restore $! } } time.dt = dt.withZone(getTimeZoneWithOffset(runtime, zone, offset)); return time; }
From source file:org.jvyamlb.SafeConstructorImpl.java
public static Object constructYamlTimestamp(final Constructor ctor, final Node node) { Matcher match = YMD_REGEXP.matcher(node.getValue().toString()); if (match.matches()) { final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0); if (year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); }//from w ww .java 2s . c om if (month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if (day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } return new Object[] { dt }; } match = TIMESTAMP_REGEXP.matcher(node.getValue().toString()); if (!match.matches()) { return new Object[] { ctor.constructPrivateType(node) }; } final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); final String hour_s = match.group(4); final String min_s = match.group(5); final String sec_s = match.group(6); final String fract_s = match.group(7); final String utc = match.group(8); final String timezoneh_s = match.group(9); final String timezonem_s = match.group(10); int usec = 0; if (fract_s != null) { usec = Integer.parseInt(fract_s); if (usec != 0) { while (10 * usec < 1000) { usec *= 10; } } } DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0); if ("Z".equalsIgnoreCase(utc)) { dt = dt.withZone(DateTimeZone.forID("Etc/GMT")); } else { if (timezoneh_s != null || timezonem_s != null) { int zone = 0; int sign = +1; if (timezoneh_s != null) { if (timezoneh_s.startsWith("-")) { sign = -1; } zone += Integer.parseInt(timezoneh_s.substring(1)) * 3600000; } if (timezonem_s != null) { zone += Integer.parseInt(timezonem_s) * 60000; } dt = dt.withZone(DateTimeZone.forOffsetMillis(sign * zone)); } } if (year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); } if (month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if (day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } if (hour_s != null) { dt = dt.withHourOfDay(Integer.parseInt(hour_s)); } if (min_s != null) { dt = dt.withMinuteOfHour(Integer.parseInt(min_s)); } if (sec_s != null) { dt = dt.withSecondOfMinute(Integer.parseInt(sec_s)); } dt = dt.withMillisOfSecond(usec / 1000); return new Object[] { dt, new Integer(usec % 1000) }; }
From source file:org.kemri.wellcome.dhisreport.api.utils.QuarterlyPeriod.java
License:Open Source License
/** * TODO: Probably more efficient ways to do this. But this is least cryptic * @param date //from w w w . j a v a 2s . com */ public QuarterlyPeriod(Date date) { DateTime dt = new DateTime(date); int monthNum = dt.getMonthOfYear(); if (monthNum >= 1 && monthNum <= 3) { startDate = dt.withMonthOfYear(DateTimeConstants.JANUARY).dayOfMonth().withMinimumValue().toDate(); endDate = dt.withMonthOfYear(DateTimeConstants.MARCH).dayOfMonth().withMaximumValue() .withTime(23, 59, 59, 999).toDate(); } else if (monthNum >= 4 && monthNum <= 6) { startDate = dt.withMonthOfYear(DateTimeConstants.APRIL).dayOfMonth().withMinimumValue().toDate(); endDate = dt.withMonthOfYear(DateTimeConstants.JUNE).dayOfMonth().withMaximumValue() .withTime(23, 59, 59, 999).toDate(); } else if (monthNum >= 7 && monthNum <= 9) { startDate = dt.withMonthOfYear(DateTimeConstants.JULY).dayOfMonth().withMinimumValue().toDate(); endDate = dt.withMonthOfYear(DateTimeConstants.SEPTEMBER).dayOfMonth().withMaximumValue() .withTime(23, 59, 59, 999).toDate(); } else if (monthNum >= 10 && monthNum <= 12) { startDate = dt.withMonthOfYear(DateTimeConstants.OCTOBER).dayOfMonth().withMinimumValue().toDate(); endDate = dt.withMonthOfYear(DateTimeConstants.DECEMBER).dayOfMonth().withMaximumValue() .withTime(23, 59, 59, 999).toDate(); } }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Gets the current hour, minute and second from a dublin core period if available. * * @param period/*w ww . jav a 2 s . c o m*/ * The current period from dublin core. * @return A new DateTime with the current hour, minute and second. */ static DateTime getCurrentStartDate(Opt<DCMIPeriod> period) { DateTime currentStartDate = new DateTime(); currentStartDate = currentStartDate.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withYear(2001); currentStartDate = currentStartDate.withMonthOfYear(1); currentStartDate = currentStartDate.withDayOfMonth(1); if (period.isSome() && period.get().hasStart()) { DateTime fromDC = new DateTime(period.get().getStart().getTime()); fromDC = fromDC.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withYear(fromDC.getYear()); currentStartDate = currentStartDate.withMonthOfYear(fromDC.getMonthOfYear()); currentStartDate = currentStartDate.withDayOfMonth(fromDC.getDayOfMonth()); } return currentStartDate; }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Gets the current hour, minute and second from a dublin core period if available. * * @param period/* www .j a v a2 s . c o m*/ * The current period from dublin core. * @return A new DateTime with the current hour, minute and second. */ private static DateTime getCurrentStartDateTime(Opt<DCMIPeriod> period) { DateTime currentStartTime = new DateTime(); currentStartTime = currentStartTime.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withYear(2001); currentStartTime = currentStartTime.withMonthOfYear(1); currentStartTime = currentStartTime.withDayOfMonth(1); currentStartTime = currentStartTime.withHourOfDay(0); currentStartTime = currentStartTime.withMinuteOfHour(0); currentStartTime = currentStartTime.withSecondOfMinute(0); if (period.isSome() && period.get().hasStart()) { DateTime fromDC = new DateTime(period.get().getStart().getTime()); fromDC = fromDC.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withYear(fromDC.getYear()); currentStartTime = currentStartTime.withMonthOfYear(fromDC.getMonthOfYear()); currentStartTime = currentStartTime.withDayOfMonth(fromDC.getDayOfMonth()); currentStartTime = currentStartTime.withHourOfDay(fromDC.getHourOfDay()); currentStartTime = currentStartTime.withMinuteOfHour(fromDC.getMinuteOfHour()); currentStartTime = currentStartTime.withSecondOfMinute(fromDC.getSecondOfMinute()); } return currentStartTime; }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Sets the start time in a dublin core catalog to the right value and keeps the start date and duration the same. * * @param dc/*from w ww . j a v a 2 s.c om*/ * The dublin core catalog to adjust * @param field * The metadata field that contains the start time. * @param ename * The EName in the catalog to identify the property that has the dublin core period. */ static void setTemporalStartTime(DublinCoreCatalog dc, MetadataField<?> field, EName ename) { if (field.getValue().isNone() || (field.getValue().get() instanceof String && StringUtils.isBlank(field.getValue().get().toString()))) { logger.debug("No value was set for metadata field with dublin core id '{}' and json id '{}'", field.getInputID(), field.getOutputID()); return; } try { // Get the current date SimpleDateFormat dateFormat = MetadataField.getSimpleDateFormatter(field.getPattern().get()); Date startDate = dateFormat.parse((String) field.getValue().get()); // Get the current period Opt<DCMIPeriod> period = getPeriodFromCatalog(dc, ename); // Get the current duration Long duration = getDuration(period); // Get the current start date DateTime currentStartDate = getCurrentStartDate(period); // Setup the new start time DateTime startDateTime = new DateTime(startDate.getTime()); startDateTime = startDateTime.withZone(DateTimeZone.UTC); startDateTime = startDateTime.withYear(currentStartDate.getYear()); startDateTime = startDateTime.withMonthOfYear(currentStartDate.getMonthOfYear()); startDateTime = startDateTime.withDayOfMonth(currentStartDate.getDayOfMonth()); // Get the current end date based on new date and duration. DateTime endDate = new DateTime(startDateTime.toDate().getTime() + duration); dc.set(ename, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDateTime.toDate(), endDate.toDate()), Precision.Second)); } catch (ParseException e) { logger.error("Not able to parse date {} to update the dublin core because: {}", field.getValue(), ExceptionUtils.getStackTrace(e)); } }
From source file:org.segrada.util.FlexibleDateParser.java
License:Apache License
/** * Parse input to Julian Day number// w w w. ja v a2 s. c o m * @param input string * @param type calendar type, e.g. "G" or "J" * @param high true to get last instance of parsed time, first otherwise * @return julian day number */ public Long inputToJd(@Nullable String input, String type, boolean high) { // sanity check if (input == null || "".equals(input)) return high ? Long.MAX_VALUE : Long.MIN_VALUE; try { DateTime date = inputFormatter.withChronology(getChronologyFromType(type)) .withLocale(Locale.getDefault()).withZoneUTC().parseDateTime(input); // get last time instance of the input if (high) { // guess input pattern by counting character occurences int count = Math.max(StringUtils.countMatches(input, "."), Math.max(StringUtils.countMatches(input, "/"), StringUtils.countMatches(input, "-"))); if (count == 0) // year only date = date.withMonthOfYear(12).withDayOfMonth(31); else if (count == 1) { // year/month date = date.withDayOfMonth(date.dayOfMonth().getMaximumValue()); } // day/month/year as is } return DateTimeUtils.toJulianDayNumber(date.getMillis()); } catch (Exception e) { logger.warn("Could not parse to DateTime: " + input + " (type = " + type + ")", e); } return null; }
From source file:ph.fingra.statisticsweb.common.util.DateTimeUtil.java
License:Apache License
public static String[] getDashboardFromToWithPrev(String numType) { int num = Integer.parseInt(numType.substring(0, numType.length() - 1)); String type = numType.substring(numType.length() - 1).toLowerCase(); final DateTime now = DateTime.now(); final DateTime to = now.minusDays(1); final DateTime from = type.equals("w") ? to.minusDays(num * 7 - 1) : (type.equals("m") ? to.minusMonths(num) : to.withMonthOfYear(1).withDayOfMonth(1)); final DateTime prevTo = type.equals("y") ? to.minusYears(1) : from.minusDays(1); final DateTime prevFrom = type.equals("w") ? prevTo.minusDays(num * 7 - 1) : (type.equals("m") ? prevTo.minusMonths(num) : prevTo.withMonthOfYear(1).withDayOfMonth(1)); final DateTime yesterday = now.minusDays(1); final DateTime beforeYesterday = now.minusDays(2); String nowTime = ""; String prevTime = ""; // before or after 10 minutes if (now.getMinuteOfHour() < 10) { // before 10 minutes String nowTemp = now.equals("0") ? "23" : String.valueOf(now.getHourOfDay() - 1); nowTime = (nowTemp.length() < 2) ? nowTemp = "0" + nowTemp : nowTemp; String prevTemp = nowTime.equals("00") ? "23" : String.valueOf(Integer.parseInt(nowTime) - 1); prevTime = (prevTemp.length() < 2) ? prevTemp = "0" + prevTemp : prevTemp; System.out.println("10 " + now.getMinuteOfHour() + ""); } else { // after 10 minutes nowTime = (String.valueOf(now.getHourOfDay()).length() < 2) ? "0" + String.valueOf(now.getHourOfDay()) : String.valueOf(now.getHourOfDay()); String prevTemp = now.equals("0") ? "23" : String.valueOf(now.getHourOfDay() - 1); prevTime = (prevTemp.length() < 2) ? prevTemp = "0" + prevTemp : prevTemp; System.out.println("10 " + now.getMinuteOfHour() + ""); }//from w w w .j a va 2 s . c o m return new String[] { from.toString("yyyy-MM-dd"), to.toString("yyyy-MM-dd"), prevFrom.toString("yyyy-MM-dd"), prevTo.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"), beforeYesterday.toString("yyyy-MM-dd"), now.toString("yyyy-MM-dd"), nowTime, prevTime }; }