Java Calendar Year getWeekOfYear(Calendar currentDate)

Here you can find the source of getWeekOfYear(Calendar currentDate)

Description

Method getWeekOfYear.

License

Open Source License

Parameter

Parameter Description
currentDate Calendar

Return

int

Declaration

public static String getWeekOfYear(Calendar currentDate) 

Method Source Code

//package com.java2s;
/**/*from www . j a  v a  2s.  c  om*/
 * @author David Garratt
 * 
 * Project Name : Commander4j
 * 
 * Filename     : JUtility.java
 * 
 * Package Name : com.commander4j.util
 * 
 * License      : GNU General Public License
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * http://www.commander4j.com/website/license.html.
 * 
 */

import java.util.Calendar;

public class Main {
    /**
     * Method getWeekOfYear.
     * 
     * @param currentDate
     *            Calendar
     * @return int
     */
    public static String getWeekOfYear(Calendar currentDate) {
        int temp = 0;
        String result = "";

        temp = currentDate.get(Calendar.WEEK_OF_YEAR);
        result = String.valueOf(temp).trim();
        result = padString(result, false, 2, "0");

        return result;
    }

    /**
     * Method padString.
     * 
     * @param size
     *            int
     * @param character
     *            String
     * @return String
     */
    public static String padString(int size, String character) {
        String s = "";

        for (int i = 0; i < size; i++) {
            s = s + character;
        }

        return s;
    }

    /**
     * Method padString.
     * 
     * @param input
     *            String
     * @param right
     *            boolean
     * @param size
     *            int
     * @param character
     *            String
     * @return String
     */
    public static String padString(String input, boolean right, int size, String character) {
        int inputlength = 0;
        String result = replaceNullStringwithBlank(input);

        inputlength = result.length();

        if (inputlength > size) {
            // result = result.substring(0,size-1);
            result = result.substring(0, size);
        } else {
            if (inputlength < size) {
                if (right == true) {
                    result = result + padString(size - inputlength, character);
                } else {
                    result = padString(size - inputlength, character) + result;
                }
            }
        }

        return result;
    }

    public static String replaceNullStringwithBlank(String value) {
        if (value == null) {
            value = "";
        }

        return value;
    }
}

Related

  1. getFiscalYear(Calendar calendar)
  2. getIntYear(Calendar c)
  3. getLastYear(Calendar cal)
  4. getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale)
  5. getValidCalendar(int year, int month, int day)
  6. getWeekOfYear(int startDay, Calendar dt)
  7. getWeekOfYearYear(GregorianCalendar cal)
  8. getYear(Calendar cal)
  9. getYear(Calendar calendar)