C#日期时间选择器清除特定项目

本文关键字:项目 清除 选择器 日期 时间 | 更新日期: 2023-09-27 18:22:41

我有一个Java应用程序正在尝试转换为C#。我已经解决了相当多的程序,但我有一个明确的方法困扰着我:

private void checkCourts()
{
    if (splMonth.getSelectedValue() != null && splDate.getSelectedValue() != null)
    {
        courtModel.clear();
        Calendar booking = new GregorianCalendar();
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = new Scanner(splMonth.getSelectedValue().toString()).nextInt() - 1;
        int date = new Scanner(splDate.getSelectedValue().toString()).nextInt();
        int time = Integer.parseInt(cmbxTime.getSelectedItem().toString());
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
        int currentDate = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        int currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        booking.set(year, month, date, time, 0, 0);
        if (month > currentMonth || (month == currentMonth && date > currentDate) || (month == currentMonth && date == currentDate && time > currentTime))
        {
            try
            {
                ArrayList<Reservation> rs = BookingManager.getInstance().getReservations();
                Reservation r = new Reservation(booking);
                ArrayList<String> courtNames = BookingManager.getInstance().getCourtsName();
                for (int i = 0; i < rs.size(); i++)
                {
                    r.getReservationTime().clear(Calendar.MILLISECOND);
                    rs.get(i).getReservationTime().clear(Calendar.MILLISECOND);
                }
                if (!rs.contains(r))
                {
                    for (String c : courtNames)
                    {
                        courtModel.addElement(c);
                    }
                }
                else
                {
                    for (String c : courtNames)
                    {
                        courtModel.addElement(c);
                    }
                    for (int i = 0; i < rs.size(); i++)
                    {
                        if (r.getReservationTime().getTime().equals(rs.get(i).getReservationTime().getTime()))
                        {
                            String courtName = BookingManager.getInstance().getNameById(rs.get(i).getCourtId());
                            courtModel.removeElement(courtName);
                        }
                    }
                }
                splCourt.setModel(courtModel);
            }
            catch (Exception e)
            {
                System.out.println("ERROR - " + e.getMessage());
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Den valgte dato er ikke tilgængelig for booking.", "Advarsel", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

嗯,我认为循环的顶部才是真正的问题。我想删除已经预订的预订时间。

这是我第一次尝试循环:

private void checkCourts()
    {
        DateTime current = DateTime.Now;
        int year = Int32.Parse(DateTimePicker.Value.ToString("yyyy"));
        int currentYear = current.Year;
        int month = Int32.Parse(DateTimePicker.Value.ToString("MM"));
        int currentMonth = current.Month;
        int day = Int32.Parse(DateTimePicker.Value.ToString("dd"));
        int currentDay = current.Day;
        int time = (int)cmbxTime.SelectedItem;
        int currentTime = current.TimeOfDay.Hours;
        string date1 = year.ToString() + "," + month.ToString() + "," + day.ToString();
        DateTime thisdate = DateTime.Parse(date1);
        thisdate = thisdate.AddHours(time);
        List<Reservation> rs = BookingManager.getInstance().getReservations();
        Reservation r = new Reservation(thisdate);
        List<string> courtNames = BookingManager.getInstance().getCourtsName();
        if (month > currentMonth || (month == currentMonth && day > currentDay) ||
            (month == currentMonth && day == currentDay && time > currentTime) && year >= currentYear)
        {
            try
            {
                for (int i = 0; i < rs.Count; i++)
                {
                    r.ReservationTime = r.ReservationTime.AddTicks(-r.ReservationTime.Ticks % 10000000);
                    rs[i].ReservationTime = rs[i].ReservationTime.AddTicks(-rs[i].ReservationTime.Ticks % 10000000);
                }
                if (!rs.Contains(r))
                {
                    foreach (string c in courtNames)
                    {
                        lboxCourts.Items.Add(c);
                    }
                }
                else
                {
                    foreach (string c in courtNames)
                    {
                        lboxCourts.Items.Add(c);
                    }
                    for (int i = 0; i < rs.Count; i++)
                    {
                        if (r.ReservationTime.Equals(rs[i].ReservationTime))
                        {
                            String courtName = BookingManager.getInstance().getNameById(rs[i].CourtId);
                            lboxCourts.Items.Remove(courtName);
                            MessageBox.Show("is equal");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        else
        {
            MessageBox.Show("Den valgte dato er ikke gyldig! - vær opmærksom på at hvis du vælger dags dato, at tidspunktet ikke kan være tidligere end nuværende tidspunkt!");
        }
    }

希望你能帮我澄清一下。。我只是失去了注意力。我知道我在网上看到的情况——日期时间选择器并不是那么容易编辑。但随后我会将已预订的项目编辑为类似"已预订"的内容。

C#日期时间选择器清除特定项目

根据文档,Java代码.clear(Calendar.MILLISECOND)只是从值中删除任何毫秒。它不会对您的应用程序逻辑做任何事情来删除实际的预订时间。它似乎也不涉及任何类型的DateTimePicker

假设在c#中,ReservationTimeDateTime属性,而r.ReservationTime是与rs[i].ReservationTime不同的属性,则需要执行以下操作:

for (int i = 0; i < rs.Count; i++)
{
   r.ReservationTime = r.ReservationTime.AddTicks(-r.ReservationTime.Ticks % 10000000);
   rs[i].ReservationTime = rs[i].ReservationTime.AddTicks(-rs[i].ReservationTime.Ticks % 10000000);
}

有几点需要注意:

  • Java的Calendar类的分辨率为毫秒,因此删除毫秒等于将值截断为秒
  • .Net中的DateTime具有刻度的分辨率。我的刻度是100纳秒,所以一秒钟有10000000个刻度
  • 因此,你不能只清除毫秒,你必须通过计算小于一秒的剩余时间来清除。然后从原始值中减去这些值,得到相同的结果
  • .Net中的DateTime是不可变的,所以不能只更改一个属性。您必须计算一个新值,然后将其分配回原始变量

结果是这样的:

for (int i = 0; i < rs.Count; i++)
                {
                    r.ReservationTime = r.ReservationTime;
                    rs[i].ReservationTime = DateTime.Parse(rs[i].ReservationTime.ToString());
                    if (thisdate.CompareTo(rs[i].ReservationTime) != 0)
                    {
                        foreach (string c in courtNames)
                        {
                            lboxCourts.Items.Add(c);
                        }
                    }
                    else
                    {
                        lboxCourts.Items.Clear();
                        foreach (string c in courtNames)
                        {
                            lboxCourts.Items.Add(c);
                        }
                        for (int j = 0; j < rs.Count; j++)
                        {
                            if (r.ReservationTime.Equals(rs[j].ReservationTime))
                            {
                                string courtName = BookingManager.getInstance().getNameById(rs[j].CourtId);
                                lboxCourts.Items.Remove(courtName);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        else
        {
            MessageBox.Show("Den valgte dato er ikke gyldig! - vær opmærksom på at hvis du vælger dags dato, at tidspunktet ikke kan være tidligere end nuværende tidspunkt!");
        }
    }

谢谢你的帮助。。。现在我的方法是删除已经预订的球场;-)

致以最诚挚的问候Rasmus