PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/ftr-gwt-date-emulation/src/test/java/eu/future/earth/date/WeekCounter.java

http://ftr-gwt-library.googlecode.com/
Java | 201 lines | 122 code | 27 blank | 52 comment | 21 complexity | 50895358b2bdc3fc3926258754b11a0b MD5 | raw file
  1package eu.future.earth.date;
  2
  3import java.util.Calendar;
  4import java.util.Date;
  5import java.util.GregorianCalendar;
  6
  7public class WeekCounter {
  8
  9	@SuppressWarnings("deprecation")
 10	public static int getWeekOfYear(final Date date, int firstDayOfWeek, int minDaysOfFirstWeek) {
 11//		log("De dag is" , date);
 12		final GregorianCalendar tester = new GregorianCalendar();
 13		tester.setFirstDayOfWeek(firstDayOfWeek);
 14		tester.setTime(date);
 15		
 16		tester.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
 17		Date firstOfWeekForDate = tester.getTime();
 18//		log("First Day of week" , firstOfWeekForDate);
 19		tester.add(Calendar.DATE, 7);
 20		Date laatsteDagOfWeekForDate = tester.getTime();
 21//		log("Laatste Dag of week" , laatsteDagOfWeekForDate);
 22		if(laatsteDagOfWeekForDate.getYear() != firstOfWeekForDate.getYear()){
 23			final Date firstDayOfNextYear = getFirstDateOfYear(laatsteDagOfWeekForDate.getYear());
 24			int weekOneDuration = compareDate(firstOfWeekForDate, firstDayOfNextYear);
 25			if((7 - minDaysOfFirstWeek) >= weekOneDuration){
 26				return 1;
 27			}
 28		}
 29		if (firstOfWeekForDate.getYear() != date.getYear()) {
 30			return getWeekOfYearNoPrev(firstOfWeekForDate, firstDayOfWeek, minDaysOfFirstWeek);
 31		}
 32		
 33		final Date firstDayOfYear = getFirstDateOfYear(date.getYear());
 34		tester.setTime(firstDayOfYear);
 35		tester.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
 36		// tester.set(Calendar.DATE, 7);
 37		final Date startOfWeekForFirstDayInYear = tester.getTime();
 38//		log("Start of first day in week of year", startOfWeekForFirstDayInYear);
 39
 40		// Check the first week
 41		// if its shorter that the min skip it.
 42
 43		
 44		int count = 1;
 45		Date startOfWeekForFirstWeekInYear = startOfWeekForFirstDayInYear;
 46		if (startOfWeekForFirstDayInYear.getYear() != firstDayOfYear.getYear()) {
 47			tester.setTime(startOfWeekForFirstWeekInYear);
 48//			log("Start of First Week",  startOfWeekForFirstWeekInYear);
 49			int weekOneDuration = compareDate(startOfWeekForFirstWeekInYear, firstDayOfYear);
 50			if((minDaysOfFirstWeek) <= weekOneDuration){
 51				tester.add(Calendar.DATE, 7);	
 52				startOfWeekForFirstWeekInYear = tester.getTime();
 53			}
 54			
 55//			log("Start of First Week",  startOfWeekForFirstWeekInYear);
 56		}
 57//		log("Start of First Week",  startOfWeekForFirstWeekInYear);
 58//		if (weekOneDuration <= minDaysOfFirstWeek) {
 59//			count--;
 60//		}
 61
 62		int days = compareDate(startOfWeekForFirstWeekInYear, firstOfWeekForDate);
 63		System.out.println(days);
 64		while (days >= 7) {
 65			days = days - 7;
 66			count++;
 67		}
 68		if (count == 54) {
 69			count = 1;
 70		}
 71
 72		if (count == 0) {
 73			count = 53;
 74		}
 75		return count;
 76	}
 77
 78	@SuppressWarnings("deprecation")
 79	private static Date getFirstDateOfYear(int year){
 80		final Date firstDayOfYear = new Date(year, Calendar.JANUARY, 1);
 81		return firstDayOfYear;
 82	}
 83	
 84	@SuppressWarnings("deprecation")
 85	private static int getWeekOfYearNoPrev(final Date date, int firstDayOfWeek, int minDaysOfFirstWeek) {
 86		final Date firstDayOfYear = getFirstDateOfYear(date.getYear());
 87
 88		final GregorianCalendar tester = new GregorianCalendar();
 89		tester.setFirstDayOfWeek(firstDayOfWeek);
 90		tester.setTime(firstDayOfYear);
 91		tester.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
 92
 93		tester.setTime(firstDayOfYear);
 94		tester.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
 95		// tester.set(Calendar.DATE, 7);
 96		final Date startOfWeekForFirstDayInYear = tester.getTime();
 97//		log("Start of first day in week of year", startOfWeekForFirstDayInYear);
 98
 99		// Check the first week
100		// if its shorter that the min skip it.
101
102		
103		int count = 1;
104		Date startOfWeekForFirstWeekInYear = startOfWeekForFirstDayInYear;
105		if (startOfWeekForFirstDayInYear.getYear() != firstDayOfYear.getYear()) {
106			tester.setTime(startOfWeekForFirstWeekInYear);
107//			log("Start of First Week",  startOfWeekForFirstWeekInYear);
108			int weekOneDuration = compareDate(startOfWeekForFirstWeekInYear, firstDayOfYear);
109			if((minDaysOfFirstWeek) <= weekOneDuration){
110				tester.add(Calendar.DATE, 7);	
111				startOfWeekForFirstWeekInYear = tester.getTime();
112			}
113			
114//			log("Start of First Week",  startOfWeekForFirstWeekInYear);
115		}
116//		log("Start of First Week",  startOfWeekForFirstWeekInYear);
117//		if (weekOneDuration <= minDaysOfFirstWeek) {
118//			count--;
119//		}
120		tester.setTime(date);
121		tester.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
122		Date firstOfWeekForDate = tester.getTime();
123		int days = compareDate(startOfWeekForFirstWeekInYear, firstOfWeekForDate);
124		System.out.println(days);
125		while (days >= 7) {
126			days = days - 7;
127			count++;
128		}
129		if (count == 54) {
130			count = 1;
131		}
132
133		if (count == 0) {
134			count = 53;
135		}
136		return count;
137	}
138
139//	private static void log(String comment, Date theDate) {
140//		SimpleDateFormat formatter = new SimpleDateFormat("EEEEE dd/MM/yyyy");
141//		System.out.println(comment + " " + formatter.format(theDate));
142//	}
143
144	/**
145	 * Set hour, minutes, second and milliseconds to zero.
146	 * 
147	 * @param d
148	 *            Date
149	 * @return Modified date
150	 */
151	@SuppressWarnings("deprecation")
152	public static Date setHourToZero(Date in) {
153		final Date d = new Date(in.getTime());
154		d.setHours(0);
155		d.setMinutes(0);
156		d.setSeconds(0);
157		// a trick to set milliseconds to zero
158		long t = d.getTime() / 1000;
159		t = t * 1000;
160		return new Date(t);
161	}
162
163	/**
164	 * Calculate the number of days betwen two dates
165	 * 
166	 * @param a
167	 *            Date
168	 * @param b
169	 *            Date
170	 * @return the difference in days betwen b and a (b - a)
171	 */
172	public static int compareDate(Date a, Date b) {
173		final Date ta = new Date(a.getTime());
174		final Date tb = new Date(b.getTime());
175		final long d1 = setHourToZero(ta).getTime();
176		final long d2 = setHourToTen(tb).getTime();
177		return (int) ((d2 - d1) / 1000 / 60 / 60 / 24);
178	}
179
180	
181	
182	/**
183	 * Set hour to 10, minutes, second and milliseconds to zero.
184	 * 
185	 * @param d
186	 *            Date
187	 * @return Modified date
188	 */
189	@SuppressWarnings("deprecation")
190	public static Date setHourToTen(Date in) {
191		final Date d = new Date(in.getTime());
192		d.setHours(10);
193		d.setMinutes(0);
194		d.setSeconds(0);
195		// a trick to set milliseconds to zero
196		long t = d.getTime() / 1000;
197		t = t * 1000;
198		return new Date(t);
199	}
200
201}