Java Week getThisWeek()

Here you can find the source of getThisWeek()

Description

Description:Get the start date - end date string before current week according to week number

specified by parameter The format is "yyyy/MM/dd-yyyy/MM/dd"

License

Open Source License

Parameter

Parameter Description

Return

SortedMap

Declaration

public static String getThisWeek() 

Method Source Code

//package com.java2s;
/*//ww w . j  a v  a 2  s. c  o m
 * $RCSfile: DatetimeUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2011  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

import java.text.DateFormatSymbols;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Locale;

public class Main {
    /**array reprsents offset bewwen Monday and current date, start from Sunday*/
    private static final int[] offsets = { -6, 0, -1, -2, -3, -4, -5 };

    /**
     * <p>Description:Get the start date - end date string before current week according to week number</p>
     * specified by parameter
     * The format is "yyyy/MM/dd-yyyy/MM/dd"
     * @param int weekNum
     * @return SortedMap
     */
    public static String getThisWeek() {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd", new DateFormatSymbols(Locale.US));

        int day = now.get(Calendar.DAY_OF_WEEK);
        int offset = offsets[day - Calendar.SUNDAY] - 1;
        now.add(Calendar.DAY_OF_YEAR, offset);
        String temp = null;

        now.add(Calendar.DAY_OF_YEAR, +1);
        temp = formatter.format(now.getTime());
        now.add(Calendar.DAY_OF_YEAR, +6);
        temp += "-" + formatter.format(now.getTime());

        return temp;
    }
}

Related

  1. getPreWeekDayByStr(String curday)
  2. getPreWeeks(int weekNum)
  3. getStartDate(Date date, int weeks, Locale locale)
  4. getStartDateOfWeek(Date selectedDate, Locale locale)
  5. getThisWeek()
  6. getWeek()
  7. getWeek()
  8. getWeek(Date date)
  9. getWeek(int duration)