查找指定一周中第一天的特定日期的星期几

时间:2015-05-05 06:16:31

标签: java android date jodatime

我正在实现一个类似于VB Weekday函数的方法,它可以给出工作日编号,给出任何星期几作为一周的开始。

对于“2010-02-16”(星期二)和星期日(1)的第一天的例子,我期望值3作为结果。

3 个答案:

答案 0 :(得分:1)

这可以在?- list_oddies(Xs,Ys). Xs = [], Ys = [] ; Xs = [_A], Ys = [_A] ; Xs = [_A,_B], Ys = [_A] ; Xs = [_A,_B,_C], Ys = [_A,_C] ; Xs = [_A,_B,_C,_D], Ys = [_A,_C] ; Xs = [_A,_B,_C,_D,_E], Ys = [_A,_C,_E] ; Xs = [_A,_B,_C,_D,_E,_F], Ys = [_A,_C,_E] ; Xs = [_A,_B,_C,_D,_E,_F,_G], Ys = [_A,_C,_E,_G] ; Xs = [_A,_B,_C,_D,_E,_F,_G,_H], Ys = [_A,_C,_E,_G] ... 课程的帮助下进行,但很少计算。

  1. 解析日期
  2. 获取日历实例并使用已解析日期
  3. 对其进行初始化
  4. 获取当时的星期数
  5. 计算星期几w.r.t。通过执行@Markus指出的计算
  6. 来启动

    以下方法证明:

    Calendar

    更新:感谢@Markus的评论

答案 1 :(得分:0)

下面接受以字符串形式提供的日期,并以ISO-8601扩展本地日期格式(例如2010-02-16)格式化。星期几是使用DayOfWeek枚号。但请注意,这取决于Java 8(标准库,而不是Joda-Time)。使用LocalDate对于避免TimeZone和时间问题(部分日期)很重要。

UPDATE:还提供了一个模拟VB版本的静态方法,其中包含日期的整数参数。除0以外的所有内容(基于NLS的系统默认值)均被接受。

更新#2:必须将previous修改为previousOrSame,否则我们会得到8而不是1。结果预计在1-7范围内。

import java.time.DayOfWeek;
import java.time.LocalDate;
import static java.time.DayOfWeek.*;
import static java.time.temporal.ChronoUnit.*;
import static java.time.temporal.TemporalAdjusters.*;

public class Main {
  public static void main(String[] args) {
    // Thanks to @Markus for the testcase
    System.out.println(getVbWeekday("2010-02-16", 1));
    System.out.println(getVbWeekday("2015-05-03", 1));
    System.out.println(getVbWeekday("2015-05-04", 1));
    System.out.println(getVbWeekday("2015-05-05", 1));
    System.out.println(getVbWeekday("2015-05-06", 1));
    System.out.println(getVbWeekday("2015-05-07", 1));
    System.out.println(getVbWeekday("2015-05-08", 1));
    System.out.println(getVbWeekday("2015-05-09", 1));
    System.out.println(getVbWeekday("2015-05-10", 1));
  }

  public static int getWeekdayNumber(String dateAsString,DayOfWeek firstDayOfWeek) {
    LocalDate d = LocalDate.parse(dateAsString);
     // add 1 because between is exclusive end date.
    return (int)DAYS.between(d.with(previousOrSame(firstDayOfWeek)),d)+1;
  }

  public static int getVbWeekday(String dateAsString,int firstDayOfWeek) {
    return getWeekdayNumber(dateAsString,SATURDAY.plus(firstDayOfWeek));
  }
}

答案 2 :(得分:0)

总结JoD的答案。和Asif Mujteba,应该是这样的:

import java.time.*;
import java.util.*;

public class SampleClass {

     public static void main(String []args) throws Exception {
        System.out.println(Weekday("2010-02-16", 1));
        System.out.println(Weekday("2015-05-03", 1));
        System.out.println(Weekday("2015-05-04", 1));
        System.out.println(Weekday("2015-05-05", 1));
        System.out.println(Weekday("2015-05-06", 1));
        System.out.println(Weekday("2015-05-07", 1));
        System.out.println(Weekday("2015-05-08", 1));
        System.out.println(Weekday("2015-05-09", 1));
        System.out.println(Weekday("2015-05-10", 1));
     }

    public static int Weekday(String dateString, int startDay) throws Exception {
        LocalDate date = LocalDate.parse(dateString);
        return (((date.getDayOfWeek().ordinal() - startDay + 2) % 7) + 1;
    }
}

这将返回:

3
1
2
3
4
5
6
7
1

这与DateAndTime.Weekday返回相同......