I have a vector that is created by retrieving data from a database It is filled with information that includes Date, Time, Volume ect... I need to create a spreadsheet from these values however I need to Remove values where the date is on a weekend(and specific holidays if possible) but more importantly the weekends If anyone knows of a function that is able to do this that would be great Thanks.
Asked
Active
Viewed 1,090 times
0
-
1This question would be improved by adding some sample code and an example of the vector data. – Caley Woods May 31 '11 at 15:09
-
3Can you modify the SQL used to retrieve the data to exclude the weekends? – highlycaffeinated May 31 '11 at 15:09
-
`java.util.Date` or `java.sql.Date`? – mre May 31 '11 at 15:10
-
check this thread: http://stackoverflow.com/questions/3272454/java-example-to-get-all-weekend-dates-in-a-given-month – abalogh May 31 '11 at 15:15
2 Answers
5
If you have a Date
object in Java, then you can use the Calendar object to do this.
Get an instance of the Calendar, then call get(Calendar.DAY_OF_WEEK). If the value is Saturday or Sunday, remove it from your Collection
.
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDateObject);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
//remove this Date
}

nicholas.hauschild
- 42,483
- 9
- 127
- 120
3
If you have the timestamp of the Date, you could use a GregorianCalendar.
http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html#setFirstDayOfWeek(int)
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(yourDate);
cal.get(Calendar.DAY_OF_WEEK) // Would be Calendar.SUNDAY or something;

phtrivier
- 13,047
- 6
- 48
- 79