久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 4 位軍用時間轉換為標準 12 小時時間格式

convert a 4-digit military time into the standard 12 hour time format(將 4 位軍用時間轉換為標準 12 小時時間格式)
本文介紹了將 4 位軍用時間轉換為標準 12 小時時間格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想做的事:

我正在嘗試將 4 位軍用時間轉換為標準的 12 小時時間格式,使用冒號和添加的 PM 或 AM,而不使用在我的代碼之前導入任何內容(我正在制作一種需要Java 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?)

輸入輸出示例:

如果給出 0056,我必須在凌晨 12:56 回來.如果給了 1125,我必須在上午 11 點 25 分返回.如果給了 2359,我必須在晚上 11:59 回來.

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. 很明顯,我正在努力生成輸出,但我不知道更簡單的方法(除了導入一些東西來完成它for我,我不想要做).
  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).

我謙虛地接受對我臃腫的當前代碼的任何批評,以及對我冗長請求的任何更正.我環顧四周尋找替代答案,一切都涉及到我以外的導入或知識.感謝您到目前為止的時間,并提前感謝大家.

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 Time library 及其 DateTime 類而不是內置的 Java DateCalendar 類.因此,我建議您使用 SimpleDateFormatrel="noreferrer">DateTimeFormat 創建一個 DateTimeFormatter,然后您將使用它來將您的字符串轉換為 日期時間.每個輸入和輸出都需要一個 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 小時時間格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 天久久 | 亚洲国产激情 | 超碰在线人 | 国产一级视频 | 国产一区二区视频免费在线观看 | 国产精品毛片在线 | 日韩欧美亚洲一区 | 成人毛片在线视频 | 国产美女在线免费观看 | 91精品国产综合久久精品 | 一区二区视频在线观看 | 在线免费观看成年人视频 | 午夜精品一区二区三区在线观看 | 国产成人精品综合 | 97av视频| 黄色一级免费 | a级在线免费视频 | 99久久99久久精品国产片果冰 | 久久这里只有精品首页 | 在线a视频| 国产一区二区久久 | 国产精品国产成人国产三级 | 午夜在线 | 99久久婷婷国产综合精品电影 | 在线观看视频中文字幕 | www国产成人 | av在线免费观看网站 | 精品国产一区二区三区久久久蜜月 | 日本精品久久 | 日韩黄色免费 | www久久久 | 天天射天天操天天干 | av第一页| 亚洲精品免费在线观看 | 91成人免费看 | 午夜在线精品偷拍 | 国产亚洲精品精品国产亚洲综合 | 国产精品日韩高清伦字幕搜索 | 激情欧美一区二区三区中文字幕 | 伊人中文字幕 | 黄视频在线网站 |