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 7608117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:50:11+00:00 2026-05-31T00:50:11+00:00

I am trying to create some code in Java where someone can determine the

  • 0

I am trying to create some code in Java where someone can determine the date of the next recurring time of the week specified. This is hard to explain so I’ll give an example. Say it is March 1st (a Thursday) and the user wants to know when the next Saturday 5:00 is the code should output March 3rd, 5:00 as a Date variable and if it is March 4th, the program should output March 10th and so on…

The user however, can specify what time of the week they want. This is done with a long value which offsets the time of the week from Thursday 0:00 in milliseconds. I’m having trouble wrapping my head around this but this is what I got so far:

public static long findNextTimeOfTheWeek(long offset)
{
    return System.currentTimeMillis() - ((System.currentTimeMillis() - offset) % 604800000) + 604800000;
}

If anyone could help, it would be greatly appreciated. And if any more clarification is needed, just ask. P.S. 604800000 is the number of milliseconds in a week.

  • 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-31T00:50:12+00:00Added an answer on May 31, 2026 at 12:50 am

    I suggest you to use Calendar class, you can easily manipulate it and then get a Date object using Calendar#getTime. See the example below, it simply do what you want:

    import java.util.Calendar;
    import java.util.Locale;
    import java.util.Scanner;
    
    public class Main {
    
        public static int dayOfWeekIWant(final String dayOfWeekIWantString)
                throws Exception {
    
            final int dayOfWeekIWant;
    
            if ("sunday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.SUNDAY;
            } else if ("monday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.MONDAY;
            } else if ("tuesday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.TUESDAY;
            } else if ("wednesday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.WEDNESDAY;
            } else if ("thursday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.THURSDAY;
            } else if ("friday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.FRIDAY;
            } else if ("saturday".equalsIgnoreCase(dayOfWeekIWantString)) {
                dayOfWeekIWant = Calendar.SATURDAY;
            } else {
                throw new Exception(
                        "Invalid input, it must be \"DAY_OF_WEEK HOUR\"");
            }
    
            return dayOfWeekIWant;
        }
    
        public static String getOrdinal(int cardinal) {
            String ordinal = String.valueOf(cardinal);
    
            switch ((cardinal % 10)) {
            case 1:
                ordinal += "st";
                break;
            case 2:
                ordinal += "nd";
                break;
            case 3:
                ordinal += "rd";
                break;
            default:
                ordinal += "th";
            }
    
            return ordinal;
    
        }
    
        public static void printDate(Calendar calendar) {
            String dayOfWeek = calendar.getDisplayName(Calendar.DAY_OF_WEEK,
                    Calendar.LONG, Locale.ENGLISH);
            String dayOfMonth = getOrdinal(calendar.get(Calendar.DAY_OF_MONTH));
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            System.out.printf("%s %s %02d:%02d", dayOfWeek, dayOfMonth, hour, minute);
        }
    
        public static void main(String[] args) {
    
            Calendar calendar = Calendar.getInstance();
            Scanner scanner = new Scanner(System.in);
    
            final int today = calendar.get(Calendar.DAY_OF_WEEK);
    
            System.out.println("Please, input the day of week you want to know:");
    
            boolean inputOk = false;
    
            while (!inputOk)
    
                try {
                    String input = scanner.nextLine();
                    String[] split = input.split("\\s");
                    String dayOfWeekIWantString = split[0];
    
                    String[] splitHour = split[1].split(":");
                    int hour = Integer.parseInt(splitHour[0]);
                    int minute = Integer.parseInt(splitHour[1]);
    
                    int dayOfWeekIWant = dayOfWeekIWant(dayOfWeekIWantString);
    
                    int diff = (today >= dayOfWeekIWant) ? (7 - (today - dayOfWeekIWant))
                            : (dayOfWeekIWant - today);
    
                    calendar.add(Calendar.DAY_OF_WEEK, diff);
                    calendar.set(Calendar.HOUR_OF_DAY, hour);
                    calendar.set(Calendar.MINUTE, minute);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
    
                    inputOk = true;
    
                } catch (Exception e) {
    
                    // Using a generic exception just for explanation purpose
                    // You must create a specific Exception
                    System.out
                            .println("Invalid input, you must enter a valid input in format: \"DAY_OF_WEEK HOUR\"");
                    continue;
                }
    
            printDate(calendar);
    
        }
    }
    

    More Information:

    • Calendar class documentation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across this in some code - basically someone trying to create
I am trying to create some MySQL code that will invoke a Java program
today I am basically trying to create some JAVA code that moves an actor
I am trying to create a countdown using javascript. I got some code from
I've recently been trying to create units tests for some legacy code. I've been
I'm trying to write some code in java to learn more about coding with
Im trying to unit test a class in some Java code that I've inherited.
I am trying to create a generic container (e.g. java code below), which has
I'm trying to convert some python code to java and need to setup a
I'm trying to port some code from Play Framework Java to Play Framework Scala

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.