Here you can find the source of getPreWeeks(int weekNum)
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"Parameter | Description |
---|
public static SortedMap<Integer, String> getPreWeeks(int weekNum)
//package com.java2s; /*/*from www . ja v a2 s.co 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.Date; import java.util.Locale; import java.util.SortedMap; import java.util.TreeMap; 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 SortedMap<Integer, String> getPreWeeks(int weekNum) { SortedMap<Integer, String> m = new TreeMap<Integer, String>(); Calendar now = Calendar.getInstance(); now.add(Calendar.DAY_OF_YEAR, -(7 * (weekNum - 1))); 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; for (int i = 0; i < weekNum; i++) { now.add(Calendar.DAY_OF_YEAR, +1); temp = formatter.format(now.getTime()); now.add(Calendar.DAY_OF_YEAR, +6); temp += "-" + formatter.format(now.getTime()); m.put(new Integer(i), temp); } return m; } /** * <p>Description:Get the start date - end date string according to selected base date and week number * The string format is "yyyy/MM/dd - yyyy/MM/dd" * </p> * @param int weekNum number of weeks contained in result string * @param Date baseDate start date of last week * @return SortedMap */ public static SortedMap<Integer, String> getPreWeeks(int weekNum, Date baseDate) { SortedMap<Integer, String> m = new TreeMap<Integer, String>(); Calendar calendar = Calendar.getInstance(); calendar.setTime(baseDate); calendar.add(Calendar.DAY_OF_YEAR, -(7 * (weekNum - 1) + 1)); SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd", new DateFormatSymbols(Locale.US)); String temp = ""; for (int i = 0; i < weekNum; i++) { calendar.add(Calendar.DAY_OF_YEAR, +1); temp = formatter.format(calendar.getTime()); calendar.add(Calendar.DAY_OF_YEAR, +6); temp += " - " + formatter.format(calendar.getTime()); m.put(new Integer(i), temp); } return m; } }