将4位数的军事时间转换为标准的12小时时间格式 [英] convert a 4-digit military time into the standard 12 hour time format

查看:393
本文介绍了将4位数的军事时间转换为标准的12小时时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是:

我正在尝试将4位数的军事时间转换为标准的12小时时间格式,冒号和添加的PM或AM,而不使用我的代码之前导入的任何东西(我正在制作一个不需要除了101的技术之外的方法)。

I'm trying to convert a 4-digit military time into the standard 12 hour time format, with a colon and an added PM or AM without the use of importing anything before my code (I'm making a method that requires nothing other than java 101 techniques).

我的情况

我有 milTime ,我手动更改每当我运行它(如上所述,目前在1100),直到我将它转换成一个方法并提交作业,并将在milTime中进行,并返回milTimeString以便主程序进行打印。我目前正在使用BlueJ作为IDE,(我不知道这是否是最好的?)

I have milTime, which I manually change around for now every time I run it(as declared up top, currently at 1100), until I convert it into a method and submit the assignment, in which it will take in milTime, and will return milTimeString for the main program to print. I'm currently using BlueJ as an IDE, (I'm not sure if that's the best one to use?)

输入和输出示例: / strong>

Example of input and output:

如果给了0056,我将不得不在上午12:56返回。
如果1125被给予,我将不得不回到上午11:25。
如果提供2359,我必须返回11:59 pm。

If 0056 were given, I would have to return 12:56am. If 1125 were given, I would have to return 11:25am. If 2359 were given, I would have to return 11:59pm.

我需要帮助


  1. 我执行,我的am / pm布尔在某个地方失败,总是输出pm,无论是输入11:24还是23:24。

  2. 很明显,我工作太难生成输出,但我不知道一个更简单的方法(除了为我导入某些东西,我不想做)

  1. When I execute, my am / pm boolean fails somewhere and it always outputs pm, no matter if I input 11:24 or 23:24.
  2. It's probably obvious I'm working too hard to generate the output, but I don't know an easier way (Other than importing something to do it for me, which I don't want to do).

我谦虚地接受了我blo肿的当前代码的任何批评,以及我长期以来的请求中的任何更正。我周围寻找了替代答案,一切都涉及到进口或者超出我的知识。感谢您的时间到目前为止,并提前感谢所有人。

I humbly submit to any criticism on my bloated current code, and any corrections in my long-winded request. I've looked around for alternate answers, and everything involved importing or knowledge beyond me. Thanks for your time so far, and thanks in advance everyone.

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时间库及其日期时间类,而不是内置Java 日期日历类。因此,我建议您使用 SimpleDateFormat rel =nofollow> DateTimeFormat 创建一个 DateTimeFormatter ,然后您将使用它将您的String转换为 DateTime 。每个输入和输出都需要一个DateTimeFormatter。例如:

Your problem is screaming out that you should be using your own custom data formatter. I like using the Joda Time library and its DateTime class instead of the built-in Java Date and Calendar classes. Therefore, instead of SimpleDateFormat, I recommend that you use a DateTimeFormat to create a DateTimeFormatter, which you will then use to convert your String into a DateTime. You will need one DateTimeFormatter for each of input and output. For example:

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;

尝试一下!

这篇关于将4位数的军事时间转换为标准的12小时时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