Android时间插槽

时间:2016-12-26 09:59:40

标签: android user-interface datepicker

我有这种方法可以在我的应用中RecyclerView绑定时间段,但我想要其他的东西

private ArrayList<String> getTimeSet() {
    ArrayList results = new ArrayList<String>();
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    for (int i = 0; i < 9; i++) {
       Calendar calendar = new GregorianCalendar();
       calendar.add(Calendar.HOUR_OF_DAY, i);
       String day = sdf.format(calendar.getTime());
       results.add(i, day);
    }
    return results;
}

enter image description here

这是上面代码行的结果

但是我希望这样的事情发生变化,即应用程序不会从当前时间开始,例如,如果它是4点,那么时间段必须从4点钟开始,建议我们 enter image description here

2 个答案:

答案 0 :(得分:0)

我修改了你的方法。请检查一下。 只需传递布尔值即可检查当天是否为当天。

private ArrayList<String> getTimeSet(boolean isCurrentDay) {
    ArrayList results = new ArrayList<String>();
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    for (int i = 0; i < 9; i++) {
        Calendar calendar = new GregorianCalendar();
        if(!isCurrentDay)
            calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.add(Calendar.HOUR_OF_DAY, i);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String day1 = sdf.format(calendar.getTime());
        calendar.add(Calendar.HOUR, 1);
        String day2 = sdf.format(calendar.getTime());
        String day = day1 + " - " + day2;
        results.add(i, day);
    }
    return results;
}

您可以使用此方法获取第二天的时段。但是,如果您将当前日期布尔值传递给上述方法并检查日期是否为今天或下一天会更好。

private ArrayList<String> getNextDayTimeSet() {
    ArrayList results = new ArrayList<String>();
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    for (int i = 0; i < 9; i++) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 9); //Added this line only
        calendar.add(Calendar.HOUR_OF_DAY, i);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String day1 = sdf.format(calendar.getTime());
        calendar.add(Calendar.HOUR, 1);
        String day2 = sdf.format(calendar.getTime());
        String day = day1 + " - " + day2;
        results.add(i, day);
    }
    return results;
}

答案 1 :(得分:0)

我创建了一个用于生成时隙和日期位的类。试试代码,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Neeraj
 */
public class DateSlotClass {

    public static void main(String[] args) {
        ArrayList<String> dateList = getDataSet();
        System.out.println(dateList);
        for (String string : dateList) {
            System.out.println(getTimeSet(string));
        }
    }

    /**
     * Generates the time slots based on the date provided
     *
     * @param dateString the chosen date
     * @return list of times
     */
    private static ArrayList<String> getTimeSet(String dateString) {
        ArrayList results = new ArrayList();
        try {
            SimpleDateFormat inSdf = new SimpleDateFormat("EEE : dd-MMM");
            Date inDate = inSdf.parse(dateString);
            Calendar cal = Calendar.getInstance();
            cal.setTime(inDate);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
            cal.set(Calendar.HOUR, Calendar.getInstance().get(Calendar.HOUR) + 1);

            if (!isTodaysDate(cal)) {
                cal = Calendar.getInstance();
                cal.set(Calendar.HOUR, 9);
                cal.set(Calendar.MINUTE, 0);
            }

            results = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
            for (int i = 0; i < 9; i++) {
                String time1 = sdf.format(cal.getTime());
                cal.add(Calendar.HOUR, 1);
                String time2 = sdf.format(cal.getTime());
                String day = time1 + " - " + time2;
                results.add(i, day);
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return results;
    }

    /**
     * Test if the given date is today
     *
     * @param input the chosen date
     * @return true if date is todays
     */
    private static boolean isTodaysDate(Calendar input) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

        String inString = sdf.format(input.getTime());

        Calendar today = Calendar.getInstance();
        String todayString = sdf.format(today.getTime());

        return inString.equals(todayString);
    }

    /**
     * Generates the date slots based on the date provided
     *
     * @param dateString the chosen date
     * @return list of dates
     */
    private static ArrayList<String> getDataSet() {
        ArrayList results = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE : dd-MMM");
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 7; i++) {
            String day = sdf.format(calendar.getTime());
            results.add(i, day);
            calendar.add(Calendar.DATE, 1);
        }
        return results;
    }

}