Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use following code: </p> <pre><code>public class DateUtils{ private static final DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MMM-dd"); public static void main(String[] args){ try { Date beg = ((args.length &gt; 0) ? DEFAULT_DATE_FORMAT.parse(args[0]) : new Date()); Date end = ((args.length &gt; 1) ? DEFAULT_DATE_FORMAT.parse(args[1]) : new Date()); System.out.println("beg: " + DEFAULT_DATE_FORMAT.format(beg)); System.out.println("end: " + DEFAULT_DATE_FORMAT.format(end)); System.out.println("# days between: " + interval(beg, end, new IncludeAllDateRule(), Calendar.DATE)); System.out.println("# weekdays between: " + interval(beg, end, new ExcludeWeekendDateRule(), Calendar.DATE)); System.out.println("# hours between: " + interval(beg, end, new IncludeAllDateRule(), Calendar.HOUR)); System.out.println("# minutes between: " + interval(beg, end, new IncludeAllDateRule(), Calendar.MINUTE) ); } catch (ParseException e) { e.printStackTrace(); } } public static int interval(Date beg, Date end, DateRule dateRule, int intervalType) { Calendar calendar = Calendar.getInstance(); calendar.setTime(beg); int count = 0; Date now = calendar.getTime(); while (now.before(end)) { calendar.add(intervalType, 1); now = calendar.getTime(); if (dateRule.include(now)) { ++count; } } return count; } } interface DateRule{ boolean include(Date d); } class ExcludeWeekendDateRule implements DateRule { public boolean include(Date d) { boolean include = true; Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); if ((dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) include = false; return include; } } class IncludeAllDateRule implements DateRule { public boolean include(Date d) { return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload