Example usage for org.joda.time DateTime getYear

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

Introduction

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

Prototype

public int getYear() 

Source Link

Document

Get the year field value.

Usage

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.Ordinary.java

License:Open Source License

/**
 * This functions deals with special terms like today, tomorrow, and yesterday
 * @param start anchor time of the Phrase
 * @param phrase/*  www  .  j  a  v  a  2  s  .c o  m*/
  * @return
  */
public static TimexChunk Ordinaryrule(DateTime start, String phrase) {
    int numterm = 0;
    int i;
    int year;
    int month;
    int day;
    DateTime finish;
    String temp1;
    String temp2;
    Interval interval;
    interval = new Interval(start, start);
    phrase = phrase.toLowerCase();
    String patternStr = "(?:the)?\\s*(?:day)?\\s*(before|after|since)?\\s*(today|tomorrow|yesterday)\\s*";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(phrase);
    boolean matchFound = matcher.find();
    TimexChunk tc = new TimexChunk();
    tc.addAttribute(TimexNames.type, TimexNames.DATE);
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

    if (matchFound) {
        for (i = 1; i <= 2; i++) {
            numterm++;
            if (matcher.group(i) == null) {
                numterm--;
            }
        }

        if (numterm == 2) {

            temp1 = matcher.group(1);
            temp2 = matcher.group(2);
            if (temp1.equals("before")) {
                if (temp2.equals("today")) {
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(0, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(finish));
                    tc.addAttribute(TimexNames.mod, TimexNames.BEFORE);
                    return tc;
                }

                else if (temp2.equals("tomorrow")) {
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(0, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day + 1, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(finish));
                    tc.addAttribute(TimexNames.mod, TimexNames.BEFORE);
                    return tc;

                }

                else if (temp2.equals("yesterday")) {
                    start = start.minusDays(1);
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(0, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(finish));
                    tc.addAttribute(TimexNames.mod, TimexNames.BEFORE);
                    return tc;
                }
            }

            else if (temp1.equals("after")) {
                if (temp2.equals("today")) {
                    //start = start.minusDays(-1);
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    tc.addAttribute(TimexNames.mod, TimexNames.AFTER);
                    return tc;
                }

                else if (temp2.equals("tomorrow")) {
                    start = start.minusDays(-1);
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    tc.addAttribute(TimexNames.mod, TimexNames.AFTER);
                    return tc;

                }

                else if (temp2.equals("yesterday")) {
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day - 1, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    tc.addAttribute(TimexNames.mod, TimexNames.AFTER);
                    return tc;
                }
            }

            else if (temp1.equals("since")) {
                if (temp2.equals("yesterday")) {
                    start = start.minusDays(1);
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    return tc;
                }

                else if (temp2.equals("today")) {
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    return tc;
                }

                else if (temp2.equals("tomorrow")) {
                    start = start.minusDays(-1);
                    day = start.getDayOfMonth();
                    month = start.getMonthOfYear();
                    year = start.getYear();
                    start = new DateTime(year, month, day, 0, 0, 0, 0);
                    finish = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, fmt.print(start));
                    return tc;
                }
            }
        }

        else if (numterm == 1) {

            temp1 = matcher.group(2);
            if (temp1.equals("today")) {
                day = start.getDayOfMonth();
                month = start.getMonthOfYear();
                year = start.getYear();
                start = new DateTime(year, month, day, 0, 0, 0, 0);
                finish = new DateTime(year, month, day, 23, 59, 59, 59);
                tc.addAttribute(TimexNames.value, fmt.print(finish));
                return tc;

            }

            else if (temp1.equals("tomorrow")) {
                start = start.minusDays(-1);
                day = start.getDayOfMonth();
                month = start.getMonthOfYear();
                year = start.getYear();
                start = new DateTime(year, month, day, 0, 0, 0, 0);
                finish = new DateTime(year, month, day, 23, 59, 59, 59);
                tc.addAttribute(TimexNames.value, fmt.print(finish));
                return tc;
            }

            else if (temp1.equals("yesterday")) {
                start = start.minusDays(1);
                day = start.getDayOfMonth();
                month = start.getMonthOfYear();
                year = start.getYear();
                start = new DateTime(year, month, day, 0, 0, 0, 0);
                finish = new DateTime(year, month, day, 23, 59, 59, 59);
                tc.addAttribute(TimexNames.value, fmt.print(finish));
                return tc;
            }
        }
    }
    return null;
}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.Period.java

