0

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.

user777904
  • 29
  • 1
  • 7

2 Answers2

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