gpt4 book ai didi

java - 当我运行下面的java代码时出现这个错误

转载 作者:行者123 更新时间:2023-12-01 21:50:10 25 4
gpt4 key购买 nike

当我运行下面的 java 代码时出现此错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at my.time.main(time.java:9)

package my;
package com.mkyong.date;

import java.text.SimpleDateFormat;
import java.util.Date;

public class time {
public static void main(String[] args) {

String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";

//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;
Date d2 = null;

try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

//in milliseconds
long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");

} catch (Exception e) {
e.printStackTrace();
}

}

}

最佳答案

您设置package两次:

package my;
package com.mkyong.date;

只允许一次。您应该设置您的包以匹配您的类路径。

关于java - 当我运行下面的java代码时出现这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35356126/

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