0

我在 android.On 中做 dateTimePicker 应用程序,我需要在特定 dateTime.Ex 的时间前后获得 10 分钟。如果用户选择“12/12/2010 5:00”,我需要将“12/12/2010 4:50”作为一个值和“12/12/2010 5:10”作为一个值。我该怎么做+ 10 分钟和 -10 分钟从特定时间开始?

我的代码:

String DATETIMEBEFORE ;
String DATETIMEAFTER;
...
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int selectedHour,
                int selectedMinute) {
            hour = selectedHour;
            minute = selectedMinute;

            String DATETIME = new StringBuilder()
                    // Month is 0 based, just add 1
                    .append(month + 1).append("-").append(day).append("-")
                    .append(year).append(" ").append(pad(hour)).append(":")
                    .append(pad(minute)).toString();

如果 DATETIME 的值 =“12/12/2010 5:00”。我需要获取 DATETIMEBEFORE 值 =“12/12/2010 4:50”和 DATETIMEAFTER 值 =“12/12/2010 5:10”。谢谢。

4

2 回答 2

1

OPs 代码后更新

public static String getTimeBefore10minutes(String currentDate) {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        try {
            // Get calendar set to current date and time with Singapore time zone
            Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Calcutta"));
            calendar.setTime(format.parse(currentDate));

            //Set calendar before 10 minutes
            calendar.add(Calendar.MINUTE, -10);     
            //Formatter 
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            formatter.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));  
            return formatter.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public static String getTimeAfter10minutes(String currentDate) {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        try {
            // Get calendar set to current date and time with Singapore time zone
            Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Calcutta"));
            calendar.setTime(format.parse(currentDate));

            //Set calendar before 10 minutes
            calendar.add(Calendar.MINUTE, 10);     
            //Formatter 
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            formatter.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));  
            return formatter.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

并打电话给

getTimeAfter10minutes("12/12/2010 5:00");
getTimeBefore10minutes("12/12/2010 5:00");

结果将是

12/12/2010 05:10
12/12/2010 04:50

阅读有关我的博客的更多信息计算日期范围

于 2013-07-18T05:31:42.540 回答
0

如果您有以英里为单位的选定时间,

long now = System.currentTimeMillis(); // or any time value in miliseconds
long tenMinuteBefore = now - 600000;
long tenMinuteAfter  = now + 600000;
于 2013-07-18T05:36:41.230 回答