License:Open Source License

/**
 * This function deals with a period of time with format the xxth day/month, etc
 * @param start the anchor time// www. j  av a 2  s. c o m
 * @param temporalPhrase
  * @return
  */
public static TimexChunk Periodrule(DateTime start, TemporalPhrase temporalPhrase) {

    int year;
    DateTime finish;
    String temp1;
    String temp2;
    Interval interval;
    interval = new Interval(start, start);
    String phrase = temporalPhrase.getPhrase();
    phrase = phrase.toLowerCase();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

    int modiword = 0;// 0 :no modified words 1:early,ealier 2:late,later
    // Handle some special cases
    TimexChunk tc = new TimexChunk();
    tc.addAttribute(TimexNames.type, TimexNames.DATE);
    if (phrase.contains("now") || phrase.contains("currently") || phrase.contains("current")
            || phrase.contains("today")) {
        DateTime virtualStart = interval.getStart();
        virtualStart = new DateTime(virtualStart.getYear(), virtualStart.getMonthOfYear(),
                virtualStart.getDayOfMonth(), virtualStart.getHourOfDay(), virtualStart.getMinuteOfHour(),
                virtualStart.getSecondOfMinute(), virtualStart.getMillisOfSecond() + 1);
        tc.addAttribute(TimexNames.value, TimexNames.PRESENT_REF);
        return tc;
    }
    if (phrase.contains("early") || phrase.contains("earlier")) {
        modiword = 1;
    }
    if (phrase.contains("late") || phrase.contains("later")) {
        modiword = 2;
    }

    String units = "";

    for (String unitStr : dateUnitSet) {
        units = units + unitStr + "|";
    }
    units += "s$";

    String patternStr = "(?:the)?\\s*(\\d{1,4})(?:th|nd|st|rd)\\s*(" + units + ")\\s*";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(phrase);
    boolean matchFound = matcher.find();
    if (matchFound) {
        temp1 = matcher.group(1);
        temp2 = matcher.group(2);

        String residual = StringUtils.difference(matcher.group(0), phrase);
        String anchorStr = "";
        if (residual.length() > 0) {
            TemporalPhrase anchorPhrase = new TemporalPhrase(residual, temporalPhrase.getTense());
            TimexChunk anchorTimex = TimexNormalizer.normalize(anchorPhrase);
            if (anchorTimex != null) {
                anchorStr = anchorTimex.getAttribute(TimexNames.value);
            }
        }

        if (temp2.equals("century")) {
            year = (Integer.parseInt(temp1) - 1) * 100;
            start = new DateTime(year, 1, 1, 0, 0, 0, 0);
            finish = new DateTime(year + 99, 12, 31, 23, 59, 59, 59);
            tc.addAttribute(TimexNames.value, String.valueOf(finish.getCenturyOfEra()));
            return tc;
        }

        else if (temp2.equals("decade")) {
            // e.g.: 3rd decade (of this century)
            // first we get this century is 20, then the 3rd decade is 203
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 10 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("year")) {
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 100 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("quarter")) {
            int anchorYear = start.getYear();
            String val = String.valueOf(anchorYear) + "-Q" + temp1;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("month")) {
            int anchorYear = start.getYear();
            String monthStr = Integer.parseInt(temp1) < 10 ? "0" + temp1 : temp1;
            String val = String.valueOf(anchorYear) + "-" + monthStr;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("day")) {
            String val = "";
            if (anchorStr.length() > 0) {
                List<String> normTimexList = Period.normTimexToList(anchorStr);
                String anchorYear = normTimexList.get(0);
                String anchorDate;
                String anchorMonth;
                if (normTimexList.size() == 1 || Integer.parseInt(temp1) > 31) {
                    anchorMonth = "01";
                } else {
                    anchorMonth = normTimexList.get(1);
                }
                DateTime normDateTime = new DateTime(Integer.parseInt(anchorYear),
                        Integer.parseInt(anchorMonth), 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                anchorYear = String.valueOf(normDateTime.getYear());
                anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = anchorYear + "-" + anchorMonth + "-" + anchorDate;

            } else {
                int month = Integer.parseInt(temp1) > 31 ? 1 : start.getMonthOfYear();
                DateTime normDateTime = new DateTime(start.getYear(), month, 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                String anchorYear = String.valueOf(normDateTime.getYear());
                String anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                String anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = String.valueOf(anchorYear) + "-" + anchorMonth + "-" + anchorDate;
            }
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("s")) {

            if (Integer.parseInt(temp1) < 100) {
                year = start.getCenturyOfEra();
                year = year * 100 + Integer.parseInt(temp1);
                if (modiword == 0) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                }

                else if (modiword == 1) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }

                return tc;
            }

            else {
                if (modiword == 0) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                }

                else if (modiword == 1) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 3, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    start = new DateTime(Integer.parseInt(temp1) + 7, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }
                return tc;

            }
        }

    }
    return null;
}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.RelativeDate.java

