【问题标题】:Create 30 min slots from given date and time从给定的日期和时间创建 30 分钟的时段
【发布时间】:2023-03-23 08:24:01
【问题描述】:

我有时间在下面的格式中

2015-10-28T18:37:04.899+05:30

我必须创建以下格式的插槽

11.00AM - 12.00PM 28/10/15

12.00PM - 1.00PM 28/10/15

1.00PM - 2.00PM 28/10/15

String timeValue = "2015-10-28T18:37:04.899+05:30";
        StringTokenizer  stringTokenizer = new StringTokenizer(timeValue,"T");
        String dateValue = stringTokenizer.nextElement().toString();
        String endDateValue = "2015-10-30";
        String restString= stringTokenizer.nextElement().toString();
        StringTokenizer secondTokeniser = new StringTokenizer(restString,":");
        String hours = secondTokeniser.nextElement().toString();
        String minutes = secondTokeniser.nextElement().toString();
        hours = String.valueOf(Integer.parseInt(hours) + 2);
        if (Integer.parseInt(minutes) > 30){
            minutes = "00";
        }else{
            minutes = "30";
        }

        String amOrPm = null;
        if(Integer.parseInt(hours) < 12){
            amOrPm = "AM";
        }else{
            amOrPm = "PM";
            hours = String.valueOf(getHoursValue(Integer.parseInt(hours)));
        }
        String time1 = hours + ":" + minutes + " " + amOrPm;
        String time2 = "12" + ":" + "00" + " AM ";
        String format = "yyyy-mm-dd hh:mm a";


        SimpleDateFormat sdf = new SimpleDateFormat(format);

        try {
            Date dateObj1 = sdf.parse(dateValue + " " + time1);
            Date dateObj2 = sdf.parse(endDateValue + " " + time2);
            Logger.d(TAG, "Date Start: " + dateObj1);
            Logger.d(TAG, "Date End: " + dateObj2);
            long dif = dateObj1.getTime();
            while (dif < dateObj2.getTime()) {
                Date slot = new Date(dif);
                Log.d(TAG, "Hour slot = " + slot);
                dif += 1800000;
            }
        }catch (ParseException ex){
            ex.printStackTrace();
        }

通过上面的代码,我得到了以下输出

2015 年 1 月 28 日星期三 20:00:00 IST

2015 年 1 月 28 日星期三 20:30:00 IST

2015 年 1 月 28 日星期三 21:00:00 IST

2015 年 1 月 28 日星期三 21:30:00 IST

2015 年 1 月 28 日星期三 22:00:00 IST

2015 年 1 月 28 日星期三 22:30:00 IST

24 小时制

我想要下面的输出格式

11.00AM - 12.00PM 28/10/15

12.00PM - 1.00PM 28/10/15

1.00PM - 2.00PM 28/10/15

谁能帮我解决这个问题

【问题讨论】:

  • 我真的不明白downvote的意思,这已经成为一种趋势
  • 可能只是无缘无故投反对票的坏人
  • 是的,我猜他们喜欢兄弟
  • 是否有任何特定的登录来创建所需的输出!意味着无论日期如何,任何日期时间都将具有相同的时隙
  • @MiteshParmar ,不,没有任何特定的登录。是的,接下来的 1 天会有相同的时间段

标签: android date datetime simpledateformat


【解决方案1】:

我认为,这是您正在寻找的东西:

