HW2.25

时间:2022-06-19 20:37:35

HW2.25

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the time zone offset to GMT: ");
         int offset = input.nextInt();

         input.close();

         long totalMilliseconds = System.currentTimeMillis();
         long totalSeconds = totalMilliseconds / 1000;
         long currentSecond = totalSeconds % 60;
         long totalMinutes = totalSeconds / 60;
         long currentMinute = totalMinutes % 60;
         long totalHours = totalMinutes / 60;
         long currentHour = totalHours % 24;

         long currentHourWithOffset = currentHour + offset;

         if(currentHourWithOffset < 0)
             currentHourWithOffset += 24;
         else if(currentHourWithOffset > 23)
             currentHourWithOffset -= 24;

         System.out.println("The current time is " + currentHourWithOffset + ":" + currentMinute
             + ":" + currentSecond);
     }
 }

相关文章