Example usage for org.joda.time DateTime withDate

List of usage examples for org.joda.time DateTime withDate

Introduction

In this page you can find the example usage for org.joda.time DateTime withDate.

Prototype

public DateTime withDate(int year, int monthOfYear, int dayOfMonth) 

Source Link

Document

Returns a copy of this datetime with the specified date, retaining the time fields.

Usage

From source file:app.rappla.calendar.Date.java

License:Open Source License

/**
 * Constructor//  www .jav  a 2  s. c  o  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.model.schuetu.model.SpielEinstellungen.java

License:Apache License

public SpielEinstellungen() {
    DateTime date = new DateTime();
    date.withDate(2013, 6, 8);
    starttag = date.toDate();
}

From source file:dk.teachus.utils.DateUtils.java

License:Apache License

public static DateTime resetDateTime(DateTime time, DateTime resetTo) {
    DateTime timeDate = time;
    DateTime resetTimeDate = resetTo;//  w w w.ja v  a 2 s.  c  o m

    timeDate = timeDate
            .withDate(resetTimeDate.getYear(), resetTimeDate.getMonthOfYear(), resetTimeDate.getDayOfMonth())
            .withSecondOfMinute(0).withMillisOfSecond(0);

    return timeDate;
}

From source file:gobblin.util.TimeRangeChecker.java

License:Apache License

/**
 * Checks if a specified time is on a day that is specified the a given {@link List} of acceptable days, and that the
 * hours + minutes of the specified time fall into a range defined by startTimeStr and endTimeStr.
 *
 * @param days is a {@link List} of days, if the specified {@link DateTime} does not have a day that falls is in this
 * {@link List} then this method will return false.
 * @param startTimeStr defines the start range that the currentTime can fall into. This {@link String} should be of
 * the pattern defined by {@link #HOUR_MINUTE_FORMAT}.
 * @param endTimeStr defines the start range that the currentTime can fall into. This {@link String} should be of
 * the pattern defined by {@link #HOUR_MINUTE_FORMAT}.
 * @param currentTime is a {@link DateTime} for which this method will check if it is in the given {@link List} of
 * days and falls into the time range defined by startTimeStr and endTimeStr.
 *
 * @return true if the given time is in the defined range, false otherwise.
 *///from  w w w.j ava  2 s.  com
public static boolean isTimeInRange(List<String> days, String startTimeStr, String endTimeStr,
        DateTime currentTime) {

    if (!Iterables.any(days, new AreDaysEqual(DAYS_OF_WEEK.get(currentTime.getDayOfWeek())))) {
        return false;
    }

    DateTime startTime = null;
    DateTime endTime = null;

    try {
        startTime = HOUR_MINUTE_FORMATTER.withZone(DATE_TIME_ZONE).parseDateTime(startTimeStr);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "startTimeStr format is invalid, must be of format " + HOUR_MINUTE_FORMAT, e);
    }

    try {
        endTime = HOUR_MINUTE_FORMATTER.withZone(DATE_TIME_ZONE).parseDateTime(endTimeStr);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "endTimeStr format is invalid, must be of format " + HOUR_MINUTE_FORMAT, e);
    }

    startTime = startTime.withDate(currentTime.getYear(), currentTime.getMonthOfYear(),
            currentTime.getDayOfMonth());
    endTime = endTime.withDate(currentTime.getYear(), currentTime.getMonthOfYear(),
            currentTime.getDayOfMonth());

    Interval interval = new Interval(startTime.getMillis(), endTime.getMillis(), DATE_TIME_ZONE);
    return interval.contains(currentTime.getMillis());
}

From source file:org.apache.isis.applib.value.Time.java

License:Apache License

/**
 * Create a Time object set to the current time.
 *//* w  w w . ja va  2s .  c o m*/
public Time() {
    final DateTime dateTime = Clock.getTimeAsDateTime();
    time = dateTime.withDate(1970, 1, 1); // Epoch is 1970-01-01
}

From source file:org.mifosplatform.infrastructure.reportmailingjob.service.ReportMailingJobWritePlatformServiceImpl.java

License:Mozilla Public License

/**
 * create the next recurring DateTime from recurrence pattern, start DateTime and current DateTime
 * /*from   w  w  w.jav  a2s . c o  m*/
 * @param recurrencePattern
 * @param startDateTime
 * @return DateTime object
 */
private DateTime createNextRecurringDateTime(final ReportMailingJob reportMailingJob) {
    DateTime nextRecurringDateTime = null;
    final String recurrencePattern = reportMailingJob.getRecurrence();
    final DateTime nextRunDateTime = reportMailingJob.getNextRunDateTime();

    // the recurrence pattern/rule cannot be empty
    if (StringUtils.isNotBlank(recurrencePattern) && nextRunDateTime != null) {
        final LocalDate currentDate = DateUtils.getLocalDateOfTenant();
        final LocalDate nextRunDate = nextRunDateTime.toLocalDate();

        LocalDate nextRecurringLocalDate = CalendarUtils.getNextRecurringDate(recurrencePattern, nextRunDate,
                nextRunDate);

        if (nextRecurringLocalDate.isBefore(currentDate)) {
            nextRecurringLocalDate = CalendarUtils.getNextRecurringDate(recurrencePattern, nextRunDate,
                    currentDate);
        }

        // get the date fields
        final int currentYearIntValue = nextRecurringLocalDate.get(DateTimeFieldType.year());
        final int currentMonthIntValue = nextRecurringLocalDate.get(DateTimeFieldType.monthOfYear());
        final int currentDayIntValue = nextRecurringLocalDate.get(DateTimeFieldType.dayOfMonth());

        // append the time of the previous next run date time
        nextRecurringDateTime = nextRunDateTime.withDate(currentYearIntValue, currentMonthIntValue,
                currentDayIntValue);
    }

    return nextRecurringDateTime;
}