Date Formatter 和 ParseException - 从 Java 迁移到 C#

本文关键字:Java 迁移 Formatter ParseException Date | 更新日期: 2023-09-27 18:33:50

这是我的C#类的更新版本,我收到消息:

检测到无法访问的代码:DateTime.Now.Ticks;

其他一切似乎都还可以。我的错误在哪里?

using System;
using System.Globalization;
namespace domain
{
    public class Request : Unique
    {
        private DateTime dateBooked_Renamed;
        private DateTime dateSignedIn ;
        private DateTime dateSignedOut ;
        private bool arrived_Renamed = false;
        private bool paid_Renamed = false;
        private Hotel hotel_Renamed;
        private Employee employee_Renamed;
        private int numberOfNights;
        public Request(Hotel hotel, Employee employee, string dateBooked, string dateArrived, int numberOfNights)
            : base()
        {
            this.hotel_Renamed = hotel;
            this.employee_Renamed = employee;
            this.numberOfNights = numberOfNights;
            try
            {
                DateTime.ParseExact(dateBooked, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                this.dateBooked_Renamed = DateTime.ParseExact(dateBooked, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                this.dateSignedIn = DateTime.ParseExact(dateArrived, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
        }
        public virtual bool checkout(double money, string date)
        {
            try
            {
                dateCheckedOut = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
            if (money >= price())
            {
                paid = true;
            }
            else
            {
                paid = false;
            }
            return paid_Renamed;
        }
        public virtual int days()
        {
            long tNow = dateSignedOut != null ? dateSignedOut.Ticks : DateTime.Now.Ticks; 
            long tThen = arrived ? dateArrived.Ticks : tNow;
            long diff = tNow - tThen;
            long secondInMillis = 1000;
            long minuteInMillis = secondInMillis * 60;
            long hourInMillis = minuteInMillis * 60;
            long dayInMillis = hourInMillis * 24;
            long yearInMillis = dayInMillis * 365;
            diff = diff % yearInMillis;
            long elapsedDays = diff / dayInMillis;
            return (int)elapsedDays;
        }
        public virtual Hotel hotel
        {
            get
            {
                return hotel_Renamed;
            }
            set
            {
                this.hotel_Renamed = value;
            }
        }
        public virtual DateTime dateArrived
        {
            get
            {
                return dateSignedIn;
            }
            set
            {
                this.dateSignedIn = value;
            }
        }
        public virtual DateTime dateBooked
        {
            get
            {
                return dateBooked_Renamed;
            }
            set
            {
                this.dateBooked_Renamed = value;
            }
        }
        public virtual DateTime dateCheckedOut
        {
            get
            {
                return dateSignedOut;
            }
            set
            {
                this.dateSignedOut = value;
            }
        }
        public virtual Employee employee
        {
            get
            {
                return employee_Renamed;
            }
            set
            {
                this.employee_Renamed = value;
            }
        }
        public virtual bool arrived
        {
            get
            {
                return arrived_Renamed;
            }
            set
            {
                this.arrived_Renamed = value;
            }
        }
        public virtual bool paid
        {
            get
            {
                return paid_Renamed;
            }
            set
            {
                this.paid_Renamed = value;
            }
        }
        public virtual double price()
        {
            // The total cost of the stay...
            double price = days() * employee_Renamed.type.price;
            if (hotel_Renamed.premium)
            {
                return price - (0.2 * price);
            }
            else
            {
                return price;
            }
        }

        public virtual bool hasArrivalDate(string date)
        {
            try
            {
                DateTime d = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                Console.WriteLine("Comparing " + d + " with " + dateSignedIn + " = " + dateSignedIn.ToString("dd/MM/yyyy").Equals(d.ToString("dd/MM/yyyy")));
                return dateSignedIn.ToString().Equals(d.ToString());
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return false;
            }
        }
    }
}

Date Formatter 和 ParseException - 从 Java 迁移到 C#

如果您的日期字符串采用标准格式,则可以使用 DateTime.Parse(date)
如果没有,您可以使用DateTime.ParseExact(date,"your custom format",CultureInfo.InvariantCulture)

作为代码中的示例

public virtual bool hasArrivalDate(string date)
        {
            try
            {
                DateTime d = DateTime.ParseExact(date,"dd/MM/yyyy",CultureInfo.InvariantCulture);
                Console.WriteLine("Comparing " + d.ToString("dd/MM/yyyy") + " with " + dateSignedIn.ToString("dd/MM/yyyy") + " = " + dateSignedIn.ToString("dd/MM/yyyy").Equals(d.ToString("dd/MM/yyyy")));
                return dateSignedIn.ToString().Equals(d.ToString());
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return false;
            }
        }

所以,它不需要日期格式的实例

在您的代码中,dateSignedOut 变量是一个 DateTime ,它永远不能为空。

因此,在这一行中:

long tNow = dateSignedOut != null ? dateSignedOut.Ticks : DateTime.Now.Ticks;

dateSignedOut 永远不会为空,因此永远不会到达语句的"else"部分。

如果希望 dateSignedOut 可为空,请将其设为Nullable<DateTime>(或DateTime?)。

顺便说一句,请格式化您的问题,使其更具可读性。 这个确实有被关闭的危险。 更好的问题会带来更好的答案。

根据评论进行更新

您可能应该尝试更多地了解属性通常如何用 C# 编写,但这超出了本问题的范围。 要使用已有的代码,请执行以下操作:

private DateTime? dateSignedOut;  // Notice the ? to make it Nullable
long tNow = dateSignedOut.HasValue ? dateSignedOut.Ticks : DateTime.Now.Ticks;

看看一些MSDN文档,或Google Nullable类型。