You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java新手求助:基于本地时区计算目的地时间的实现问题

Fixing Your Java Time Zone Conversion Code

Hey there! Let's work through your Java time conversion problem step by step. I can see you're trying to calculate the destination time based on a time difference from your home timezone, but there are a few key issues in your code that are preventing it from running correctly. Let's break them down and fix them together.

The Issues in Your Current Code

  • Uncaught ParseException: The parse() method from SimpleDateFormat throws a checked exception that you need to handle (either with a try-catch block or by declaring it in the method signature).
  • Undefined variable diff1: You referenced diff1 in your code but never declared or initialized it — you meant to use the addhours variable you got from user input instead.
  • Date class has no plusHours() method: The old java.util.Date class doesn't have this method. Instead, we should use Java 8+'s modern java.time API, which is far more intuitive and reliable for date/time operations.

Corrected Code Using Modern Java Time API

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class TripPlanner {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        timeDiff(input);
    }

    // Time Difference Method
    public static void timeDiff(Scanner input) {
        System.out.print("What is the time difference, in hours, between your home and your destination? ");
        int addHours = input.nextInt();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalTime midnight = LocalTime.parse("24:00", formatter);
        LocalTime noon = LocalTime.parse("12:00", formatter);

        // Add the time difference to home times
        LocalTime destinationMidnight = midnight.plusHours(addHours);
        LocalTime destinationNoon = noon.plusHours(addHours);

        System.out.println("That means that when it is midnight at home, it will be " + destinationMidnight.format(formatter) + " at your destination.");
        System.out.println("And when it is noon at home, it will be " + destinationNoon.format(formatter) + " at your destination.");
        System.out.println("**********************");
    }
}

Key Changes Explained

  1. Switched to java.time.LocalTime: This class is designed specifically for handling time without dates, which is perfect for your use case. It has built-in methods like plusHours() that make time arithmetic straightforward.
  2. Used DateTimeFormatter: Replaced the outdated SimpleDateFormat with this modern formatter, which is thread-safe and has better error handling.
  3. Fixed variable references: Replaced the undefined diff1 with your user-input addHours variable.
  4. Handled parsing implicitly: The LocalTime.parse() method can throw a DateTimeParseException, but since it's an unchecked exception, you don't need to explicitly catch it for a simple test project (though you could add a try-catch if you want to handle invalid time inputs gracefully).
  5. Formatted output properly: Used format(formatter) to display the resulting times in the HH:mm format instead of the default LocalTime string representation.

Bonus Tip

If you ever need to work with full dates and time zones (not just time differences), the java.time API has classes like ZonedDateTime that can handle time zone conversions directly (e.g., converting from America/New_York to Europe/London) without manually calculating hour differences. It's worth exploring once you're comfortable with basic time operations!

内容的提问来源于stack exchange,提问作者Deepak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:37:34