Java check todays date is biweekly tuesday - java

I have a couple of requirement, I need to validate if today's date is matching with a condition and if true create records in DB.
but the condition is something like this - biweekly Tuesday, biweekly Thursday.
Can anybody provide how to achieve this?

private static Calendar cacheCalendar;
public static LocalDate getNDayOfMonth(int dayweek, int nthweek, int month, int year) {
LocalDate d = new LocalDate(year, month, 1).withDayOfWeek(dayweek);
if (d.getMonthOfYear() != month)
d = d.plusWeeks(1);
return d.plusWeeks(nthweek - 1);
}
public static LocalDate getLastWeekdayOfMonth(int dayweek, int month, int year) {
LocalDate d = new LocalDate(year, month, 1).plusMonths(1).withDayOfWeek(dayweek);
if (d.getMonthOfYear() != month)
d = d.minusWeeks(1);
return d;
}
public static void main(String[] args) {
cacheCalendar = Calendar.getInstance();
for (int i = 1; i < 13; i++) {
// second wednesday of oct-2011
LocalDate secondMonday = getNDayOfMonth(DateTimeConstants.MONDAY, 2, i, cacheCalendar.get(Calendar.YEAR));
System.out.println("secondMonday= " + secondMonday);
LocalDate secondTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 2, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("secondTuesday= " + secondTuesday);
LocalDate monthlyTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 1, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("monthlyTuesday= " + monthlyTuesday);
LocalDate weeklyFirstFriday = getNDayOfMonth(DateTimeConstants.FRIDAY, 1, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("weeklyFirstFriday= " + weeklyFirstFriday);
}
}

Related

Set date in DatePickerDialog from String

How can I set date in a DatePicker from a string (eg: 02/10/19):
Following is the code:
iqp_editDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(ActivityClass.this, (DatePickerDialog.OnDateSetListener) date1, y, m, d).show();
}
});
DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
y = year;
m = month;
d = dayOfMonth;
dateMonth = month + 1;
dateYear = year;
}
};
Update: misunderstood the question:
Calendar cal = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY,18);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.DATE,2);
cal.set(Calendar.MONTH,9); //Month -1
//Or to set it from a String:
String string = "02/10/19";
DateFormat format = new SimpleDateFormat("dd/MM/yy", Locale.getDefault());
Date date = format.parse(string);
cal.setTimeInMillis(date.getTime());
new DatePickerDialog(getContext(),date1, cal
.get(Calendar.YEAR), cal .get(Calendar.MONTH),
cal .get(Calendar.DAY_OF_MONTH)).show();
A string is a character array. Meaning that you could just make a loop and set the different day, month and year values to different parts of the string.
String date = "02/19/19";
String year = "";
for(int i = 0; i < date.length; i++)
{
month += date.charAt(i);
...
}
Then you'd tell it when to switch from adding to month to day to year when it encounters '/'
if(date.charAt(i) == '/')
{
...
}
at the end of it all if you need to make it into an int then do
int month = Integer.parseInt("month");
sorry I've gotta be going somewhere so I couldn't just write the code out for ya but I'm sure you can figure it out from what I gave ya.
Following code works fine for me:
new DatePickerDialog(Activityclass.this, date1, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
view.setMinDate(1559327400);
y = year;
m = month;
d = dayOfMonth;
dateMonth = month + 1;
dateYear = year;
iqp_editDate.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
try {
epoch = new java.text.SimpleDateFormat("MM/dd/yyyy").parse(m + "/" + dayOfMonth + "/" + year).getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
}
};

How can I create a calculation to get the age of a person from two dates?

