本文介绍了查找下一个星期一6:00的Java日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java中创建一些代码,有人可以确定指定周的下一个周期性时间的日期。这很难解释,所以我举一个例子。说是3月1日(星期四),用户想知道下个星期六5:00的代码是否应该输出3月3日,5:00作为Date变量,如果是3月4日,则程序应该输出3月10日等等...



然而,用户可以指定他们想要的一周中的几个时间。这是通过一个长的值来实现的,该值可以从星期四0:00(以毫秒为单位)抵消一周的时间。我在遇到这个问题时遇到了麻烦,但这是我到目前为止所得到的:

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

如果有人可以帮助,将不胜感激。如果需要更多的澄清,请问。美国604800000是一周内的毫秒数。

解决方案

我建议您使用 Calendar class,你可以轻松地操作它,然后使用 Calendar#getTime 获取一个 Date 对象。看下面的例子,它只是做你想要的:

  import java.util.Calendar; 
import java.util.Locale;
import java.util.Scanner;

public class Main {

public static int dayOfWeekIWant(final String dayOfWeekIWantString)
throws异常{

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(
无效的输入,它必须是\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;
案例2:
ordinal + =nd;
break;
案例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 。英语);
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.getInstance();
扫描仪扫描仪=新扫描仪(System.in);

final int today = calendar.get(Calendar.DAY_OF_WEEK);

System.out.println(请输入你想知道的星期几);

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 =(今天> = dayOfWeekIWant)? (7 - (今天 - 周日))
:(dayOfWeekIWant - 今天);

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(异常e){

//使用通用异常仅用于说明目的
//您必须创建一个特定的异常
系统.out
.println(输入无效,您必须输入格式有效的输入:\DAY_OF_WEEK HOUR\);
继续;
}

printDate(calendar);

}
}

更多信息:






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.

解决方案

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:

这篇关于查找下一个星期一6:00的Java日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:12