2

Returned the exact String for "closing time" below from my json Result. It appears to be in 24hour time format. I did a little search around and noticed it is not in the desired format of HH:MM:SS. What's an efficient way to convert this to a 12Hour AM PM time. Thanks.`

String closingTime = "2100";
//Desired output: String closingTime = "9:00 pm"
rds
  • 26,253
  • 19
  • 107
  • 134
user3144836
  • 4,060
  • 3
  • 27
  • 27

2 Answers2

2

I would suggest parsing the time into a Calendar object. This makes it very easy to actually do stuff with the time given, rather than just dealing with it as a string

Calendar time = Calendar.getInstance();

//Calendar.HOUR_OF_DAY is in 24-hour format
time.set(Calendar.HOUR_OF_DAY, closingTime.substring(0,2));

time.set(Calendar.MINUTE, closingTime.substring(2,4));

//Calendar.HOUR is in 12-hour format
System.out.print(time.get(Calendar.HOUR) + ":" + time.get(Calendar.MINUTE) + " " + time.get(Calendar.AM_PM));

The above code will print out "9:00 PM" if you give it "2100", but the data internally is stored as millis so you can do so much more with it if you need.

EDIT The above code was not correct, more like pseudocode, as noted by the asker, who suggested the following, much more complete, working code:

String closingTime = "2101";
//getInstance() will return the current millis, so changes will be made relative to the current day and time
Calendar time = Calendar.getInstance();
// Calendar.HOUR_OF_DAY is in 24-hour format
time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(closingTime.substring(0, 2)));

// time.get(Calendar.MINUTE) returns the exact minute integer e.g for 10:04 will show 10:4
// For display purposes only We could just return the last two substring or format Calender.MINUTE as shown below
time.set(Calendar.MINUTE, Integer.parseInt(closingTime.substring(2, 4)));
String minute = String.format("%02d", time.get(Calendar.MINUTE));

// time.get(Calendar.AM_PM) returns integer 0 or 1 so let's set the right String value
String AM_PM = time.get(Calendar.AM_PM) == 0 ? "AM" : "PM";

// Calendar.HOUR is in 12-hour format
System.out.print("...\n" + time.get(Calendar.HOUR) + ":" + minute + " " + AM_PM);
cjbrooks12
  • 1,368
  • 1
  • 13
  • 25
  • The accepted answer works fine as well but your method is the most useful since the time is being converted to a Calendar object. I have some additions to add. I'll edit right below your answer. Thank you. – user3144836 May 25 '14 at 17:43
0
public static Date getDateFromString(String format, String dateStr) {

        DateFormat formatter = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = (Date) formatter.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

/////////////////////

public static String getCurrentDate(String format) {
        SimpleDateFormat sdfFrom = new SimpleDateFormat(format);
        Calendar currentTime = Calendar.getInstance();
        return (sdfFrom.format(currentTime.getTime()));
    }
Ran Adler
  • 3,587
  • 30
  • 27