I am trying to make a method that will calculate the age of a person. I want the calculation to be done under the second public static int getAge. If the person is born after the current date i want it to print out error -1.
How do I compare the two SimpleDate values dateBd and dateRef in order to get an int value for age?
public static SimpleDate today() {
Calendar todayCal = Calendar.getInstance();
SimpleDate todayDate = new SimpleDate();
todayDate.setDate(todayCal.get(Calendar.MONTH) + 1,
todayCal.get(Calendar.DATE),
todayCal.get(Calendar.YEAR));
return todayDate;
public static int getAge(SimpleDate dateBd) {
int age;
SimpleDate dateToday = today();
age = getAge(dateBd, dateToday);
return age;
public static int getAge(SimpleDate dateBd, SimpleDate dateRef) {
if(getAge(dateBd)>getAge(dateRef)){
system.out.println("error");
}
return -1;
What is SimpleDate ? Anyway here something to get you started
import java.util.GregorianCalendar;
import java.util.Calendar;
public class CalcAge {
public static void main(String [] args) {
// remember ... months are 0-based : jan=0 feb=1 ...
System.out.println
("1962-11-11 : " + age(1962,10,11));
System.out.println
("1999-12-03 : " + age(1999,11,3));
}
private static int age(int y, int m, int d) {
Calendar cal = new GregorianCalendar(y, m, d);
Calendar now = new GregorianCalendar();
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
|| (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
&& cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH)))
{
res--;
}
return res;
}
}
Don't ever try and use the millisecond difference between two times to calculate the differences, there are just to many idiosyncrasies with date/time calculations which can cause all sorts of erroneous errors.
Instead, save yourself (alot) of time and use a dedicated library
Java 8
LocalDate start = LocalDate.of(1972, Month.MARCH, 8);
LocalDate end = LocalDate.now();
long years = ChronoUnit.YEARS.between(start, end);
System.out.println(years);
Which outputs 43
JodaTime
DateTime startDate = new DateTime(1972, DateTimeConstants.MARCH, 8, 0, 0);
DateTime endDate = new DateTime();
Years y = Years.yearsBetween(startDate, endDate);
int years = y.getYears();
System.out.println(years );
Which outputs 43
You can even use a Period to gain more granuarlity...
Period period = new Period(startDate, endDate);
PeriodFormatter hms = new PeriodFormatterBuilder()
.printZeroAlways()
.appendYears()
.appendSeparator(" years, ")
.appendMonths()
.appendSeparator(" months, ")
.appendDays()
.appendLiteral(" days")
.toFormatter();
String result = hms.print(period);
System.out.println(result);
Which prints 43 years, 1 months, 5 days

Using Java Dates, Given a year, I want a method that should return a list of dates representing a given day of the week, all 52 times

A method receives and integer representing a year and an integer representing a day of the week. The method should return a list of dates representing a given day of the week. For example, if the year is 2014 and the day of week is 2 then the method should return a list of dates representing all the Mondays in 2014.
public List<Date> getDatesforDayOfWeek(int year, int dayOfWeek) throws InvalidDateException
I'm not too sure what the best code would be. Any suggestions?
if (year <= 0) {
throw new InvalidDateException("Invalid year.");
}
if ((dayOfWeek < 1) || (dayOfWeek > 7)) {
throw new InvalidDateException("Invalid day.");
}
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Check this implementation that would work for non-leap years (and for leap years with some modification)
public List<Date> getDatesforDayOfWeek(int year, int dayOfWeek)
throws InvalidDateException {
if (year <= 0) {
throw new InvalidDateException("Invalid year.");
}
if ((dayOfWeek < 1) || (dayOfWeek > 7)) {
throw new InvalidDateException("Invalid day.");
}
List<Date> dates = new ArrayList<Date>();
// Start with the given inputs
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.JANUARY);
int day = cal.get(Calendar.DAY_OF_WEEK);
while (day != dayOfWeek) {
cal.add(Calendar.DATE, 1);
day = cal.get(Calendar.DAY_OF_WEEK);
}
// Make this 366 for a leap year
for (int i = 0; i < 365; i += 7) {
if (cal.get(Calendar.DAY_OF_WEEK) == dayOfWeek
&& cal.get(Calendar.YEAR) == year) {
dates.add(cal.getTime());
cal.add(Calendar.DATE, 7);
}
}
return dates;
}
public static void main(String[] args) throws InvalidDateException {
for (Date date : new DateList().getDatesforDayOfWeek(2014, 4)) {
System.out.println(date);
}
}
}
class InvalidDateException extends Exception {
public InvalidDateException(String string) {
super(string);
}
private static final long serialVersionUID = 1L;
}
If I understand your question, I would use a Calendar to solve this problem (actually I'd first pick Joda-Time, but otherwise Calendar because there are a lot of corner cases to handle) -
public static List<Date> getDatesforDayOfWeek(int year, int dayOfWeek) {
List<Date> al = new ArrayList<>();
if (dayOfWeek >= 1 && dayOfWeek <= 7) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
while (cal.get(Calendar.DAY_OF_WEEK) != dayOfWeek) {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
for (int i = 0; i < 52; i++) {
al.add(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 7);
}
}
return al;
}
public static void main(String[] args) {
System.out.println(getDatesforDayOfWeek(2014, 2));
}
Output is (formatted, and 50 lines omitted, for display)
[Mon Jan 06 23:52:16 EST 2014,
...
Mon Dec 29 23:52:16 EST 2014]
Looking through all the answers they will provide you what you're looking for. Here is another approach I believe to be a little more efficient then the previous answers.
public static List<Date> GetDatesForDayOfWeek(int year, int dayOfWeek)
{
// Days in the week
final int DAYS_IN_WEEK = 7;
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, 0); // January
c.set(Calendar.DAY_OF_MONTH, 1); // The first
// Find the first day we are interested in
int offset = c.get(Calendar.DAY_OF_WEEK);
if(offset!= dayOfWeek)
{
c.add(Calendar.DATE, (dayOfWeek < offset ? (offset + 7) % 7 : (dayOfWeek - offset)));
}
while(c.get(Calendar.YEAR) == year)
{
dates.add(c.getTime());
c.add(Calendar.DATE, DAYS_IN_WEEK);
}
return dates;
}
public static void main(String[] args){
System.out.println("A list of all Sundays in 2014.");
List<Date> days = GetDatesForDayOfWeek(2014, Calendar.SUNDAY);
for(Date ad : days){
System.out.println(ad);
}
}
Running the above would give you every sunday in 2014 with the output of.
A list of all Sundays in 2014.
Sun Jan 05 23:49:56 EST 2014
...
Sun Dec 28 23:49:56 EST 2014
New Java8 DateTime API gives very clean methods to use for any Date, Time related code.
Below is my part on your query.
public class ExampleDayOfYear {
public static void main(String[] args) throws InvalidDateException {
List<LocalDate> list=getDatesforDayOfWeek(2020,2);
list.forEach(System.out::println);
System.out.println("Total dates: "+list.size());
}
public static String getDay(int day, int month, int year) {
LocalDate dt=LocalDate.of(year,month,day);
return dt.getDayOfWeek().toString();
}
private static int getDayOfYear(int year){
LocalDate dt = LocalDate.parse(year+"-01-01");
return dt.getDayOfWeek().getValue();
}
public static List<LocalDate> getDatesforDayOfWeek(int year, int dayOfWeek) throws
InvalidDateException{
List<LocalDate> list=new ArrayList<LocalDate>();
int differenceOfdaysToDayofWeek=0;
int firstDayOfYear= getDayOfYear(year); // Wednesday
int count=1;
// This method call gives the date of first Monday. Also, this method requires the differenceOfDate which we have found out from getDaysDifference.
LocalDate firstDateOfWeek=getFirstDateOfWeek(year,getDaysDifference(firstDayOfYear));
list.add(firstDateOfWeek);
while(firstDateOfWeek.getYear()== year){
firstDateOfWeek= getDateWeekWise(firstDateOfWeek);
if(firstDateOfWeek.getYear()==year){
count++;
list.add(firstDateOfWeek); }
}
return list;
}
// First Day is Wednesday, so we need ot get first date of the January, 2020.
private static int getDaysDifference(int firstDayOfYear){
int difDays=0;
int dayofWeekNum=Integer.valueOf(DayOfWeek.MONDAY.ordinal()+1);
// It gives the int value of DayOfWeek. e.g. if it's Monday it will give 0 because week ordinal value starts from 0.
if(firstDayOfYear==dayofWeekNum) {
}if(firstDayOfYear>dayofWeekNum){
difDays= dayofWeekNum+7 -firstDayOfYear;
}if(firstDayOfYear<dayofWeekNum)
{
difDays= dayofWeekNum -firstDayOfYear;
}
return difDays;
}
private static LocalDate getFirstDateOfWeek(int year,int differenceOfWeekDays){
LocalDate dt = LocalDate.of(year,01,01).plusDays(differenceOfWeekDays);
return dt;
}
private static LocalDate getDateWeekWise(LocalDate dt){
dt=dt.plusWeeks(1);
return dt;
}
}
class InvalidDateException extends Exception{
InvalidDateException(String s){
super(s);
}
}

