【问题标题】:Java - Adding a 0 in the minutes of the current time (GMT) [duplicate]Java - 在当前时间(格林威治标准时间)的分钟内添加 0 [重复]
【发布时间】:2017-04-02 03:16:36
【问题描述】:

当我输出以下代码时(取自梁博士 Java 简介,第 10 版,第 3 章 - 选择)

/*
(Current time) Listing 2.7, ShowCurrentTime.java, gives a program that displays
the current time in GMT. Revise the program so that it prompts the user to enter
the time zone offset to GMT and displays the time in the specified time zone.
*/


import java.util.Scanner;

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

         System.out.print("Enter the time zone (GMT): ");
        int gmt = input.nextInt();

         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;
        currentHour = currentHour + gmt;

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

        input.close();
    }
}

输出是

Enter the time zone (GMT): 1
The current time is 11:2:31

我怎样才能让显示代替11:02:31?

谢谢。

【问题讨论】:

    标签: java time


    【解决方案1】:

    您可以使用 format 方法以 C 风格 printf 格式化您的输入。

    class NumericFormat
    {
        public static void main(String[] args) {
            System.out.format("%02d%n",3);
            //you can use \n too but %n is preferrable for format method
        }
    }
    

    通过Java Docs获得更好的理解

    如果链接失败,这里有一些格式化程序。


    另一方面,为了格式化和使用日期和时间,Java 8 具有干净的内置 API。看看这个 Oracle 教程 - Date Time Parsing and Formatting

    【讨论】:

      【解决方案2】:
           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;
           currentHour = currentHour + gmt;
      
           String strTime = "" + (currentHour < 10 ? "0" + currentHour : currentHour) +
               (currentMinute < 10 ? "0" + currentMinute : currentMinute) +
               (currentSecond < 10 ? "0" + currentSecond : currentSecond);
      
           System.out.println("The current time is : " + strTime);
      

      【讨论】:

        【解决方案3】:

        你可以这样做,

        String currentMinuteStr=""+currentMinute ;
        if(currentMinuteStr.length()==1){
        currentMinuteStr="0"+currentMinuteStr;
        }
        

        我刚刚将分钟变量转换为字符串,然后检查字符串的长度是否为 1,即是否为一位数分钟,然后我已将现有分钟附加到 0,然后您可以像以前一样显示它像这样,

        System.out.println("The current time is " + currentHour + ":"
                    + currentMinuteStr+ ":" + currentSecond);
        

        【讨论】:

          猜你喜欢
          • 2011-10-30
          • 2019-11-25
          • 2011-03-01
          • 2011-05-13
          • 2013-03-10
          • 1970-01-01
          • 2010-12-02
          • 1970-01-01
          • 2012-05-31
          相关资源
          最近更新 更多