String timeValue = "2015-10-28T18:37:04.899+05:30";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(sdf.parse(timeValue));

    if (startCalendar.get(Calendar.MINUTE) < 30) {
        startCalendar.set(Calendar.MINUTE, 30);
    } else {
        startCalendar.add(Calendar.MINUTE, 30); // overstep hour and clear minutes
        startCalendar.clear(Calendar.MINUTE);
    }

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(startCalendar.getTime());

    // if you want dates for whole next day, uncomment next line
    //endCalendar.add(Calendar.DAY_OF_YEAR, 1);
    endCalendar.add(Calendar.HOUR_OF_DAY, 24 - startCalendar.get(Calendar.HOUR_OF_DAY));

    endCalendar.clear(Calendar.MINUTE);
    endCalendar.clear(Calendar.SECOND);
    endCalendar.clear(Calendar.MILLISECOND);

    SimpleDateFormat slotTime = new SimpleDateFormat("hh:mma");
    SimpleDateFormat slotDate = new SimpleDateFormat(", dd/MM/yy");
    while (endCalendar.after(startCalendar)) {
        String slotStartTime = slotTime.format(startCalendar.getTime());
        String slotStartDate = slotDate.format(startCalendar.getTime());

        startCalendar.add(Calendar.MINUTE, 30);
        String slotEndTime = slotTime.format(startCalendar.getTime());

        Log.d("DATE", slotStartTime + " - " + slotEndTime + slotStartDate);
    }

} catch (ParseException e) {
    // date in wrong format
}

我需要提一下,这段代码忽略了时区,但您似乎不需要它。示例输出:

cz.skywall.stack D/DATE: 07:00PM - 07:30PM, 28/10/15
cz.skywall.stack D/DATE: 07:30PM - 08:00PM, 28/10/15
cz.skywall.stack D/DATE: 08:00PM - 08:30PM, 28/10/15
cz.skywall.stack D/DATE: 08:30PM - 09:00PM, 28/10/15
cz.skywall.stack D/DATE: 09:00PM - 09:30PM, 28/10/15
cz.skywall.stack D/DATE: 09:30PM - 10:00PM, 28/10/15
cz.skywall.stack D/DATE: 10:00PM - 10:30PM, 28/10/15
cz.skywall.stack D/DATE: 10:30PM - 11:00PM, 28/10/15
cz.skywall.stack D/DATE: 11:00PM - 11:30PM, 28/10/15
cz.skywall.stack D/DATE: 11:30PM - 12:00AM, 28/10/15

正如我在代码中提到的,您可以通过取消注释行 endCalendar.add(Calendar.DAY_OF_YEAR, 1); 来生成整个第二天的插槽。

【讨论】:

  • 与其他解决方案相比,您的解决方案看起来整洁干净,一旦我测试代码就会接受。非常感谢您的回答
  • 所有场景的完美答案,我已经为所有场景测试了这个代码,包括 2015 年 12 月 31 日和月底和年底,它运行良好,我做了一些调整来完全满足我的要求,非常感谢
  • 完美生成时隙。但是我有一个小问题,我想在同一天从上午 12 点到晚上 11 点生成时段,但它从上午 12:30 开始。通过这个输入“2016-11-03 00:00:00”。
  • @Shyam 删除第一个 if (startCalendar.get(Calendar.MINUTE) &lt; 30) { ... } 语句应该倾向于请求的行为。
  • 我得到了另一个非常适合我要求的答案。感谢@skywall 的帮助。
【解决方案2】:

您可以使用以下内容,请注意对于月份您应该使用MM 而不是mm。我添加了一个简单的getHoursValue,因为我不知道你的。

