从字符串数组中根据迭代查找日期,使用Java。

huangapple go评论77阅读模式
英文:

Find day from String array based on iteration using Java

问题

我有一个字符串数组和以下输入

S = "Tue",K = 23

        String[] daysOfWeek = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

我将我的输入设置为 "Tue",以查找在 10 天后的下一个日期。我期望的答案是 "Fri"

以下是我的代码

String[] daysOfWeek = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
int day = Arrays.asList(daysOfWeek).indexOf(S);
int n = K % 7;
if ((day + n) > 6) {
    int index = (day + n) % 6;
    //return daysOfWeek[ ];

} else {
    return daysOfWeek[day + n];

}
return null;

有什么想法吗

英文:

I have my string array and the input as follows

S = "Tue" and K = 23

        String[] daysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

I give my input as "Tue" and to find the next day after 10 days. I'm expecting answer as "Fri"

Here is my code

        String[] daysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
    int day = Arrays.asList(daysOfWeek).indexOf(S);
    int n = K % 7;
    if((day + n) > 6) {
        int index = (day + n) % 6;
        //return daysOfWeek[ ];

    } else {
        return daysOfWeek[day + n ];

    }
    return null;

Any ideas

答案1

得分: 4

可以使用 java.time.DayOfWeek,而不是重新发明轮子。

例如:

DayOfWeek dayOfWeek = DayOfWeek.TUESDAY;
DayOfWeek dayOfWeek1 = dayOfWeek.plus(10);
System.out.println(dayOfWeek1); // FRIDAY
英文:

You can use java.time.DayOfWeek instead of reinventing the wheel.

For example:

DayOfWeek dayOfWeek = DayOfWeek.TUESDAY;
DayOfWeek dayOfWeek1 = dayOfWeek.plus(10);
System.out.println(dayOfWeek1); // FRIDAY

答案2

得分: 1

private static final String[] DAYS_OF_WEEK = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
private static final Map<String, Integer> MAP = Map.of(DAYS_OF_WEEK[0], 0, DAYS_OF_WEEK[1], 1,
        DAYS_OF_WEEK[2], 2, DAYS_OF_WEEK[3], 3, DAYS_OF_WEEK[4], 4, DAYS_OF_WEEK[5], 5, DAYS_OF_WEEK[6], 6);

public static String find(String dayOfWeek, int days) {
    return DAYS_OF_WEEK[(MAP.get(dayOfWeek) + days) % 7];    // NPE if not found
}
英文:

Be simple!

private static final String[] DAYS_OF_WEEK = { &quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot; };
private static final Map&lt;String, Integer&gt; MAP = Map.of(DAYS_OF_WEEK[0], 0, DAYS_OF_WEEK[1], 1,
        DAYS_OF_WEEK[2], 2, DAYS_OF_WEEK[3], 3, DAYS_OF_WEEK[4], 4, DAYS_OF_WEEK[5], 5, DAYS_OF_WEEK[6], 6);

public static String find(String dayOfWeek, int days) {
    return DAYS_OF_WEEK[(MAP.get(dayOfWeek) + days) % 7];    // NPE if not found
}

答案3

得分: 0

你可以对天数取模7,然后根据输出找到从起始位置移动到结果的索引。

英文:

You can do modulo 7 for the number of days after, and then you can find based on the output move the index to the result from the starting position.

答案4

得分: 0

混合方法 -

    String[] daysOfWeek = {"周一","周二","周三","周四","周五","周六","周日"};
    int today = LocalDate.now().getDayOfWeek().getValue()-1;
    for (int i = 0; i < 31; i++) {
        System.out.println(daysOfWeek[today] + " >>>     " + i + "  - " + daysOfWeek[(today+i)%7]);
    }
英文:

a mixed approach -

String[] daysOfWeek = {&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thu&quot;,&quot;Fri&quot;,&quot;Sat&quot;,&quot;Sun&quot;};
int today = LocalDate.now().getDayOfWeek().getValue()-1;
for (int i = 0; i &lt; 31; i++) {
    System.out.println(daysOfWeek[today] + &quot; &gt;&gt;&gt;     &quot; + i + &quot;  - &quot; + daysOfWeek[(today+i)%7]);
}

huangapple
  • 本文由 发表于 2020年9月9日 13:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定