View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package millscript;
22  
23  import java.lang.*;
24  import java.util.*;
25  
26  public final class DateTools {
27  
28  	public static int currentYear() {
29  		Calendar calendar = Calendar.getInstance();
30  		return calendar.get(Calendar.YEAR);
31  	}
32  
33  	public static int currentMonth() {
34  		Calendar calendar = Calendar.getInstance();
35  		return calendar.get(Calendar.MONTH);
36  	}
37  
38  	public static int currentDay() {
39  		Calendar calendar = Calendar.getInstance();
40  		return calendar.get(Calendar.DAY_OF_MONTH);
41  	}
42  
43  	public static int currentTime() {
44  		Calendar calendar = Calendar.getInstance();
45  		Date date = calendar.getTime();
46  		// Return the number of seconds since
47  		// January 1, 1970, 00:00:00
48  		return (int)(date.getTime() / 1000);
49  	}
50  
51  	public static int getYear( Integer seconds ) {
52  		Calendar calendar = Calendar.getInstance();
53  		// Initialise a new Date object with the supplied
54  		// number of milliseconds.
55  		Date date = new Date( ((long)seconds.intValue()) * 1000 );
56  
57  		calendar.setTime( date );
58  
59  		return calendar.get(Calendar.YEAR);
60  	}
61  
62  	public static int getMonth( Integer seconds ) {
63  		Calendar calendar = Calendar.getInstance();
64  		// Initialise a new Date object with the supplied
65  		// number of milliseconds.
66  		Date date = new Date( ((long)seconds.intValue()) * 1000 );
67  
68  		calendar.setTime( date );
69  
70  		return calendar.get(Calendar.MONTH);
71  	}
72  
73  	public static int getDay( Integer seconds ) {
74  		Calendar calendar = Calendar.getInstance();
75  		// Initialise a new Date object with the supplied
76  		// number of milliseconds.
77  		Date date = new Date( ((long)seconds.intValue()) * 1000 );
78  
79  		calendar.setTime( date );
80  
81  		return calendar.get(Calendar.DAY_OF_MONTH);
82  	}
83  
84  	/*
85  	// threeDigitMonth
86  	*/
87  	public static String shortMonth( int month ) {
88  
89  		return(
90  			month == Calendar.JANUARY ? "Jan" :
91  			month == Calendar.FEBRUARY ? "Feb" :
92  			month == Calendar.MARCH ? "Mar" :
93  			month == Calendar.APRIL ? "Apr" :
94  			month == Calendar.MAY ? "May" :
95  			month == Calendar.JUNE ? "Jun" :
96  			month == Calendar.JULY ? "Jul" :
97  			month == Calendar.AUGUST ? "Aug" :
98  			month == Calendar.SEPTEMBER ? "Sep" :
99  			month == Calendar.OCTOBER ? "Oct" :
100 			month == Calendar.NOVEMBER ? "Nov" :
101 			month == Calendar.DECEMBER ? "Dec" :
102 			""
103 		);
104 	}
105 
106 	/*
107 	// getThis
108 	*/
109 	public static int getThis( int day, int field ) {
110 		Calendar calendar = Calendar.getInstance();
111 
112 		for (;;) {
113 			// Loop until we get the required day.
114 			if ( calendar.get(Calendar.DAY_OF_WEEK) == day ) {
115 				return calendar.get( field );
116 			} else {
117 				calendar.roll(Calendar.DAY_OF_MONTH, 1);
118 			}
119 		}
120 	}
121 
122 	/*
123 	// getNext
124 	*/
125 	public static int getNext( int day, int field ) {
126 		Calendar calendar = Calendar.getInstance();
127 
128 		boolean thisweek = true;
129 
130 		for (;;) {
131 			// Loop until we get the required day.
132 			if ( calendar.get(Calendar.DAY_OF_WEEK) == day ) {
133 				if ( thisweek == true ) {
134 					thisweek = false;
135 				} else {
136 					return calendar.get( field );
137 				}
138 			}
139 			calendar.roll(Calendar.DAY_OF_MONTH, 1);
140 
141 		}
142 	}
143 
144 	/*
145 	// This weekends date functions.
146 	// i.e. the coming Friday and Sunday
147 	*/
148 	public static int getThisFridayDay() {
149 		return getThis( Calendar.FRIDAY, Calendar.DAY_OF_MONTH );
150 	}
151 
152 	public static String getThisFridayMonth() {
153 		return shortMonth(
154 			getThis( Calendar.FRIDAY, Calendar.MONTH )
155 		);
156 	}
157 
158 	public static int getThisFridayYear() {
159 		return getThis( Calendar.FRIDAY, Calendar.YEAR );
160 	}
161 
162 	public static int getThisSundayDay() {
163 		return getThis( Calendar.SUNDAY, Calendar.DAY_OF_MONTH );
164 	}
165 
166 	public static int getThisSundayYear() {
167 		return getThis( Calendar.SUNDAY, Calendar.YEAR );
168 	}
169 
170 	public static String getThisSundayMonth() {
171 		return shortMonth(
172 			getThis( Calendar.SUNDAY, Calendar.MONTH )
173 		);
174 	}
175 
176 	/*
177 	// Next weekends date functions.
178 	// i.e. the Friday and Sunday after the above functions.
179 	*/
180 	public static int getNextFridayDay() {
181 		return getNext( Calendar.FRIDAY, Calendar.DAY_OF_MONTH );
182 	}
183 
184 	public static String getNextFridayMonth() {
185 		return shortMonth(
186 			getNext( Calendar.FRIDAY, Calendar.MONTH )
187 		);
188 	}
189 
190 	public static int getNextFridayYear() {
191 		return getNext( Calendar.FRIDAY, Calendar.YEAR );
192 	}
193 
194 	public static int getNextSundayDay() {
195 		return getNext( Calendar.SUNDAY, Calendar.DAY_OF_MONTH );
196 	}
197 
198 	public static String getNextSundayMonth() {
199 		return shortMonth(
200 			getNext( Calendar.SUNDAY, Calendar.MONTH )
201 		);
202 	}
203 
204 	public static int getNextSundayYear() {
205 		return getNext( Calendar.SUNDAY, Calendar.YEAR );
206 	}
207 }