License:Open Source License

/**
 * This function converts phrase that contains a relative date: more than xxx, etc
 * @param start anchor time//from w  w w .  java2s .c o m
 * @param phrase
  * @return
  */
public static TimexChunk Relativerule(DateTime start, String phrase) {
    int numterm;
    int flag_ago = 0;
    int i;
    int year;
    int month;
    int day;
    DateTime finish;
    String temp1;
    String temp2;
    String temp3;
    int amount;
    Interval interval;
    interval = new Interval(start, start);
    phrase = phrase.toLowerCase();
    phrase = phrase.trim();
    TimexChunk tc = new TimexChunk();

    if (phrase.equals("recently") || phrase.equals("recent") || phrase.equals("past")
            || phrase.equals("at time") || phrase.equals("previously")) {
        tc.addAttribute(TimexNames.type, TimexNames.DATE);
        tc.addAttribute(TimexNames.value, TimexNames.PAST_REF);
        return tc;
    }

    if (phrase.contains("future")) {
        tc.addAttribute(TimexNames.type, TimexNames.DATE);
        tc.addAttribute(TimexNames.value, TimexNames.FUTURE_REF);
        return tc;
    }

    // Handle some special cases
    if (phrase.endsWith("earlier")) {
        phrase = phrase.replace("earlier", "ago");
        phrase = phrase.trim();
    }

    if (phrase.contains("ago")) {
        phrase = phrase.replaceAll("ago", "");
        phrase = "last " + phrase;
        flag_ago = 1;
    }

    if (phrase.contains("later")) {
        phrase = phrase.replaceAll("later", "");
        phrase = "after " + phrase;
    }

    if (phrase.contains("after")) {
        phrase = phrase.replaceAll("after", "");
        phrase = "after " + phrase;
    }

    if (phrase.contains("more than")) {
        phrase = phrase.replaceAll("more than", "after");
    }

    if (phrase.contains("less than")) {
        phrase = phrase.replaceAll("less than", "in");
    }
    if (phrase.contains("last decade")) {
        phrase = phrase.replaceAll("last decade", "last ten years");
    }

    if (phrase.contains("next decade")) {
        phrase = phrase.replaceAll("next decade", "next ten years");

    }

    String patternStr = "\\s*(" + positionTerm + "|" + shiftIndicator + ")\\s*((?:\\d{1,4}|" + number
            + ")\\s*(?:" + number + ")?)\\s*(" + dateUnit + "|" + timeUnit + ")\\s*\\w*";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(phrase);
    boolean matchFound = matcher.find();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
    DateTimeFormatter ymFmt = DateTimeFormat.forPattern("yyyy-MM");
    DateTimeFormatter yFmt = DateTimeFormat.forPattern("yyyy");
    if (matchFound) {
        for (i = 1; i <= 3; i++) {
            if (matcher.group(i) == null) {
                i--;
                break;
            }
        }
        if (i == 4) {
            i--;
        }
        numterm = i;
        if (numterm == 3) {

            temp1 = matcher.group(1);
            temp2 = matcher.group(2);
            temp3 = matcher.group(3);
            amount = Integer.parseInt(converter(temp2));
            if (temp1.equals("last") || temp1.equals("past") || temp1.equals("previous")
                    || temp1.equals("since") || temp1.equals("this") || temp1.equals("recent")) {
                if (flag_ago == 0) {
                    if (temp3.equals("years") || temp3.equals("year")) {
                        finish = start.minusYears(amount);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.getDayOfMonth();
                        finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 0,
                                0, 0, 0);
                        start = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                        return tc;
                    }

                    else if (temp3.equals("day") || temp3.equals("days")) {
                        finish = start.minusDays(amount);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.getDayOfMonth();
                        finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 0,
                                0, 0, 0);
                        start = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "D");
                        return tc;

                    }

                    else if (temp3.equals("month") || temp3.equals("months")) {
                        finish = start.minusMonths(amount);
                        year = start.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                0, 0, 0, 0);
                        finish = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "M");
                        return tc;
                    }

                    else if (temp3.equals("week") || temp3.equals("weeks")) {
                        finish = start.minusWeeks(amount);
                        year = start.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                0, 0, 0, 0);
                        finish = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "W");
                        return tc;
                    }

                    else if (temp3.equals("decade") || temp3.equals("decades")) {

                        finish = start.minusYears(amount * 10);
                        year = start.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                0, 0, 0, 0);
                        finish = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "DE");
                        return tc;
                    }

                    else if (temp3.equals("century") || temp3.equals("centuries")) {
                        finish = start.minusYears(amount * 100);
                        year = start.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                0, 0, 0, 0);
                        finish = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "P" + amount + "CE");
                        return tc;
                    }

                    else if (temp3.equals("hour") || temp3.equals("hours")) {
                        finish = start.minusHours(amount);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "PT" + amount + "H");
                        return tc;
                    }

                    else if (temp3.equals("minute") || temp3.equals("minutes")) {
                        finish = start.minusMinutes(amount);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "PT" + amount + "M");
                        return tc;
                    }

                    else if (temp3.equals("second") || temp3.equals("seconds")) {
                        finish = start.minusSeconds(amount);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "PT" + amount + "S");
                        return tc;
                    }

                }

                else {
                    flag_ago = 0;
                    if (temp3.equals("years") || temp3.equals("year")) {
                        finish = start.minusYears(amount);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.getDayOfMonth();
                        start = new DateTime(year, 1, 1, 0, 0, 0, 0);
                        finish = new DateTime(year, 12, 31, 23, 59, 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value, yFmt.print(finish));
                        return tc;
                    }

                    else if (temp3.equals("day") || temp3.equals("days")) {
                        finish = start.minusDays(amount);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.getDayOfMonth();
                        finish = new DateTime(year, month, day, 23, 59, 59, 59);
                        start = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value, fmt.print(finish));
                        return tc;

                    }

                    else if (temp3.equals("month") || temp3.equals("months")) {
                        finish = start.minusMonths(amount);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.dayOfMonth().getMaximumValue();
                        start = new DateTime(year, month, 1, 0, 0, 0, 0);
                        finish = new DateTime(year, month, day, 23, 59, 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value, ymFmt.print(finish));
                        return tc;
                    }

                    else if (temp3.equals("week") || temp3.equals("weeks")) {
                        finish = start.minusWeeks(amount);
                        start = finish.minusWeeks(amount + 1);
                        year = start.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                23, 59, 59, 59);
                        start = new DateTime(year, month, day, 0, 0, 0, 0);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value,
                                yFmt.print(finish) + "-W" + String.valueOf(finish.getWeekOfWeekyear()));
                        return tc;
                    }

                    else if (temp3.equals("decade") || temp3.equals("decades")) {

                        finish = start.minusYears(amount * 10);
                        year = finish.getYear();
                        month = finish.getMonthOfYear();
                        day = finish.getDayOfMonth();
                        start = new DateTime(year, 1, 1, 0, 0, 0, 0);
                        finish = new DateTime(year, 12, 31, 23, 59, 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10) + "X");
                        return tc;
                    }

                    else if (temp3.equals("century") || temp3.equals("centuries")) {
                        finish = start.minusYears(amount * 100);
                        year = finish.getYear();
                        month = start.getMonthOfYear();
                        day = start.getDayOfMonth();
                        start = new DateTime(year, 1, 1, 0, 0, 0, 0);
                        finish = new DateTime(year, 12, 31, 23, 59, 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DATE);
                        tc.addAttribute(TimexNames.value, String.valueOf(finish.getCenturyOfEra()));
                        return tc;
                    }

                    else if (temp3.equals("hour") || temp3.equals("hours")) {
                        finish = start.minusHours(amount);
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                finish.getHourOfDay(), 0, 0, 0);
                        finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                finish.getHourOfDay(), 59, 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "PT" + amount + "H");
                        return tc;
                    }

                    else if (temp3.equals("minute") || temp3.equals("minutes")) {
                        finish = start.minusMinutes(amount);
                        start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                finish.getHourOfDay(), finish.minuteOfHour().get(), 0, 0);
                        finish = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(),
                                finish.getHourOfDay(), finish.minuteOfHour().get(), 59, 59);
                        tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                        tc.addAttribute(TimexNames.value, "PT" + amount + "M");
                        return tc;
                    }

                }

            }

            else if (temp1.equals("in") || temp1.equals("upcoming") || temp1.equals("next")
                    || temp1.equals("from") || temp1.equals("following") || temp1.equals("during")
                    || temp1.equals("additional")) {
                if (temp3.equals("years") || temp3.equals("year")) {
                    finish = start.minusYears((-1) * amount);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 23,
                            59, 59, 59);
                    start = new DateTime(year, month, day, 23, 59, 59, 59);
                    interval = new Interval(finish, start);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("day") || temp3.equals("days")) {
                    finish = start.minusDays((-1) * amount);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 23,
                            59, 59, 59);
                    start = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "D");
                    return tc;

                }

                else if (temp3.equals("month") || temp3.equals("months")) {
                    finish = start.minusMonths((-1) * amount);
                    year = start.getYear();
                    month = start.getMonthOfYear();
                    day = start.getDayOfMonth();
                    start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23,
                            59, 59, 59);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("week") || temp3.equals("weeks")) {
                    finish = start.minusWeeks((-1) * amount);
                    year = start.getYear();
                    month = start.getMonthOfYear();
                    day = start.getDayOfMonth();
                    start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23,
                            59, 59, 59);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "W");
                    return tc;
                }

                else if (temp3.equals("decade") || temp3.equals("decades")) {
                    finish = start.minusYears((-1) * amount * 10);
                    year = start.getYear();
                    month = start.getMonthOfYear();
                    day = start.getDayOfMonth();
                    start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23,
                            59, 59, 59);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    amount = amount * 10;
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("century") || temp3.equals("centuries")) {
                    finish = start.minusYears((-1) * amount * 100);
                    year = start.getYear();
                    month = start.getMonthOfYear();
                    day = start.getDayOfMonth();
                    start = new DateTime(finish.getYear(), finish.getMonthOfYear(), finish.getDayOfMonth(), 23,
                            59, 59, 59);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    amount *= 100;
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("hour") || temp3.equals("hours")) {
                    finish = start.minusHours((-1) * amount);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "H");
                    return tc;
                }

                else if (temp3.equals("minute") || temp3.equals("minutes")) {
                    finish = start.minusMinutes((-1) * amount);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("second") || temp3.equals("seconds")) {
                    finish = start.minusSeconds((-1) * amount);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "S");
                    return tc;
                }

            }

            else if (temp1.equals("before") || temp1.equals("prior to") || temp1.equals("preceding")) {
                if (temp3.equals("years") || temp3.equals("year")) {
                    finish = start.minusYears(amount);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("day") || temp3.equals("days")) {

                    finish = start.minusDays(amount);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "D");
                    return tc;

                }

                else if (temp3.equals("month") || temp3.equals("months")) {
                    finish = start.minusMonths(amount);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("week") || temp3.equals("weeks")) {
                    finish = start.minusWeeks(amount);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "W");
                    return tc;
                }

                else if (temp3.equals("decade") || temp3.equals("decades")) {
                    finish = start.minusYears(amount * 10);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    amount *= 10;
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("century") || temp3.equals("centuries")) {
                    finish = start.minusYears(amount * 100);
                    finish = finish.minusDays(1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(year, month, day, 23, 59, 59, 59);
                    amount *= 100;
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("hour") || temp3.equals("hours")) {
                    finish = start.minusHours(amount);
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    //interval = new Interval(start, finish);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "H");
                    return tc;
                }

                else if (temp3.equals("minute") || temp3.equals("minutes")) {
                    finish = start.minusMinutes(amount);
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("second") || temp3.equals("seconds")) {
                    finish = start.minusSeconds(amount);
                    start = new DateTime(0000, 1, 1, 0, 0, 0, 0);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "S");
                    return tc;
                }

            }

            else if (temp1.equals("after") || temp1.equals("over")) {
                if (temp3.equals("years") || temp3.equals("year")) {
                    finish = start.minusYears((-1) * amount);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("day") || temp3.equals("days")) {
                    finish = start.minusDays((-1) * amount);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "D");
                    return tc;

                }

                else if (temp3.equals("month") || temp3.equals("months")) {
                    finish = start.minusMonths((-1) * amount);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("week") || temp3.equals("weeks")) {
                    finish = start.minusWeeks((-1) * amount);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "W");
                    return tc;
                }

                else if (temp3.equals("decade") || temp3.equals("decades")) {
                    finish = start.minusYears((-1) * amount * 10);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    amount *= 10;
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("century") || temp3.equals("centuries")) {
                    finish = start.minusYears((-1) * amount * 100);
                    finish = finish.minusDays(-1);
                    year = finish.getYear();
                    month = finish.getMonthOfYear();
                    day = finish.getDayOfMonth();
                    finish = new DateTime(year, month, day, 0, 0, 0, 0);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    interval = new Interval(finish, start);
                    amount *= 100;
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "P" + amount + "Y");
                    return tc;
                }

                else if (temp3.equals("hour") || temp3.equals("hours")) {
                    finish = start.minusHours((-1) * amount);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    interval = new Interval(finish, start);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "H");
                    return tc;
                }

                else if (temp3.equals("minute") || temp3.equals("minutes")) {
                    finish = start.minusMinutes((-1) * amount);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    interval = new Interval(finish, start);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "M");
                    return tc;
                }

                else if (temp3.equals("second") || temp3.equals("seconds")) {
                    finish = start.minusSeconds((-1) * amount);
                    start = new DateTime(9999, 12, 31, 23, 59, 59, 59);
                    interval = new Interval(finish, start);
                    tc.addAttribute(TimexNames.type, TimexNames.DURATION);
                    tc.addAttribute(TimexNames.value, "PT" + amount + "S");
                    return tc;
                }

            }
        }
    }
    return null;
}

