tl;dr
Use java.time rather than Joda-Time.
LocalTime.of( 13 , 0 )
.format(
DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT )
.withLocale( Locale.US )
)
1:00 PM
See this code run live at IdeOne.com.
…or…
ZonedDateTime.of(
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) , // Get today’s date at this moment in this time zone.
LocalTime.of( 13 , 0 ) , // Specify the time-of-day given to us. In this example `13` for 1 PM is given to us.
ZoneId.of( "Pacific/Auckland" ) // Specify the time zone giving the context for this date-time.
).format( // Generate a string representing the value of this date-time object.
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
.withLocale( Locale.US ) // Localize using the language and cultural norms of the United States.
)
5/4/17 1:00 PM
Update
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.
Using java.time
The LocalTime
and LocalDate
classes represent each part of a date-time without any offset-from-UTC nor time zone.
int hour = 13 ; // 24-hour clock, 0-23. So `13` is 1 PM.
LocalTime lt = LocalTime.of( hour , 0 ); // ( hours , minutes )
lt.toString(): 13:00
Let java.time localize for you.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.
Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( Locale.US ) ;
String output = lt.format( f );
output: 1:00 PM
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z );
ld.toString(): 2017-01-23
To arrive at a specific point on the timeline, we apply a time zone (ZoneId
object) to get a ZonedDateTime
.
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ; // ( LocalDate , LocalTime , ZoneId )
zdt.toString(): 2017-01-23T13:00:00-05:00[America/Montreal]
Generate a string in your desired format. As seen above, let java.time localize for you.
DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( Locale.US ) ;
String output2 = zdt.format( f2 );
5/4/17 1:00 PM
See this code run live at IdeOne.com.
Not really “military time”
By the way, the term “military time” in referring to the 24-hour clock is mainly a United States phenomenon as the US is one of the rare places where the 12-hour clock dominates.
Other people around the globe commonly use both, referring to 12-hour clock for casual matters and to 24-hour clock in critical matters such as train schedules. A much wiser practice in my experience.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?