Java Code Examples for java.util.Calendar.getWeekYear()

The following are Jave code examples for showing how to use getWeekYear() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 8 votes
@Override
public String format(DateTime timestamp) {
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(4);
    c.setTimeInMillis(timestamp.getMillis());
    int week = c.get(Calendar.WEEK_OF_YEAR);
    int weekYear = c.getWeekYear();
    int year = c.get(Calendar.YEAR);
    if (weekYear < year) {
        week = 0;
    } else if (weekYear > year) {
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 2
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 8 votes
@Override
public String format(DateTime timestamp) {
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(4);
    c.setTimeInMillis(timestamp.getMillis());

    int week = c.get(Calendar.WEEK_OF_YEAR);
    int weekYear = c.getWeekYear();
    int year = c.get(Calendar.YEAR);
    if (weekYear < year) {
        // get weeks from last year
        c.add(Calendar.DAY_OF_MONTH, -7);
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 3
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 7 votes
@Override
public String format(DateTime timestamp) {
    // if week starts in last year, return 00
    // range 00 - 53
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.SUNDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTimeInMillis(timestamp.getMillis());
    int week = c.get(Calendar.WEEK_OF_YEAR);
    int weekYear = c.getWeekYear();
    int year = c.get(Calendar.YEAR);
    if (weekYear < year) {
        week = 0;
    } else if (weekYear > year) {
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 4
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 7 votes
@Override
public String format(DateTime timestamp) {
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.SUNDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTimeInMillis(timestamp.getMillis());

    int week = c.get(Calendar.WEEK_OF_YEAR);
    int weekYear = c.getWeekYear();
    int year = c.get(Calendar.YEAR);
    if (weekYear < year) {
        // get weeks from last year
        c.add(Calendar.DAY_OF_MONTH, -7);
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}