Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:54:08+00:00 2026-05-31T11:54:08+00:00

I am able to add 102 days to any date I input, but now

  • 0

I am able to add 102 days to any date I input, but now the problem is, it should be 102 week days (excluding weekends). How can I do that?

Here’s my code for just adding 102 days:

private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
    try {    
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(txtStart.getText());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
        Calendar cal  = Calendar.getInstance();
        cal.setTime(date1);
        cal.add(Calendar.DATE, 102);
        String expDateString = sdf.format(cal.getTime());
        txtExpiry.setText(expDateString);
    } catch (ParseException ex) {
        Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
    } 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:54:09+00:00Added an answer on May 31, 2026 at 11:54 am

    This should add weekdays week days (weekends are ignored) onto a date. If you pass 0 for weekdays it will get the next working day. So, Saturday + 1 weekday = Tuesday.

    private static Date addWeekdaysToDate(Date date, int weekdays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int originalDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int numWeeks = weekdays / 5;
        int remainderDays = weekdays % 5;
        cal.add(Calendar.DAY_OF_MONTH, numWeeks * 7 + remainderDays);
    
        int adjustmentDays = 0;
        if (originalDayOfWeek == Calendar.SUNDAY) {
            adjustmentDays = 1;
        } else if (originalDayOfWeek + remainderDays > Calendar.FRIDAY) {
            adjustmentDays = 2;
        }
        cal.add(Calendar.DAY_OF_MONTH, adjustmentDays);
        return cal.getTime();
    }
    

    EDIT:

    In your code, just replace with the following:

    private static Date addWeekdaysToDate(Date date, int weekdays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int originalDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int numWeeks = weekdays / 5;
        int remainderDays = weekdays % 5;
        cal.add(Calendar.DAY_OF_MONTH, numWeeks * 7 + remainderDays);
    
        int adjustmentDays = 0;
        if (originalDayOfWeek == Calendar.SUNDAY) {
            adjustmentDays = 1;
        } else if (originalDayOfWeek + remainderDays > Calendar.FRIDAY) {
            adjustmentDays = 2;
        }
        cal.add(Calendar.DAY_OF_MONTH, adjustmentDays);
        return cal.getTime();
    }
    
    private static SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
        try {
            txtExpiry.setText(
                inputDateFormat.format(
                    addWeekdaysToDate(inputDateFormat.parse(txtStart.getText()), 102)
                )
            );
        } catch (ParseException ex) {
            Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was able to add a timestamp (the question/problem below)... but now I'm having
I am able to add data, but not sure how should I update the
I'm not able to add breakpoints to any Java file. It seems this problem
I want to be able to add and update certain information. Now it was
I have a feeling I should be able add a directory to the PATH
I am loading images externally using jQuery load function.. but not able add light
I love being able to add my own context menus to my application but
I'm able to add contact to address book now after adding the contact whil
I need to be able to add a decimal from input to a decimal
According to add to page documentation you should be able to add an app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.