有没有办法在Java中做24小时格式和60分钟格式?
Is there a way to do 24-hour format and 60-minute format in Java?
if(ExcessTimeHours >= 24) {
ExcessTimeHours = 0;
}
if(ExcessTimeMin >= 60) {
ExcessTimeMin = 0;
ExcessTimeHours ++;
}
我添加了第一部电影开始的时间,然后我添加了那部电影的时长,然后我添加了第二部电影的开始时间,还添加了这部电影的时长,程序会告诉我是否可以看两部电影都没有问题,或者我是否会错过电影的某些分钟,或者我无法完成。
我的问题是,当时间超过午夜并从 24 小时跳到 0 小时时,我不知道如何解决这个问题。无法弄清楚条件。
System.out.println("Start of movie A:");
System.out.print("hour: "); // 17; 23; 0
int startHourA = s.nextInt();
System.out.print("min: "); // 30; 30; 30
int startMinA = s.nextInt();
System.out.println("Length of movie A:");
System.out.print("hour: "); // 2; 2; 2
int lengthHourA = s.nextInt();
System.out.print("min: "); // 0; 0; 1
int lengthMinA = s.nextInt();
System.out.println("Start of movie B:");
System.out.print("hour: "); // 20; 0; 0
int startHourB = s.nextInt();
System.out.print("min: "); // 0; 20; 31
int startMinB = s.nextInt();
System.out.println("Length of movie B:");
System.out.print("hour: "); // 0; 1; 2
int lengthHourB = s.nextInt();
System.out.print("min: "); // 35; 35; 0
int lengthMinB = s.nextInt();
int timeBetweenMoviesHour = startHourB - ExcessTimeHours;
int timeBetweenMoviesMin = startMinB - ExcessTimeMin;
if (timeBetweenMoviesHour == 0 && timeBetweenMoviesMin >= 0) {
System.out.println("Recommendation: No problem");
}
else if (timeBetweenMoviesMin > 0) {
System.out.println("Recommendation: You won't see " +
timeBetweenMoviesMin + " minutes");
}
else System.out.println("Recommendation: You can't make it");
java.time
您将要使用 LocalTime
和 Duration
类。例如像下面这样。我不认为我的代码完全符合您希望您的代码执行的操作,但它应该可以帮助您入门。
// Convert inputs to LocalTime and Duration objects
LocalTime startMovieA = LocalTime.of(16, 0);
Duration lengthMoviaA = Duration.ofHours(2).plusMinutes(15);
LocalTime startMovieB = LocalTime.of(18, 45);
// Do calculations
LocalTime endMovieA = startMovieA.plus(lengthMoviaA);
Duration timeBetweenMovies = Duration.between(endMovieA, startMovieB);
// Output
if (timeBetweenMovies.isNegative()) {
System.out.println("You will be missing " + timeBetweenMovies.abs());
}
else if (timeBetweenMovies.isZero()) {
System.out.println("You will just make it");
}
else {
System.out.println("You will have " + timeBetweenMovies + " between the movies");
}
输出为:
You will have PT30M between the movies
Duration
对象打印有点滑稽。格式为 ISO 8601。对于您的用户,您将希望格式化得更漂亮,例如 You will have 30 minutes between the movies
。搜索 and/or 如何使用底部的链接。在 WWW 上的各个地方都有描述。
链接
- Oracle tutorial: Date Time 解释如何使用 java.time.
- Wikipedia article: ISO 8601
- 关于如何格式化 a
Duration
的问题和答案:
- Convert seconds value to hours minutes seconds? — My answer
- How to format a duration in java? (e.g format H:MM:SS) — Answer by lauhub
if(ExcessTimeHours >= 24) {
ExcessTimeHours = 0;
}
if(ExcessTimeMin >= 60) {
ExcessTimeMin = 0;
ExcessTimeHours ++;
}
我添加了第一部电影开始的时间,然后我添加了那部电影的时长,然后我添加了第二部电影的开始时间,还添加了这部电影的时长,程序会告诉我是否可以看两部电影都没有问题,或者我是否会错过电影的某些分钟,或者我无法完成。
我的问题是,当时间超过午夜并从 24 小时跳到 0 小时时,我不知道如何解决这个问题。无法弄清楚条件。
System.out.println("Start of movie A:");
System.out.print("hour: "); // 17; 23; 0
int startHourA = s.nextInt();
System.out.print("min: "); // 30; 30; 30
int startMinA = s.nextInt();
System.out.println("Length of movie A:");
System.out.print("hour: "); // 2; 2; 2
int lengthHourA = s.nextInt();
System.out.print("min: "); // 0; 0; 1
int lengthMinA = s.nextInt();
System.out.println("Start of movie B:");
System.out.print("hour: "); // 20; 0; 0
int startHourB = s.nextInt();
System.out.print("min: "); // 0; 20; 31
int startMinB = s.nextInt();
System.out.println("Length of movie B:");
System.out.print("hour: "); // 0; 1; 2
int lengthHourB = s.nextInt();
System.out.print("min: "); // 35; 35; 0
int lengthMinB = s.nextInt();
int timeBetweenMoviesHour = startHourB - ExcessTimeHours;
int timeBetweenMoviesMin = startMinB - ExcessTimeMin;
if (timeBetweenMoviesHour == 0 && timeBetweenMoviesMin >= 0) {
System.out.println("Recommendation: No problem");
}
else if (timeBetweenMoviesMin > 0) {
System.out.println("Recommendation: You won't see " +
timeBetweenMoviesMin + " minutes");
}
else System.out.println("Recommendation: You can't make it");
java.time
您将要使用 LocalTime
和 Duration
类。例如像下面这样。我不认为我的代码完全符合您希望您的代码执行的操作,但它应该可以帮助您入门。
// Convert inputs to LocalTime and Duration objects
LocalTime startMovieA = LocalTime.of(16, 0);
Duration lengthMoviaA = Duration.ofHours(2).plusMinutes(15);
LocalTime startMovieB = LocalTime.of(18, 45);
// Do calculations
LocalTime endMovieA = startMovieA.plus(lengthMoviaA);
Duration timeBetweenMovies = Duration.between(endMovieA, startMovieB);
// Output
if (timeBetweenMovies.isNegative()) {
System.out.println("You will be missing " + timeBetweenMovies.abs());
}
else if (timeBetweenMovies.isZero()) {
System.out.println("You will just make it");
}
else {
System.out.println("You will have " + timeBetweenMovies + " between the movies");
}
输出为:
You will have PT30M between the movies
Duration
对象打印有点滑稽。格式为 ISO 8601。对于您的用户,您将希望格式化得更漂亮,例如 You will have 30 minutes between the movies
。搜索 and/or 如何使用底部的链接。在 WWW 上的各个地方都有描述。
链接
- Oracle tutorial: Date Time 解释如何使用 java.time.
- Wikipedia article: ISO 8601
- 关于如何格式化 a
Duration
的问题和答案:- Convert seconds value to hours minutes seconds? — My answer
- How to format a duration in java? (e.g format H:MM:SS) — Answer by lauhub