Convert 4-digit wartime to standard 12-hour time format

What am I trying to do:

I am trying to convert 4-digit wartime into a standard 12-hour time format, with a colon and added PM or AM without importing anything up to my code (I am making a method that requires nothing but java 101 technique).

My situation:

I have milTime , which I manually change around every time I run it (as indicated above, currently at 1100), until I convert it to a method and pass the job to which it will accept in milTime, and return milTimeString for the main program for printing. I am currently using BlueJ as an IDE (I'm not sure if this is the best option to use?)

Example input and output:

If 0056 were given, I should have returned 12:56. If 1125 were given, I would have to return 11:25. If 2359 were provided, I would have to return 23:59.

Problems I Need Help With

  • When I execute, my am / pm boolean fails somewhere, and it always outputs pm, regardless of whether I enter 11:24 or 23:24.
  • It is probably obvious that I work too much to generate output, but I don’t know an easier way (besides importing something for me that I don’t want to do).

I humbly submit to any criticism of my bloated current code and any corrections in my long request. I was looking for alternative answers, and everything included import or knowledge for me. Thank you for your time, and thank you in advance.

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 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); } } 
+6
source share
4 answers

Your problem is that you should use your own data format. I like to use the Joda time library and its DateTime class instead of the Java Date and Calendar built-in classes. So instead of SimpleDateFormat, I recommend using DateTimeFormat to create a DateTimeFormatter , which you will then use to convert your string to DateTime . For each entry and exit you will need one DateTimeFormatter. For instance:

 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; 

Try it!

+6
source

You can make it much simpler using smart string conversion and SimpleDateFormat, here is an example, but parsed in two lines:

 // 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 
+5
source
 if (pm = true) if (pm = false) 

One equal sign is an assignment. Do you want to make a comparison:

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

They can also be written more idiomatically as:

 if (pm) if (!pm) 

This is the main problem. The other is your logic for setting pm . You need only one if with the subtraction of the line 1200 placed inside the pm = true block.

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

There are a few more errors that I will leave to you. I believe that these corrections will help you a little further.

+4
source
 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; } 
+1
source

Source: https://habr.com/ru/post/959209/


All Articles