gpt4 book ai didi

java - 未为对象类型定义getTime()方法-怎么了?

转载 作者:行者123 更新时间:2023-12-02 11:15:06 25 4
gpt4 key购买 nike

我创建了一个应用程序,当您单击名为“计算差异”的按钮时,会在两个日期之间产生差异,但是我不知道这段代码有什么问题。我尝试了不同的方法,但没有任何结果。如果您能帮助我,我将不胜感激。

public class DateForm extends javax.swing.JPanel {

String date1 = "26/02/2011";
String time1 = "11:00 AM";
String date2 = "27/02/2011";
String time2 = "12:15 AM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
private Object dateObj1;
private Object dateObj2;

public DateForm() {
initComponents();
}

private void btnActionPerformed(java.awt.event.ActionEvent evt) {
try {
Date dateObj1 = sdf.parse(date1 + " " + time1);
} catch (ParseException ex) {
Logger.getLogger(DateForm.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Date dateObj2 = sdf.parse(date2 + " " + time2); // TODO add your handling code here:
} catch (ParseException ex) {
Logger.getLogger(DateForm.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(dateObj1);
System.out.println(dateObj2);
long diff = dateObj2.getTime() - dateObj1.getTime();
double diffInHours = diff / ((double) 1000 * 60 * 60);
System.out.println(diffInHours);
System.out.println("Hours " + (int)diffInHours);
System.out.println("Minutes " + (diffInHours - (int)diffInHours)*60 );

}
}

最佳答案

从2018年开始,您确实应该使用Java 8中引入的日期/时间API(如果您使用的是Java 7或更低版​​本,则应使用ThreeTen Backport)

String date1 = "26/02/2011";
String time1 = "11:00 AM";
String date2 = "27/02/2011";
String time2 = "12:15 AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm a");

LocalDateTime start = LocalDateTime.parse(date1 + " " + time1, formatter);
LocalDateTime end = LocalDateTime.parse(date2 + " " + time2, formatter);

Duration duration = Duration.between(start, end);

long hours = duration.toHours();
long mins = duration.minusHours(hours).toMinutes();
// Or if you're using Java 9+
//long mins = duration.toMinutesPart();
System.out.println("Hours = " + hours);
System.out.println("Mins = " + mins);

哪个输出
Hours = 13
Mins = 15

我建议阅读 Date and Time Classes

"whats wrong with this code please?"



以下...
try {
Date dateObj1 = sdf.parse(date1 + " " + time1);
} catch (ParseException ex) {
Logger.getLogger(DateForm.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Date dateObj2 = sdf.parse(date2 + " " + time2); // TODO add your handling code here:
} catch (ParseException ex) {
Logger.getLogger(DateForm.class.getName()).log(Level.SEVERE, null, ex);
}

只是创建两个对象,它们的范围仅与定义它们的 try-catch块相关,在这些块之外无法访问 dateObj1dateObj2……但是...

您定义...
private Object dateObj1;
private Object dateObj2;

作为实例字段,这意味着...
System.out.println(dateObj1);
System.out.println(dateObj2);

将最有可能打印 null和...
long diff = dateObj2.getTime() - dateObj1.getTime();

将不会编译,因为 dateObj1dateObj2仅定义为 Object,而 Object没有 getTime方法

日期/时间计算很复杂,并且受许多不同的规则支配(由于您使用的日历类型而更加复杂)

只是做...
double diffInHours = diff / ((double) 1000 * 60 * 60);

最好是天真,坏了

和...
System.out.println("Minutes " + (diffInHours - (int)diffInHours)*60 );

很好,毫无意义, (something - something) * 60是什么?

关于java - 未为对象类型定义getTime()方法-怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com