Example usage for org.joda.time DateTime getWeekOfWeekyear

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

Introduction

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

Prototype

public int getWeekOfWeekyear() 

Source Link

Document

Get the week of weekyear field value.

Usage

From source file:kr.debop4j.timeperiod.tools.Times.java

License:Apache License

/**
 * ? ?? (Week)   true  false .//from   w  ww.j a  va 2  s  .c  o m
 *
 * @param left  the left
 * @param right the right
 * @return the boolean
 */
public static boolean isSameWeek(DateTime left, DateTime right) {
    return isSameYear(left, right) && left.getWeekOfWeekyear() == right.getWeekOfWeekyear();
}

From source file:kr.debop4j.timeperiod.tools.Times.java

License:Apache License

/**
 * Start time of week.//from ww  w.  j a v a  2 s  .  c  om
 *
 * @param year         the year
 * @param weekOfYear   the week of year
 * @param timeCalendar the time calendar
 * @return the date time
 */
public static DateTime startTimeOfWeek(int year, int weekOfYear, ITimeCalendar timeCalendar) {
    DateTime current = startTimeOfYear(year).minusWeeks(1);
    while (current.getYear() < year + 2) {
        if (current.getWeekyear() == year && current.getWeekOfWeekyear() == weekOfYear)
            break;
        current = current.plusDays(1);
    }
    return current;
}

From source file:kr.debop4j.timeperiod.tools.Weeks.java

License:Apache License

/**
 *  ???  .//w  ww.jav a2  s.c om
 *
 * @param moment    the moment
 * @param baseMonth the base month
 * @return the year and week
 */
public static YearAndWeek getYearAndWeek(DateTime moment, int baseMonth) {
    return new YearAndWeek(moment.getWeekyear(), moment.getWeekOfWeekyear());
}

From source file:kr.debop4j.timeperiod.YearAndWeek.java

License:Apache License

/**
 * Instantiates a new Year and week./*from  www  . j ava2s. c  om*/
 *
 * @param moment the moment
 */
public YearAndWeek(DateTime moment) {
    this.year = moment.getWeekyear();
    this.weekOfYear = moment.getWeekOfWeekyear();
}

From source file:net.rrm.ehour.util.DateUtil.java

License:Open Source License

/**
 * Get week number for date but compensate with configured first day of week.
 * Week officially starts on monday but when it's configured to start on sunday we have to compensate
 * because the day falls in the previous week.
 *//*  www. j  a  v a  2s  .c  o  m*/
public static int getWeekNumberForDate(Date date, int configuredFirstDayOfWeek) {
    DateTime dateTime = new DateTime(date);

    return configuredFirstDayOfWeek == Calendar.SUNDAY ? dateTime.plusDays(1).getWeekOfWeekyear()
            : dateTime.getWeekOfWeekyear();
}

From source file:net.sf.jasperreports.functions.standard.DateTimeFunctions.java

License:Open Source License

/**
 * Returns the week number of a given date.
 *///ww w.j  a v  a 2  s .  c  om
@Function("WEEKNUM")
@FunctionParameters({ @FunctionParameter("dateObj") })
public Integer WEEKNUM(Object dateObj) {
    Date date = convertDateObject(dateObj);
    if (date == null) {
        logCannotConvertToDate();
        return null;
    } else {
        DateTime dt = new DateTime(date);
        return dt.getWeekOfWeekyear();
    }
}

From source file:net.tourbook.ui.views.calendar.CalendarGraph.java

License:Open Source License