From source file:edu.nyu.vida.data_polygamy.feature_identification.IndexCreationReducer.java

License:BSD License

/**
 * Reduce function if merge tree for the dataset must be created and stored.
 *///from ww w  .j  a  v  a  2  s . co  m
public TopologicalIndex reduceMergeTreeCreation(AttributeResolutionWritable key,
        Iterable<SpatioTemporalFloatWritable> values, Context context, Attribute att, int tempRes,
        int spatialRes) throws IOException {

    //System.out.println("Dataset: " + key.getDataset());

    Iterator<SpatioTemporalFloatWritable> it = values.iterator();
    SpatioTemporalFloatWritable st;
    DateTime date;
    while (it.hasNext()) {
        st = it.next();

        int spatial = st.getSpatial();
        int temporal = st.getTemporal();
        float val = st.getValue();

        att.nodeSet.add(spatial);

        // for each temporal bin
        date = new DateTime(((long) temporal) * 1000, DateTimeZone.UTC);
        int hash = (tempRes == FrameworkUtils.HOUR) ? date.getYear() * 100 + date.getMonthOfYear()
                : ((tempRes == FrameworkUtils.DAY) ? date.getYear() * 100 + (date.getMonthOfYear() / 4) : 1);
        ArrayList<SpatioTemporalVal> temporalBinVals = att.data.get(hash);
        if (temporalBinVals == null)
            temporalBinVals = new ArrayList<SpatioTemporalVal>();

        // add val
        SpatioTemporalVal point = new SpatioTemporalVal(spatial, temporal, val);
        temporalBinVals.add(point);

        att.data.put(hash, temporalBinVals);
    }

    for (ArrayList<SpatioTemporalVal> stVal : att.data.values())
        Collections.sort(stVal);

    /*if ((spatialRes == FrameworkUtils.CITY) && (tempRes == FrameworkUtils.DAY)) {
    for (int spatial : att.nodeSet) {
        System.out.println("Attribute: " + key.getAttribute());
        //System.out.println("Spatial Attribute: " + spatial);
        for (int hash : att.data.keySet()) {
            ArrayList<String> data = new ArrayList<String>();
            for (SpatioTemporalVal val : att.data.get(hash)) {
                if (val.getSpatial() == spatial) {
                    data.add(val.getTemporal() + "," + val.getVal());
                }
            }
            if (data.size() > 0) {
                System.out.println("Month: " + hash);
                for (String d : data) {
                    System.out.println(d);
                }
            }
        }
    }
    System.out.println("");
    }*/

    TopologicalIndex index = (spatialRes == FrameworkUtils.NBHD)
            ? new TopologicalIndex(spatialRes, tempRes, this.nvNbhd)
            : ((spatialRes == FrameworkUtils.ZIP) ? new TopologicalIndex(spatialRes, tempRes, this.nvZip)
                    : new TopologicalIndex(spatialRes, tempRes, this.nvCity));
    int ret = (spatialRes == FrameworkUtils.NBHD) ? index.createIndex(att, this.nbhdEdges)
            : index.createIndex(att, this.zipEdges);

    // saving topological index
    /*outputStream = new ObjectOutputStream(FrameworkUtils.createFile(
        generateIndexFileName(idToDataset.get(key.getDataset()), key.getAttribute(),
                tempRes, spatialRes), context.getConfiguration(), s3));
    outputStream.writeObject(index);
    outputStream.writeObject(att);
    outputStream.close();*/

    if (ret == 1) {
        return new TopologicalIndex();
    }

    return index;
}

