Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display time in AM/PM format

Tags:

java

datetime

I wanted to display time in AM / PM format. Example : 9:00 AM I wanted to perform addition subtraction operation as well. My event will start from 9:00 AM all time. I wanted to add minutes to get the result schedule event. How can I do that other then making a custom Time class?

Start 9:00 AM Add 45 min, after addition Start Time 9:45 AM

like image 939
Jeet Avatar asked Jul 31 '14 05:07

Jeet


2 Answers

Start with a SimpleDateFormat, this will allow you parse and format time values, for example...

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
    // Get the start time..
    Date start = sdf.parse("09:00 AM");
    System.out.println(sdf.format(start));
} catch (ParseException ex) {
    ex.printStackTrace();
}

With this, you can then use Calendar with which you can manipulate the individual fields of a date value...

Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, 45);
Date end = cal.getTime();

And putting it all together...

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
    Date start = sdf.parse("09:00 AM");
    Calendar cal = Calendar.getInstance();
    cal.setTime(start);
    cal.add(Calendar.MINUTE, 45);
    Date end = cal.getTime();

    System.out.println(sdf.format(start) + " to " + sdf.format(end));
} catch (ParseException ex) {
    ex.printStackTrace();
}

Outputs 09:00 AM to 09:45 AM

Updated

Or you could use JodaTime...

DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2).appendLiteral(" ").appendHalfdayOfDayText().toFormatter();
LocalTime start = LocalTime.parse("09:00 am", dtf);
LocalTime end = start.plusMinutes(45);

System.out.println(start.toString("hh:mm a") + " to " + end.toString("hh:mm a"));

Or, if you're using Java 8's, the new Date/Time API...

DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("hh:mm a").toFormatter();
LocalTime start = LocalTime.of(9, 0);
LocalTime end = start.plusMinutes(45);

System.out.println(dtf.format(start) + " to " + dtf.format(end));
like image 72
MadProgrammer Avatar answered Oct 01 '22 15:10

MadProgrammer


java.time

I should like to contribute the modern answer

    // create a time of day of 09:00
    LocalTime start = LocalTime.of(9, 0);
    // add 45 minutes
    start = start.plusMinutes(45);

    // Display in 12 hour clock with AM or PM
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
            .withLocale(Locale.US);
    String displayTime = start.format(timeFormatter);
    System.out.println("Formatted time: " + displayTime);

The output is:

Formatted time: 9:45 AM

The SimpleDateFormat, Date and Calendar classes used in most of the other answers are not only poorly designed (the first in particular notoriously troublesome), they are also long outdated since java.time, the modern Java date and time API, was already out when this question was asked more than four years ago.

For a time to be displayed to a user I generally recommend the built-in formats that you get from DateTimeFormatter.ofLocalizedDate, .ofLocalizedTime and .ofLocalizedDateTime. Should you in some situation have particular formatting needs that are not met with the built-in formats, you may also specify your own, for example:

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.US);

(This particular example is pointless since it gives the same result as above, but you may use it as a starting point and modify it to your needs.)

Link: Oracle tutorial: Date Time explaining how to use java.time.

like image 22
Ole V.V. Avatar answered Oct 01 '22 16:10

Ole V.V.