Removing weekends in a vector filled with Dates
I have a vector that is created by retrieving data from a database It is filled with information that includes Date, Time, Volume ect开发者_如何学C... 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.
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
}
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;
精彩评论