0

我有一个字符串,其中包含天的名称,我只想在那串天中看到它,也就是即将到来的一天。例如,字符串包含“Sun,Mon,Tue,Fri,Sat”,如果今天是星期三,它应该只返回 fri。任何人都知道如何做到这一点?目前我正在做这件事,但它似乎也回归了太阳,尽管星期天已经过去了。

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

    String nexday="sun,mon,tue,fri,sat";     
    String nexdayone[] = nexday.split(",");
    Calendar now = Calendar.getInstance();
    Calendar futurecal = Calendar.getInstance();
    for (int i = 0; i < nexdayone.length; i++) {
        for (int j = 0; j < 7; j++) {
            if (strDays[j].equalsIgnoreCase(nexdayone[i])) {

                futurecal.set(Calendar.DAY_OF_WEEK, j);
                if (futurecal.after(now)) {
                    Log.d(now.get(Calendar.DAY_OF_WEEK)+" --after-- "+j,""+strDays[j]);
                    break;
                }
            }
        }
    }
4

4 回答 4

3

所以你想在你的字符串中找到从今天开始的下一个工作日?

我会做一个描述一周的散列,并总是指向一个“假”的散列值

Sun->false
Mon->false
Tue->false
Wed->false
Thu->false
Fri->false
Sat->false

不,爆炸你的字符串,你可以在哈希中将该字符串中的所有出现设置为真。对于您的示例,这将是

Sun->true
Mon->true
Tue->true
Wed->false
Thu->false
Fri->true
Sat->true

现在您可以从当前的工作日(日历)开始并遍历哈希,直到您获得“真”的哈希值。实现真实的关键将是您寻找的工作日!

于 2012-11-21T10:46:22.247 回答
1

试试乔达时间。它是一个很棒的 java 日期/时间计算库。

StringDate在 Java 中解析,请使用SimpleDateFormat (parse方法)。

于 2012-11-21T10:39:09.323 回答
1

使用 SortedSet 查找第二天的解决方案。

private static SimpleDateFormat fmt = new SimpleDateFormat("E");

public static void main(String[] args) throws Exception {
    // Put the days in the list into a sorted set
    TreeSet<Integer> daySet = new TreeSet<Integer>();
    for (String day : "Sun,Mon,Tue,Fri,Sat".split(",")) {
        daySet.add(dayOfWeek(day));
    }

    // Find the next day in the list, starting from today
    Calendar cal = Calendar.getInstance();
    int today = cal.get(Calendar.DAY_OF_WEEK);
    cal.set(Calendar.DAY_OF_WEEK, findNext(daySet, today));
    System.out.println(fmt.format(cal.getTime()));
}

/**
 * Parses a day name, and returns the number of the day in the week.
 */
private static int dayOfWeek(String day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(fmt.parse(day, new ParsePosition(0)));
    return cal.get(Calendar.DAY_OF_WEEK);
}

/**
 * Finds a value in the sorted set that is greater than 'from'.
 * If there are no greater values, return the first value.
 */
private static int findNext(SortedSet<Integer> set, int from) {
    SortedSet<Integer> tail = set.tailSet(Integer.valueOf(from));
    return tail.isEmpty() ? set.first() : tail.first();
}

假设今天是星期二。我不确定是周二还是周五回来。上面的解决方案将打印“Tue”。如果您需要“星期五”,即不是今天的第二天,您可以使用:

Integer.valueOf(from + 1)findNext.

于 2012-11-21T11:01:45.197 回答
-1
Try this.

 public static void main(String a[]){
 String days [] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    Date d = new Date();
    SimpleDateFormat  st = new SimpleDateFormat ("EEEEE");
    String day = st.format(d);
    for(int i=0;i<days.length;i++){
        if(days[i].equals(day)){
            System.out.println(days[i+2]);
            break;
        }
    }
}
于 2012-11-21T10:49:13.853 回答