0

我有一个试图转换为 C# 的 Java 应用程序。我已经解决了相当多的程序,但我有一个让我烦恼的明确方法:

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

好吧,我认为最上面的 for 循环才是真正的问题。我想删除已经预订的预订时间。

这是我的第一个 for 循环试用:

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!");
        }
    }

希望你能清除我的视线。我只是失去了注意力。我知道我在网上看到的 - datetimepicker 不是那么容易编辑。但是然后我只需将已预订的项目编辑为“已预订”之类的内容。

4

2 回答 2

0

结果是这样的:

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!");
        }
    }

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

最亲切的问候拉斯穆斯

于 2013-07-16T12:30:18.320 回答
0

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

假设在 c# 中ReservationTime是一个DateTime属性,并且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类具有毫秒的分辨率,因此删除毫秒会将值截断为秒。
  • DateTime在 .Net 中对勾号有分辨率。我的滴答声是 100 纳秒,所以一秒钟有 10,000,000 个滴答声。
  • 因此,您不能只清除毫秒数,您必须通过计算小于一秒的剩余刻度来清除。然后从原始值中减去这些,得到相同的结果。
  • DateTime在 .Net 中是不可变的,因此您不能只更改一个属性。您必须计算一个新值,然后将其分配回原始变量。
于 2013-07-15T23:32:17.030 回答