How to get every day except weekend or Saturday or Sunday between two dates in java?

hi all how to generate dates if the start date and end date are provided in java?
like the following :
Ex : if the start date is 15-04-2012 and end date is 15-06-2012 having this i want dates to be like the following and the list should omit saturday and sunday
1.15-04-2012,
2.16-04-2012,
3.17-04-2012,
.
.
.
.
.
.
15-06-2012
I have done like the following but it will generate for one month if the date range is of same month.
`
public static ConcurrentHashMap<String, String> getWorkingDaysMap(int year, int month, int day){
int totalworkingdays=0,noofdays=0;
String nameofday = "";
ConcurrentHashMap<String,String> workingDaysMap = new ConcurrentHashMap<String,String>();
Map<String,String> holyDayMap = new LinkedHashMap<String,String>();
noofdays = findNoOfDays(year,month,day);
for (int i = 1; i <= noofdays; i++) {
Date date = (new GregorianCalendar(year,month - 1, i)).getTime(); // year,month,day
SimpleDateFormat f = new SimpleDateFormat("EEEE");
nameofday = f.format(date);
String daystr="";
String monthstr="";
if(i<10)daystr="0";
if(month<10)monthstr="0";
String formatedDate = daystr+i+"/"+monthstr+month+"/"+year;
if(!(nameofday.equals("Saturday") || nameofday.equals("Sunday"))){
workingDaysMap.put(formatedDate,formatedDate);
totalworkingdays++;
}
}
return workingDaysMap;
}
So please do advice me how to go about.
Regards
Tony
int noOfDaysBetween = 5;
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
for(int index = 0 ; index < noOfDaysBetween; index++){
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if( dayOfWeek!=Calendar.SUNDAY && dayOfWeek!=Calendar.SATURDAY) {
System.out.println(cal.getTime());
}
cal.add(Calendar.DATE, 1);
}
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
//15-04-2012
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.YEAR, 2012);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MONTH, 3);
Date start = calendar.getTime();
//15-06-2012
calendar.set(Calendar.MONTH, 5);
Date end = calendar.getTime();
calendar.setTime(start);
Date d = null;
while((d = calendar.getTime()).before(end) || d.equals(end)) {
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) {
System.out.println(d);
}
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
I prefer usage of LocalDate.
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Game {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2018, 6, 1);
LocalDate end = LocalDate.of(2018, 8, 1);
LocalDate d = start;
while(d.compareTo(end) <= 0) {
if (d.getDayOfWeek() != DayOfWeek.SATURDAY && d.getDayOfWeek() != DayOfWeek.SUNDAY) {
System.out.println(d);
}
d = d.plusDays(1);
}
}
}
use the following joda-time
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
public class DatesexcludingWeekend {
public static void main(String[] args) {
final LocalDate start = new LocalDate(2012, 06, 15);
final LocalDate end = new LocalDate(2012, 07, 14);
LocalDate weekday = start;
if (start.getDayOfWeek() == DateTimeConstants.SATURDAY|| start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
}
while (weekday.isBefore(end)) {
System.out.println(weekday);
if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
weekday = weekday.plusDays(3);
else
weekday = weekday.plusDays(1);
}
}

Java calendar getting weekdays not working

I am trying to get this to output all the weekdays (MON-FRI) between 5/16/2010 (a sunday) and 5/25/2010 (a tuesday). The correct output should be 17,18,19,20,21,24,25. However, the result im getting is 17,18,19,20,21,17,18,19. The other methods just split up the string the date is in
import java.util.*;
public class test
{
public static void main(String[] args) {
String startTime = "5/16/2010 11:44 AM";
String endTime = "5/25/2010 12:00 PM";
GregorianCalendar startCal = new GregorianCalendar();
startCal.setLenient(true);
String[] start = splitString(startTime);
//this sets year, month day
startCal.set(Integer.parseInt(start[2]),Integer.parseInt(start[0])-1,Integer.parseInt(start[1]));
startCal.set(GregorianCalendar.HOUR, Integer.parseInt(start[3]));
startCal.set(GregorianCalendar.MINUTE, Integer.parseInt(start[4]));
if (start[5].equalsIgnoreCase("AM")) { startCal.set(GregorianCalendar.AM_PM, 0); }
else { startCal.set(GregorianCalendar.AM_PM, 1); }
GregorianCalendar endCal = new GregorianCalendar();
endCal.setLenient(true);
String[] end = splitString(endTime);
endCal.set(Integer.parseInt(end[2]),Integer.parseInt(end[0])-1,Integer.parseInt(end[1]));
endCal.set(GregorianCalendar.HOUR, Integer.parseInt(end[3]));
endCal.set(GregorianCalendar.MINUTE, Integer.parseInt(end[4]));
if (end[5].equalsIgnoreCase("AM")) { endCal.set(GregorianCalendar.AM_PM, 0); }
else { endCal.set(GregorianCalendar.AM_PM, 1); }
for (int i = startCal.get(Calendar.DATE); i < endCal.get(Calendar.DATE); i++)
{
startCal.set(Calendar.DATE, i);
startCal.set(Calendar.DAY_OF_WEEK, i);
if (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
{
System.out.println("\t" + startCal.get(Calendar.DATE));
}
}
}
private static String[] splitDate(String date)
{
String[] temp1 = date.split(" "); // split by space
String[] temp2 = temp1[0].split("/"); // split by /
//5/21/2010 10:00 AM
return temp2; // return 5 21 2010 in one array
}
private static String[] splitTime(String date)
{
String[] temp1 = date.split(" "); // split by space
String[] temp2 = temp1[1].split(":"); // split by :
//5/21/2010 10:00 AM
String[] temp3 = {temp2[0], temp2[1], temp1[2]};
return temp3; // return 10 00 AM in one array
}
private static String[] splitString(String date)
{
String[] temp1 = splitDate(date);
String[] temp2 = splitTime(date);
String[] temp3 = new String[6];
return dateFill(temp3, temp2[0], temp2[1], temp2[2], temp1[0], temp1[1], temp1[2]);
}
private static String[] dateFill(String[] date, String hours, String minutes, String ampm, String month, String day, String year) {
date[0] = month;
date[1] = day;
date[2] = year;
date[3] = hours;
date[4] = minutes;
date[5] = ampm;
return date;
}
private String dateString(String[] date) {
//return month+" "+day+", "+year+" "+hours+":"+minutes+" "+ampm
//5/21/2010 10:00 AM
return date[3]+"/"+date[4]+"/ "+date[5]+" "+date[0]+":"+date[1]+" "+date[2];
}
}
startCal.set(Calendar.DAY_OF_WEEK, i); Will flip flip your date back every 7 loops.
This code isn't good.
I don't understand why you're doing all this parsing of Strings to get to Date and visa versa when you have java.text.DateFormat and java.text.SimpleDateFormat to do it easily for you.
I think this is better. See if you agree:
package com.contacts.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtils
{
private static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");
public static void main(String[] args)
{
try
{
Date startDate = ((args.length > 0) ? DEFAULT_FORMAT.parse(args[0]) : new Date());
Date endDate = ((args.length > 1) ? DEFAULT_FORMAT.parse(args[1]) : new Date());
List<Date> weekdays = DateUtils.getWeekdays(startDate, endDate);
Calendar calendar = Calendar.getInstance();
for (Date d : weekdays)
{
calendar.setTime(d);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
// System.out.println(DEFAULT_FORMAT.format(d));
System.out.println("day: " + dayOfMonth + " month: " + (month+1) + " year: " + year);
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
public static List<Date> getWeekdays(Date startDate, Date endDate)
{
List<Date> weekdays = new ArrayList<Date>();
if ((startDate == null) || (endDate == null))
return weekdays;
if (startDate.equals(endDate))
{
if (isWeekday(startDate))
{
weekdays.add(startDate);
}
}
else if (startDate.after(endDate))
{
weekdays = getWeekdays(endDate, startDate);
}
else
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
Date d = startDate;
while (endDate.equals(d) || endDate.after(d))
{
if (isWeekday(d))
{
weekdays.add(d);
}
calendar.add(Calendar.DATE, 1);
d = calendar.getTime();
}
}
return weekdays;
}
public static boolean isWeekday(Date d)
{
if (d == null)
return false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return ((dayOfWeek >= Calendar.MONDAY) && (dayOfWeek <= Calendar.FRIDAY));
}
}
I don't know if this is an issue with your code, but JDK uses some unexpected values for Calendar constants. For example, months star with zero. In other words, Calendar.JANUARY is 0. On the other hand, weekdays are 1 to 7, starting with Sunday as 1. etc.
I luckily don't know much about Date in Java, but I know it's basically a difficult and bad API. Go for JodaTime until the new JSR-310 is done.

Categories