将4位军事时间转换为标准的12小时时间格式

我想做什么:

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

我的情况:

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

输入输出示例:

如果给了0056,我将不得不在早上12:56返回。 如果给出1125,我将不得不在上午11:25返回。 如果给出2359,我将不得不在晚上11:59返回。

我需要帮助的问题

  1. 当我执行时,无论是输入11:24还是23:24,我的am / pm布尔值都会在某处失败,它总是输出pm。
  2. 可能很明显我工作太难以产生输出,但我不知道一种更简单的方法(除了导入一些东西为我做,我不想这样做)。

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

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

试试吧!

使用聪明的字符串转换和SimpleDateFormat,您可以做得更多,更简单,这是示例,但分为两行:

 // Heres your military time int like in your code sample int milTime = 56; // Convert the int to a string ensuring its 4 characters wide, parse as a date Date date = new SimpleDateFormat("hhmm").parse(String.format("%04d", milTime)); // Set format: print the hours and minutes of the date, with AM or PM at the end SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a"); // Print the date! System.out.println(sdf.format(date)); // Output: 12:56 AM 
 if (pm = true) if (pm = false) 

单个等号是一个赋值。 你想做一个比较:

 if (pm == true) if (pm == false) 

这些也可以更具惯用性地写成:

 if (pm) if (!pm) 

这是主要问题。 另一个是你设置pm的逻辑。 您只需要一个if语句,将减去1200的行放在pm = true块中。

 if (milTime < 1200) { pm = false; } else { pm = true; milTime -= 1200; } 

还有一些其他的错误我会留给你。 我相信这些更正会让你更进一步,至少。

 public static String convert24HourToAmPm(String time) { if (time == null) return time; // Convert time where time is like: 0100, 0200, 0300....2300... if (time.length() == 4 && Helper.isInteger(time)) { String hour = time.substring(0,2); String minutes = time.substring(2,4); String meridian = "am"; if (hour.substring(0,2).equals("00")) { hour = "12"; } else if (hour.substring(0,1).equals("1") || hour.substring(0,1).equals("2")) { meridian = "pm"; Integer militaryHour = Integer.parseInt(hour); Integer convertedHour = null; if (militaryHour > 12) { convertedHour = (militaryHour - 12); if (convertedHour < 10) hour = "0" + String.valueOf(convertedHour); else hour = String.valueOf(convertedHour); } } time = hour + ":" + minutes + " " + meridian; } // TODO - Convert time where time is like 01:00...23:00... return time; }