0
votes

I have a string with name of days in it , i just want to see that in that string of days , which is the upcoming day. For example the string contains "Sun,Mon,Tue,Fri,Sat" and if today is Wednesday it should return fri only. Anyone got an idea how to do this? At the moment i am doing this thing but it seems to return sun too although sunday is already past.

        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
Today is Wednesday, and the upcoming day is Friday?? Is that so, because your String does not contain Wed,Thu ? - jlordo
@jlordo That is correct, because Thursday is not in the given string - Simon Lehmann
yes , i want to check which is the upcoming day out of the days present in string. - chossen-addict
And the day after Sat is Sun? - Peter Lawrey
well i am assuming that sunday is passed already in this week or it isnt? i have tried using cal.setFirstDayOfWeek(Calendar.SUNDAY); but the result are inconsistent - chossen-addict

4 Answers

3
votes

So you want to find the next weekday starting from today that ist listet in your string?

I would do a hash describing a week and always pointing to a hash-value of "false"

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

No, exploding your string, you could set all occurences in that string to true in the hash. For your example this would be

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

now you can start at the current weekday (calendar) and walk the hash until you get a hash-value of "true". The key delivering true will be your weekday you looked for!

1
votes

Try Joda Time. Its a great library for java date/time calculations.

To parse String to Date in Java use SimpleDateFormat (parse method).

1
votes

A solution that uses SortedSet to find the next day.

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();
}

Suppose it was Tuesday today. I wasn't sure whether to return Tuesday or Friday. The solution above would print "Tue". If you need "Fri", i.e. the next day that isn't today, you can use:

Integer.valueOf(from + 1) in findNext.

-1
votes
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;
        }
    }
}