private void drawCalendar(GC gc) {

    final int dayLabelXOffset = 1;

    final int XX = getSize().x;
    final int YY = getSize().y;

    //      System.out.println(_graphClean ? "clean!" : "NOT clean!");
    //      System.out.println(_highlightChanged ? "HL changed!" : "HL NOT changed");
    //      System.out.println("-----------");

    _fontHeight = gc.getFontMetrics().getHeight();

    if (_graphClean && _image != null) {

        final GC oldGc = gc;
        _highlight = new Image(getDisplay(), XX, YY);
        gc = new GC(_highlight);
        gc.drawImage(_image, 0, 0);//from   ww w  .  j  a  v a2s  .  co  m

        drawSelection(gc);

        if (_highlightChanged) {
            drawHighLight(gc);
            _highlightChanged = false;
        }

        gc.dispose();
        oldGc.drawImage(_highlight, 0, 0);
        _highlight.dispose();
        return;
    }

    if (_scrollDebug) {
        System.out.println("Drawing year: " + _dt.getYear() + " week: " + _dt.getWeekOfWeekyear()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    if (_image != null && !_image.isDisposed()) {
        _image.dispose();
    }

    DateTime date = new DateTime(_dt);
    _image = new Image(getDisplay(), XX, YY);

    // update month/year dropdown box
    // look at the 1st day of the week after the first day displayed because if we go to
    // a specific month we ensure that the first day of the month is displayed in
    // the first line, meaning the first day in calendar normally contains a day
    // of the *previous* month
    if (_calendarYearMonthContributor.getSelectedYear() != date.plusDays(7).getYear()) {
        _calendarYearMonthContributor.selectYear(date.getYear());
    }
    if (_calendarYearMonthContributor.getSelectedMonth() != date.plusDays(7).getMonthOfYear()) {
        _calendarYearMonthContributor.selectMonth(date.getMonthOfYear());
    }

    final GC oldGc = gc;
    gc = new GC(_image);

    _refTextExtent = gc.stringExtent(_refText);
    final boolean oldLayout = _tinyLayout;
    _tinyLayout = (_refTextExtent.x > XX / 9); // getNumOfWeeks needs the _tinuLayout set

    if (oldLayout != _tinyLayout) { // the layout style changed, try to restore weeks and make selection visible
        if (_tinyLayout) {
            _dt_normal = _dt;
            _dt = _dt_tiny;
        } else {
            _dt_tiny = _dt;
            _dt = _dt_normal;
        }
        scrollBarUpdate();
        if (_selectedItem.id > 0) {
            switch (_selectedItem.type) {
            case DAY:
                gotoDate(new DateTime(0).plusDays(_selectedItem.id.intValue()));
                return;
            case TOUR:
                gotoTourId(_selectedItem.id);
                return;
            }
        }
    }

    final int numCols = 9; // one col left and right of the week + 7 week days
    final int numRows = getNumOfWeeks(); // number of weeks per month displayed (make sure _tinyLayout is already defined!)

    final Color alternate = _colorCache.getColor(0xf0f0f0);

    _tourFocus = new ArrayList<ObjectLocation>();
    _dayFocus = new ArrayList<ObjectLocation>();

    CalendarTourData[] data;

    final Font normalFont = gc.getFont();
    final FontData fd[] = normalFont.getFontData();
    fd[0].setStyle(SWT.BOLD);
    final Font boldFont = new Font(_display, fd[0]);

    final Rectangle area = getClientArea();
    gc.setBackground(_white);
    gc.setForeground(_black);
    gc.fillRectangle(area);

    final float dY = (float) YY / (float) numRows;
    float dX = (float) XX / (float) numCols;

    // keep the summary column at a minimal width and hide it completely if height goes blow usable value
    final int minSummaryWidth = _refTextExtent.x;
    final int minSummaryHeigth = (_refTextExtent.y * 2) / 3;
    int summaryWidth = 0;
    if (dY > minSummaryHeigth) {
        if (dX < minSummaryWidth) {
            summaryWidth = minSummaryWidth;
        } else {
            summaryWidth = (int) dX;
        }
    }

    final int minInfoWidth = _refTextExtent.x / 2;
    final int minInfoHeigth = (_refTextExtent.y / 3);
    int infoWidth = 0;
    if (dY > minInfoHeigth) {
        if (dX < minInfoWidth) {
            infoWidth = minInfoWidth;
        } else {
            infoWidth = (int) dX;
        }
    }

    dX = (float) (XX - summaryWidth - infoWidth) / (numCols - 2);

    _calendarAllDaysRectangle = new Rectangle(infoWidth, 0, (int) (7 * dX), YY);
    //      _calendarFirstWeekRectangle = new Rectangle(infoWidth, 0, (int) (7 * dX), (int) dY);
    //      _calendarLastWeekRectangle = new Rectangle( infoWidth, (int) ((getNumOfWeeks() - 1) * dY), (int) (7 * dX), (int) dY);

    // first draw the horizontal lines
    gc.setBackground(_white);
    gc.setForeground(_gray);
    for (int i = 0; i <= numRows; i++) {
        gc.drawLine(0, (int) (i * dY), XX, (int) (i * dY));
    }

    //      final Rectangle selectedRec = null;
    //      final CalendarTourData selectedTour = null;
    //      final boolean doSelection = false;

    final long todayDayId = (new Day(new DateTime())).dayId;

    gc.setFont(boldFont);
    // a rough guess about the max size of the label
    final Point[] headerSizes = { gc.stringExtent("22. May 99"), //$NON-NLS-1$
            gc.stringExtent("22. May"), //$NON-NLS-1$
            gc.stringExtent("22") }; //$NON-NLS-1$
    gc.setFont(normalFont);

    final String[] headerFormats = { "dd. MMM yy", "dd. MMM", "dd" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String headerFormat = UI.EMPTY_STRING;

    // Find a format for the day header which fits into the rectangle available;
    int g = 0;
    while (g < headerSizes.length && headerSizes[g].x > (dX - dayLabelXOffset)) {
        g++;
    }
    g = Math.min(g, headerSizes.length - 1); // if the cell is smaller than the shortest format (no index 'g' was found) we use the shortest format and relay on clipping
    // if (headerSizes[g].y < dY) {
    //    headerFormat = headerFormats[g];
    // }
    headerFormat = headerFormats[g];
    final int dayLabelWidht = headerSizes[g].x;
    int dayLabelHeight = headerSizes[g].y;

    // _tinyLayout = (refTextExtent.x > dX || refTextExtent.y > dY - dayLabelHeight) ? true : false;

    DateTime weekDate;
    long dayId = (new Day(date)).dayId; // we use simple ids

    // Weeks
    for (int i = 0; i < numRows; i++) {

        final int Y1 = (int) (i * dY);
        final int Y2 = (int) ((i + 1) * dY);

        // Days per week
        Rectangle dayRec = null;
        weekDate = date; // save the first day of this week as a pointer to this week

        if (infoWidth > 0) {
            final Rectangle infoRec = new Rectangle(0, Y1, infoWidth, (Y2 - Y1));
            drawWeekInfo(gc, date, infoRec);
        }

        for (int j = 0; j < 7; j++) {
            final int X1 = infoWidth + (int) (j * dX);
            final int X2 = infoWidth + (int) ((j + 1) * dX);
            //            final Rectangle dayRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            dayRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            final Day day = new Day(dayId);
            _dayFocus.add(new ObjectLocation(dayRec, dayId, day));
            dayId = day.dayId + 1;
            final int weekDay = date.getDayOfWeek();

            gc.setBackground(_white);

            // Day background rectangle
            if ((date.getMonthOfYear() % 2) == 1) {
                gc.setBackground(alternate);
                gc.fillRectangle(dayRec.x, dayRec.y + 1, dayRec.width, dayRec.height - 1);
            }

            data = _dataProvider.getCalendarDayData(date.getYear(), date.getMonthOfYear(),
                    date.getDayOfMonth());

            // Day header box
            if (!_tinyLayout) {
                gc.setForeground(_gray);
                gc.fillGradientRectangle(X1, Y1, dayRec.width + 1, dayLabelHeight, true); // no clue why I've to add 1 to the width, looks like a bug on Linux and does not hurt as we overwrite with the vertial line at the end anyway

                // Day header label
                gc.setFont(boldFont);
                if (day.dayId == todayDayId) {
                    gc.setForeground(_blue);
                } else if (weekDay == DateTimeConstants.SATURDAY || weekDay == DateTimeConstants.SUNDAY) {
                    gc.setForeground(_red);
                } else {
                    gc.setForeground(_darkGray);
                }
                gc.setClipping(X1, Y1, dayRec.width, dayLabelHeight); // this clipping should only kick in if shortest label format is still longer than the cell width
                gc.drawText(date.toString(headerFormat), X2 - dayLabelWidht - dayLabelXOffset, Y1, true);
                gc.setFont(normalFont);
                gc.setClipping(_nullRec);

            } else {
                dayLabelHeight = 0;
            }

            drawDayTours(gc, data, new Rectangle(dayRec.x, dayRec.y + dayLabelHeight, dayRec.width,
                    dayRec.height - dayLabelHeight));

            if (_tinyLayout && _showDayNumberInTinyView) {
                if (day.dayId == todayDayId) {
                    gc.setForeground(_blue);
                } else if (weekDay == DateTimeConstants.SATURDAY || weekDay == DateTimeConstants.SUNDAY) {
                    gc.setForeground(_red);
                } else {
                    gc.setForeground(_darkGray);
                }
                gc.setAlpha(0x50);
                gc.setFont(boldFont);
                gc.setClipping(dayRec);
                gc.drawText(date.toString(headerFormat), X2 - dayLabelWidht - dayLabelXOffset, Y1, true);
                gc.setFont(normalFont);
                gc.setClipping(_nullRec);
                gc.setAlpha(0xFF);
            }

            date = date.plusDays(1);
        }

        if (summaryWidth > 0) {
            final int X1 = infoWidth + (int) (7 * dX);
            final int X2 = X1 + summaryWidth;
            final Rectangle weekRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            final CalendarTourData weekSummary = _dataProvider.getCalendarWeekSummaryData(weekDate.getYear(),
                    weekDate.getWeekOfWeekyear());
            if (weekSummary.numTours > 0) {
                drawWeekSummary(gc, weekSummary, weekRec);
            }
        }

    }
    gc.setFont(normalFont);

    // and finally the vertical lines
    gc.setForeground(_display.getSystemColor(SWT.COLOR_GRAY));
    for (int i = 0; i <= 7; i++) {
        gc.drawLine(infoWidth + (int) (i * dX), 0, infoWidth + (int) (i * dX), YY);
    }

    // draw the selection on top of our calendar graph image so we can reuse that image
    _highlight = new Image(getDisplay(), XX, YY);
    gc = new GC(_highlight);
    gc.drawImage(_image, 0, 0);
    drawSelection(gc);
    oldGc.drawImage(_highlight, 0, 0);
    _highlight.dispose();

    boldFont.dispose();
    oldGc.dispose();
    gc.dispose();

    _graphClean = true;

}

From source file:net.tourbook.ui.views.calendar.CalendarGraph.java

License:Open Source License

private void drawWeekInfo(final GC gc, final DateTime dt, final Rectangle rec) {

    final Font normalFont = gc.getFont();
    final FontData fd[] = normalFont.getFontData();
    fd[0].setStyle(SWT.BOLD);/*  w ww .jav  a 2 s.co m*/
    // fd[0].setHeight(((rec.height) * 72 / _display.getDPI().y) / 4);
    final Font boldFont = new Font(_display, fd[0]);

    gc.setForeground(_darkGray);
    gc.setBackground(_white);

    gc.setFont(boldFont);
    String text;
    if (_tinyLayout) {
        if (dt.minusDays(1).getMonthOfYear() != dt.plusDays(6).getMonthOfYear()) { // a new month started on this week
            gc.setClipping(new Rectangle(rec.x, rec.y, rec.width, 4 * rec.height)); // clipp to the room left of this month
            text = dt.plusDays(6).toString("MMM"); //$NON-NLS-1$
            if (rec.width < (2 * _refTextExtent.x / 3)) {
                text = text.substring(0, 1);
            }
            gc.drawText(text, rec.x + 2, rec.y + 2);
            gc.setClipping(_nullRec);
        }
    } else {
        gc.drawText("" + dt.getWeekOfWeekyear(), rec.x + 4, rec.y + 2);//$NON-NLS-1$
    }
    gc.setFont(normalFont);

    boldFont.dispose();

}

From source file:netload.controller.StatsController.java

License:Open Source License

public ArrayList<Week> getTotalWeeks() {
    ArrayList<Day> days = (ArrayList<Day>) stats.getDays(49);
    Collections.sort(days);/*w w w  .  jav  a 2 s  .c om*/

    ArrayList<Week> weeks = new ArrayList<>();
    int weekNumber = new DateTime(days.get(0).getDatum()).getWeekOfWeekyear();
    double total = 0, up = 0, down = 0;

    for (Day day : days) {
        DateTime dayTime = new DateTime(day.getDatum());
        if (dayTime.getWeekOfWeekyear() == weekNumber) {
            total += day.getTotal();
            up += day.getUp();
            down += day.getDown();
        } else {
            weeks.add(new Week(0, weekNumber, Math.ceil(total), Math.ceil(up), Math.ceil(down)));
            total = 0;
            up = 0;
            down = 0;
            weekNumber++;
        }
    }

    if (weeks.isEmpty()) {
        throw new NullPointerException("List is empty");
    } else {
        return weeks;
    }
}

From source file:nu.yona.app.utils.DateUtility.java

/**
 * Gets retrive week.//from   w w  w  .j  a v  a  2s.c  o m
 *
 * @param week the week
 * @return the retrive week
 * @throws ParseException the parse exception
 */
public static String getRetriveWeek(String week) throws ParseException {
    String retriveWeek = "";
    DateTime time = new DateTime();
    int thisWeek = time.getWeekOfWeekyear();
    int pastWeek = thisWeek - 1;
    if (week.endsWith("" + thisWeek)) {
        retriveWeek = YonaApplication.getAppContext().getString(R.string.this_week);
    } else if (week.endsWith("" + pastWeek)) {
        retriveWeek = YonaApplication.getAppContext().getString(R.string.last_week);
    } else {
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        LocalDateTime localDateTime = LocalDateTime.parse(week, DateTimeFormat.forPattern("yyyy-'W'ww"));
        calendar.setTime(new Date(localDateTime.toDate().getTime()));
        calendar.add(Calendar.DAY_OF_WEEK, -1);
        Date startDate = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        Date endDate = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM");
        retriveWeek = sdf.format(startDate) + " - " + sdf.format(endDate);
    }
    return retriveWeek.toUpperCase();
}