希望这会有所帮助!

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displayTimeSlots();
    }

    private int getHoursValue(int hours){
        return hours - 12;
    }

    private void displayTimeSlots(){
        String timeValue = "2015-10-28T18:37:04.899+05:30";
        StringTokenizer stringTokenizer = new StringTokenizer(timeValue,"T");
        String dateValue = stringTokenizer.nextElement().toString();
        String endDateValue = "2015-10-30";
        String restString= stringTokenizer.nextElement().toString();
        StringTokenizer secondTokeniser = new StringTokenizer(restString,":");
        String hours = secondTokeniser.nextElement().toString();
        String minutes = secondTokeniser.nextElement().toString();
        hours = String.valueOf(Integer.parseInt(hours) + 2);
        if (Integer.parseInt(minutes) > 30){
            minutes = "00";
        }else{
            minutes = "30";
        }

        String amOrPm;
        if(Integer.parseInt(hours) < 12){
            amOrPm = "AM";
        }else{
            amOrPm = "PM";
            hours = String.valueOf(getHoursValue(Integer.parseInt(hours)));
        }
        String time1 = hours + ":" + minutes + " " + amOrPm;
        String time2 = "12" + ":" + "00" + " AM ";
        String format = "yyyy-MM-dd hh:mm a";

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        try {
            Date dateObj1 = sdf.parse(dateValue + " " + time1);
            Date dateObj2 = sdf.parse(endDateValue + " " + time2);
            Log.d("TAG", "Date Start: " + dateObj1);
            Log.d("TAG", "Date End: " + dateObj2);
            long dif = dateObj1.getTime();
            while (dif < dateObj2.getTime()) {
                Date slot1 = new Date(dif);
                dif += 3600000;
                Date slot2 = new Date(dif);
                dif += 3600000;
                SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm a");
                SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a, dd/MM/yy");
                Log.d("TAG", "Hour slot = " + sdf1.format(slot1) + " - " + sdf2.format(slot2));
            }
        }catch (ParseException ex){
            ex.printStackTrace();
        }
    }
}

这里是日志:

11-07 14:54:02.506 24299-24299/? D/TAG: Date Start: Wed Oct 28 20:00:00 GMT+07:00 2015
11-07 14:54:02.506 24299-24299/? D/TAG: Date End: Fri Oct 30 00:00:00 GMT+07:00 2015
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 08:00 PM - 09:00 PM, 28/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 10:00 PM - 11:00 PM, 28/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 12:00 AM - 01:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 02:00 AM - 03:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 04:00 AM - 05:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 06:00 AM - 07:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 08:00 AM - 09:00 AM, 29/10/15
11-07 14:54:02.527 24299-24299/? D/TAG: Hour slot = 10:00 AM - 11:00 AM, 29/10/15
11-07 14:54:02.537 24299-24299/? D/TAG: Hour slot = 12:00 PM - 01:00 PM, 29/10/15
11-07 14:54:02.537 24299-24299/com.example.timeslots D/TAG: Hour slot = 02:00 PM - 03:00 PM, 29/10/15
11-07 14:54:02.556 24299-24299/com.example.timeslots D/TAG: Hour slot = 04:00 PM - 05:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 06:00 PM - 07:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 08:00 PM - 09:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 10:00 PM - 11:00 PM, 29/10/15

【讨论】:

  • 不客气,你应该在接受之前测试。但是,您的问题标题Create 30 min slots,问题正文1-hour slots。所以,如果你想要 30 分钟,你可以用 1800000 替换 3600000 :)
  • 是的,我将替换为 180000 。再次感谢您,先生
  • 但是除了使用我们自己的逻辑之外,skywall 还使用日历给出了一个非常合适的答案,对不起,我只能给一个人赏金
  • 我明白了,不用担心赏金,我只是想确保我的回答是否适用于您的问题。原始代码是你的,我只是检查并修改它以使其正常工作:)
  • 是的,这是真的。我很抱歉
【解决方案3】:

你可以得到你的第一个时间间隔,类似的你可以找到其他两个剩余的时间间隔。

String time ="2015-10-28T18:37:04.899+05:30";

        try {
        Date d= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(time);
        SimpleDateFormat dateFormat1=new SimpleDateFormat("hh:mm aaa dd/MM/yy");
        String date =dateFormat1.format( d);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateFormat1.parse(date));
        calendar.add(Calendar.HOUR, 1);

        String one=dateFormat1.format(calendar.getTime());
        String[] strings=date.split(" ");
        one=(strings[0]+strings[1]+"-"+one);

        System.out.println("Time here "+one);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

【讨论】:

    【解决方案4】:
    <div id="rotu">hello</div>
    
    <script>
        var r = document.getElementById('rotu');
        for(let i=8;i<22;i++){
            if(i.toString().length === 1){
                i = '0'+i;
            }
            r.innerHTML += '<div>'+i+':00</div><div>'+i+':30</div>';
        }
    </script>
    

    【讨论】:

    • 口头解释通常很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2020-02-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多