如何判断是否使用java传递了一天

时间:2012-11-21 10:35:55

标签: java calendar

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

        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 个答案:

答案 0 :(得分:3)

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

我会做一个描述一周的哈希并总是指向哈希值“false”

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

不,爆炸你的字符串,你可以在散列中将该字符串中的所有出现设置为true。对于你的例子,这将是

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

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

答案 1 :(得分:1)

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

要使用SimpleDateFormatString方法)在Java中解析Dateparse

答案 2 :(得分: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

答案 3 :(得分:-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;
        }
    }
}