From source file:edu.unc.lib.deposit.normalize.Proquest2N3BagJob.java

License:Apache License

/**
 * Transform the given root element from the data document into MODS and stores it as the metadata for the object
 * being ingested//from  w  w w.  j a  v  a  2 s .  c om
 *
 * @param primaryPID
 * @param dataRoot
 * @param modified
 * @throws TransformerException
 * @throws FileNotFoundException
 * @throws IOException
 */
private Document extractMods(PID primaryPID, Element dataRoot, DateTime modified)
        throws TransformerException, FileNotFoundException, IOException {

    int month = modified.getMonthOfYear();
    String gradSemester;

    if (month >= 2 && month <= 6) {
        gradSemester = "Spring";
    } else if (month >= 7 && month <= 9) {
        gradSemester = "Summer";
    } else {
        gradSemester = "Winter";
    }

    JDOMResult mods = new JDOMResult();
    // Transform the metadata into MODS
    synchronized (proquest2ModsTransformer) {
        proquest2ModsTransformer.setParameter("graduationSemester", gradSemester + " " + modified.getYear());
        proquest2ModsTransformer.transform(new JDOMSource(dataRoot), mods);
    }

    // Create the description folder and write the MODS out to it
    final File modsFolder = getDescriptionDir();
    modsFolder.mkdir();

    File modsFile = new File(modsFolder, primaryPID.getUUID() + ".xml");

    try (FileOutputStream fos = new FileOutputStream(modsFile)) {
        new XMLOutputter(Format.getPrettyFormat()).output(mods.getDocument(), fos);
    }

    return mods.getDocument();
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.navigation.GoToPanel.java

License:Open Source License

/**
 * Parses the input to the goTo field to ensure proper formatting and handle
 * syntax errors//from  ww  w  .ja v a 2s .c  om
 * @param text string to parse
 */
public void parseGoto(String text) {

    DateTime dt;
    boolean isValidYear = true;

    try {
        dt = gotoField.parseDateTime(text);
        if (dt.getYear() < 1900 || dt.getYear() > 2100) {
            isValidYear = false;
            dt = null;
        }
    }

    catch (IllegalArgumentException illArg) {
        try {
            MutableDateTime mdt = gotoFieldShort.parseMutableDateTime(text);
            mdt.setYear(currentDate.getYear()); // this format does not provide years. add it
            dt = mdt.toDateTime();
        } catch (IllegalArgumentException varArg) {
            dt = null;
        }
    }
    if (dt != null) {
        MainPanel.getInstance().display(dt);
        MainPanel.getInstance().refreshView();
        gotoErrorText.setText(" ");
    } else {
        if (isValidYear)
            gotoErrorText.setText("* Use format: mm/dd/yyyy");
        else
            gotoErrorText.setText("* Year out of range (1900-2100)");
    }
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.navigation.MiniMonth.java

License:Open Source License

public MiniMonth(DateTime time, final MiniCalendarHostIface mc, boolean monthOnly) {
    this.setLayout(new GridLayout(7, 7));
    MutableDateTime prevMonth = new MutableDateTime(time);
    prevMonth.setDayOfMonth(1);//from   w  w w . ja va 2  s  . com
    prevMonth.addMonths(-1); // What is prevMonth for?
    String[] dayLabel = { "S", "M", "T", "W", "R", "F", "S" };

    MouseListener monthChanger = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            DayLabel d = (DayLabel) (me.getSource());
            if (!(d instanceof DescriptiveDayLabel)) {
                mc.display(d.getMonth());
            }
        }
    };

    MutableDateTime referenceDay = new MutableDateTime(time);
    // reset to the first of the month at midnight, then find Sunday
    referenceDay.setDayOfMonth(1);
    referenceDay.setMillisOfDay(0);
    int first = referenceDay.getDayOfWeek();
    referenceDay.addDays(-first);
    boolean flipFlop = false;

    // add day labels
    for (int i = 0; i < 7; i++) {
        DayLabel day = new DescriptiveDayLabel(dayLabel[i], time);
        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
    }

    // generate days, 6*7 covers all possible months, so we just loop
    // through and add each day
    for (int i = 0; i < (6 * 7); i++) {
        DayLabel day;
        if (monthOnly || MainPanel.getInstance().getView() == ViewSize.Month) {
            if (referenceDay.getDayOfMonth() == 1)
                flipFlop ^= true; // flops the flip flop flappity flip
        } else if (MainPanel.getInstance().getView() == ViewSize.Day)
            flipFlop = referenceDay.getDayOfYear() == time.getDayOfYear()
                    && referenceDay.getYear() == time.getYear();
        else if (MainPanel.getInstance().getView() == ViewSize.Week) {
            if (Months.getWeekStart(time).getMonthOfYear() == 12
                    && Months.getWeekStart(time).getDayOfMonth() >= 26) // Exception case for weeks between years
                flipFlop = time.getMonthOfYear() == 12 ? i >= 35 : i <= 6;
            else
                flipFlop = referenceDay.getDayOfYear() >= Months.getWeekStart(time).getDayOfYear()
                        && referenceDay.getDayOfYear() <= Months.getWeekStart(time).getDayOfYear() + 6;
        }

        if (flipFlop)
            day = new ActiveDayLabel(referenceDay.toDateTime());
        else
            day = new InactiveDayLabel(referenceDay.toDateTime());

        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
        referenceDay.addDays(1); // go to next day
    }
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.views.month.MonthItem.java

License:Open Source License

/**
 * Check if start date of event is before the specified day
 * @param currentDay day to compare event time with
 * @param eventStart event time/* w ww .  ja v  a2  s  . c o m*/
 * @return boolean determining whether the event start date is before current day
 */
private boolean isStartBeforeCurrent(DateTime currentDay, DateTime eventStart) {
    if (eventStart.getYear() < currentDay.getYear())//if the year is less than its always true
        return true;
    else if (eventStart.getYear() > currentDay.getYear())
        return false;
    else if (eventStart.getDayOfYear() < currentDay.getDayOfYear())//year is the same, so only day matters
        return true;
    return false;
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.views.month.MonthItem.java

License:Open Source License

/**
 * Check if end date of event is after the specified day
 * @param currentDay day to compare event time with
 * @param eventEnd event time/*from www  .j a v  a 2 s. c om*/
 * @return boolean determining whether the event end date is after current day
 */
private boolean isEndAfterCurrent(DateTime currentDay, DateTime eventEnd) {
    if (eventEnd.getYear() > currentDay.getYear())//if the year is less than its always true
        return true;
    else if (eventEnd.getYear() < currentDay.getYear())
        return false;
    else if (eventEnd.getDayOfYear() > currentDay.getDayOfYear())//year is the same, so only day matters
        return true;
    return false;
}

From source file:elw.web.FormatTool.java

License:Open Source License

public String format(final long dateLong, String pattern, String patternWeek, String patternToday) {
    final DateTimeFormatter formatter = lookupFormatter(pattern);
    final DateTimeFormatter formatterToday = lookupFormatter(patternToday);
    final DateTimeFormatter formatterWeek = lookupFormatter(patternWeek);

    final DateTime now = new DateTime(System.currentTimeMillis());
    final DateTime midnight = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0,
            0);//from w  w w. j  av a  2  s.co  m

    if (midnight.isBefore(dateLong) && midnight.plusDays(1).isAfter(dateLong)) {
        return formatterToday.print(dateLong);
    } else if (now.minusDays(7).isBefore(dateLong) && now.plusDays(7).isAfter(dateLong)) {
        return formatterWeek.print(dateLong);
    } else {
        return formatter.print(dateLong);
    }
}