Java 预约班
我正在尝试创建一个方法,将约会添加到arraylist appointmentCalndar。此方法将验证日期,以查看用户输入是否等于代码中的SimpleDateFormat、约会的开始时间和结束时间,并查看其是否在将来 我曾尝试使用JavaDateAPI来检查这一点,但当我尝试扩展类以访问属性时,它总是在编译时导致错误。所以总的来说,我的问题是,比较约会类型的对象和日期类型的对象的最佳方式是什么?我尝试使用ACCESOR获取日期()和开始时间以及结束时间,但它不允许我也获取它们Java 预约班,java,date,object,Java,Date,Object,我正在尝试创建一个方法,将约会添加到arraylist appointmentCalndar。此方法将验证日期,以查看用户输入是否等于代码中的SimpleDateFormat、约会的开始时间和结束时间,并查看其是否在将来 我曾尝试使用JavaDateAPI来检查这一点,但当我尝试扩展类以访问属性时,它总是在编译时导致错误。所以总的来说,我的问题是,比较约会类型的对象和日期类型的对象的最佳方式是什么?我尝试使用ACCESOR获取日期()和开始时间以及结束时间,但它不允许我也获取它们 public
public AppointmentDate(String appString)
{
// 1) split ithe string into Date/from/to
// 2) consturct the Date object for the appDate
// 3) consturct the Date object for the startTime
// 4) consturct the Date object for the endTime
String[] appDetails = appString.split(",");
if(appDetails.length == 2)
{
try {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
this.appDate = df.parse(appDetails[0]);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy,mm:HH");
String dFormat = appDetails[0] + "," + appDetails[1];
this.startTime = formatter.parse(dFormat);
dFormat = appDetails[0] + "," + appDetails[2];
this.endTime = formatter.parse(dFormat);
}
catch (Exception ex)
{
}
}
else
{
System.out.print("User Date is Invalid");
}
}
public void setStartTime(Date startTime)
{
this.startTime = startTime;
}
public Date getStartTime()
{
return startTime;
}
public void setEndTime(Date endTime)
{
this.endTime = endTime;
}
public Date getEndTime()
{
return endTime;
}
public void setAppdate(Date appDate)
{
this.appDate = appDate;
}
public Date getAppDate()
{
return appDate;
}
public void add(Appointment a)
{
if (a.equals(a.getDate()))
{
if(a.getStartTime() < a.getEndTime())
{
}
}
else
{
System.out.print("");
}
}
公共任命日期(字符串appString)
{
//1)将字符串拆分为日期/开始日期/结束日期
//2)构造appDate的日期对象
//3)构造startTime的日期对象
//4)构造endTime的日期对象
String[]appDetails=appString.split(“,”);
如果(appDetails.length==2)
{
试一试{
SimpleDataFormat df=新的SimpleDataFormat(“dd/MM/yyyy”);
this.appDate=df.parse(appDetails[0]);
DateFormat格式化程序=新的SimpleDateFormat(“dd/MM/yyyy,MM:HH”);
字符串dFormat=appDetails[0]+“,”+appDetails[1];
this.startTime=formatter.parse(dFormat);
dFormat=appDetails[0]+“,”+appDetails[2];
this.endTime=formatter.parse(dFormat);
}
捕获(例外情况除外)
{
}
}
其他的
{
系统输出打印(“用户日期无效”);
}
}
公共无效设置开始时间(日期开始时间)
{
this.startTime=startTime;
}
公共日期getStartTime()
{
返回起始时间;
}
公共无效设置结束时间(日期结束时间)
{
this.endTime=endTime;
}
公共日期getEndTime()
{
返回结束时间;
}
公共无效setAppdate(日期appDate)
{
this.appDate=appDate;
}
公共日期getAppDate()
{
返回应用日期;
}
公共无效添加(预约a)
{
如果(a.equals(a.getDate()))
{
if(a.getStartTime()
静态块(几乎)
您用于练习该类的代码位于错误的位置。你把它卡在类的顶部,这在语法上是不正确的。我们可以在顶部运行代码,但它需要标记为static{…}
。根据我的经验,静态块并不常用。当然,这不是你在那里做什么的好地方
main
方法
相反,您应该使用。这个非小东西是一个技巧,一个黑客,来解决这个难题,让我们从没有应用程序运行到我们的OOP天堂的想法,有一堆东西漂浮在周围,互相传递信息
第一次学习Java时,不要试图理解main
方法的所有语法和用途。只需将其视为运行应用程序的必要条件,它只是执行应用程序的入口点。重点学习面向对象的概念和实践。稍后,main
方法和语法将更有意义
访问器
下面是示例代码的简化重写。为了简单起见,我们只使用了一个LocalDate
,它足以显示(a)一个main
方法,以及(b)getter/setter
ZoneDateTime
在计算日历时,当我们需要一个特定的时刻时,我们应用时区(ZoneId
)来获得一个zoneDateTime
对象
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ; // Determine a moment, a specific point on the timeline.
ZoneId zAuckland = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdtStart = start.atZone( z ) ; // Adjust from UTC to some time zone. Same moment, same point on the timeline, different wall-clock time.
ZonedDateTime zdtStop = stop.atZone( z ) ;
Instant
我们可以通过提取一个瞬间来查看UTC中的同一时刻
Instant instant = zdt.toInstant() ; // Adjust to UTC.
持续时间
约会通常最好存储为起点加上持续时间。无需存储停车点,因为可以计算停车点
Duration d = Duration.ofHours( 1 ) ; // A one-hour appointment.
虽然我们通常希望调整到一个时区,以便向用户演示,但通常在幕后跟踪UTC中的时刻是最佳做法。因此,作为时刻计算的约会的开始点和停止点应该作为一对Instant
对象来完成
Instant start = ldt.atZone( z ).toInstant() ;
Instant stop = start.plus( d ) ;
间隔
我们可以利用一个类来表示这对即时对象
这个类可以在Three Ten Extra library中找到,这个项目的负责人与Joda Time、JSR 310和java.Time项目的负责人Stephen Colebourne是同一个人
这个类有非常方便的比较方法,例如邻接
,重叠
,包含
,等等。您可能希望在调度应用程序中使用这些方法
Appointment.java
把这些放在一起,我们得到了这样一个类:
package com.basilbourque.example;
import org.threeten.extra.Interval;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Appointment {
private LocalDateTime start;
private Duration duration;
// Constructor.
public Appointment ( LocalDateTime start , Duration duration ) {
this.start = start;
this.duration = duration;
}
// Might add some getter/setter methods in here.
// Dynamically determine the start and stop points of this appointment, given today’s definition of the intended time zone.
public Interval toInterval ( ZoneId zoneId ) {
ZonedDateTime zdtStart = this.start.atZone( zoneId );
Interval interval = Interval.of( zdtStart.toInstant() , this.duration );
return interval;
}
}
通过调用toInterval
方法生成间隔时,可能需要单独的开始和停止时刻
Instant start = interval.getStart() ;
Instant stop = interval.getEnd() ;
这两个Instant
对象定义为UTC。如果您想通过特定地区的人们使用的挂钟时间来查看它们,请应用ZoneId
以获取zoneDateTime
对象
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ; // Determine a moment, a specific point on the timeline.
ZoneId zAuckland = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdtStart = start.atZone( z ) ; // Adjust from UTC to some time zone. Same moment, same point on the timeline, different wall-clock time.
ZonedDateTime zdtStop = stop.atZone( z ) ;
未来
您询问是否要检查此约会是否在将来进行。同样,我们需要一个时区来正确回答这个问题。目前,世界各地的时区范围约为26至27小时。因此,在当前时刻的许多小时内,如果不考虑时区,我们无法判断LocalDateTime
是未来还是过去
// Dynamically determine if this appointment will be in the future for some specific time zone.
public Boolean isFuture ( ZoneId zoneId ) {
Objects.requireNonNull( zoneId , "Must pass a time zone to determine if an appointment is in the future. Message # e1c64bc1-9a44-4d15-b20d-e68414fb5ab5.");
ZonedDateTime zdtStart = this.start.atZone( zoneId );
ZonedDateTime zdtNow = ZonedDateTime.now( zoneId );
boolean isInTheFuture = zdtNow.isBefore( zdtStart );
return isInTheFuture ;
}
因此,让我们为未来添加一个测试方法,它需要通过一个时区
// Dynamically determine if this appointment will be in the future for some specific time zone.
public Boolean isFuture ( ZoneId zoneId ) {
Objects.requireNonNull( zoneId , "Must pass a time zone to determine if an appointment is in the future. Message # e1c64bc1-9a44-4d15-b20d-e68414fb5ab5.");
ZonedDateTime zdtStart = this.start.atZone( zoneId );
ZonedDateTime zdtNow = ZonedDateTime.now( zoneId );
boolean isInTheFuture = zdtNow.isBefore( zdtStart );
return isInTheFuture ;
}
开始/停止时刻
继续关于动态确定时刻的相同主题,让我们添加一些方法来返回开始时刻(包含)和停止时刻(排除)。如上所述,这需要通过一个时区
// Dynamically determine if this appointment will be in the future for some specific time zone.
public Boolean isFuture ( ZoneId zoneId ) {
Objects.requireNonNull( zoneId , "Must pass a time zone to determine if an appointment is in the future. Message # e1c64bc1-9a44-4d15-b20d-e68414fb5ab5.");
ZonedDateTime zdtStart = this.start.atZone( zoneId );
ZonedDateTime zdtNow = ZonedDateTime.now( zoneId );
boolean isInTheFuture = zdtNow.isBefore( zdtStart );
return isInTheFuture ;
}
调用的程序员可以自己完成这项工作。但我怀疑这可能是经常需要的,足以证明添加这些方法是一种方便
// Get start moment for a particular time zone.
public ZonedDateTime toStartMoment ( ZoneId zoneId ) {
ZonedDateTime zdt = this.toInterval( zoneId ).getStart().atZone( zoneId );
return zdt;
}
// Get stop moment for a particular time zone.
public ZonedDateTime toStopMoment ( ZoneId zoneId ) {
ZonedDateTime zdt = this.toInterval( zoneId ).getEnd().atZone( zoneId );
return zdt;
}
注意,我没有给这些命名