java - 将 4 位军用时间转换为标准 12 小时时间格式

标签 java datetime time formatting

我想做的事:

我正在尝试将 4 位军用时间转换为标准的 12 小时时间格式,使用冒号和添加的 PM 或 AM,而不使用在我的代码之前导入任何内容(我正在制作一种需要Java 101 技术)。

我的情况:

我有 milTime,我现在每次运行它时都会手动更改它(如顶部声明的,当前为 1100),直到我将其转换为方法并提交分配,其中它将接受 milTime,并将返回 milTimeString 供主程序打印。我目前正在使用 BlueJ 作为 IDE,(我不确定这是否是最好的?)

输入输出示例:

如果给出 0056,我必须在凌晨 12:56 回来。 如果给了 1125,我必须在上午 11 点 25 分返回。 如果给了 2359,我必须在晚上 11:59 回来。

我需要帮助的问题

  1. 当我执行时,我的 am/pm boolean 值在某处失败,它总是输出 pm,无论我输入 11:24 还是 23:24。
  2. 很明显,我正在努力生成输出,但我不知道更简单的方法(除了导入一些东西来完成它for我,我不想要做)。

我谦虚地接受对我臃肿的当前代码的任何批评,以及对我冗长请求的任何更正。我环顾四周寻找替代答案,一切都涉及到我以外的导入或知识。感谢您到目前为止的时间,并提前感谢大家。

public class timeTest
{
    public static void main(String []args)
    {   
        /*Declare my variables*/
        int milTime = 2400;
        String timeString = "";
        boolean pm;

        /*determine AM or PM and convert over 1200 into a clock's digits */
        if (milTime >1259)
        {
            if (milTime <1200)
            {
                pm = false;
            }
            else
            {
                pm = true;
            }
            milTime = (milTime - 1200);
        }
        else
        {
        }

        /*figure out my digits*/
        int fourthDigit = milTime%10;
        milTime = milTime/10;
        int thirdDigit = milTime%10;
        milTime = milTime/10;
        int secondDigit = milTime%10;
        milTime = milTime/10;
        int firstDigit = milTime%10;

        /*build each side of the colon*/
        String hoursString = thirdDigit + "" + fourthDigit;
        String minutesString = firstDigit + "" + secondDigit;

        /*determine if the first digit is zero and if so, omit it*/
        if (firstDigit == 0 )
        {
            minutesString = "" + secondDigit;
        }
        else
        {
        }
        if (secondDigit == 0)
        {
            minutesString = "12";
        }
        else
        {
        }

        /*build the total string and return the result with AM or PM based on conditional boolean.*/
        if (pm = true)
        {
            timeString = (minutesString + ':' + hoursString + "pm");
        }
        else
        {
        }

        if (pm = false)
        {
            timeString = (minutesString + ':' + hoursString + "am");
        }
        else
        {
        }
        System.out.println(timeString);

    }
}

最佳答案

您的问题是您应该使用自己的自定义数据格式化程序。我喜欢使用 Joda Time library及其 DateTime 类,而不是 Java 内置的 DateCalendar 类。因此,我建议您使用 DateTimeFormat 而不是 SimpleDateFormat。创建 DateTimeFormatter ,然后您将使用它来将您的字符串转换为 DateTime .每个输入和输出都需要一个 DateTimeFormatter。例如:

String rawTimestamp = "2300"; // For example
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("HHmm");
DateTimeFormatter outputFormatter = DateTimeFormat.forPattern("hh:mm a");
DateTime dateTime = inputFormatter.parseDateTime(rawTimestamp);
String formattedTimestamp = outputFormatter.print(dateTime.getMillis());
return formattedTimestamp;

试试这个!

关于java - 将 4 位军用时间转换为标准 12 小时时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342596/

相关文章:

c++ - 如何在c++中实现函数超时

java - Jboss 4.2类加载

python - 在 Python 中创建时间范围

python - 从 TimeDelta 到 Pandas 中的 float 天数

c# - 使用 DateTime 获取前几个月的名称

python - '020-06-08 17 :11:02+00:00' I want to extract the date from this time format in Python using datetime

java - 尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'

java - H2 数据库与 .NET 应用程序

java - 如何从序列化中排除 GWT 中的对象属性?

linux - 在linux C中的特定时间发送信号