Boiler Bill
Boiler Bill

Reputation: 1940

How to format Time and String value in Java's String formatter

Using Java String.format() how do I format a string to result in "Time - userId" (ie. 3:02 pm - joe user).

I have worked several iterations, and I think it is the space between the minutes and the am/pm that is throwing me off. My last iteration is

String.format("%1$tl:%1$tM %1$tp - %2s", new Date(), "joe user");

I am about to punt and use a SimpleDateFormat, but thought I would ask here first.

Upvotes: 4

Views: 3489

Answers (1)

dogbane
dogbane

Reputation: 274522

You need %2$s to get the second argument, as shown below:

String.format("%1$tl:%1$tM %1$tp - %2$s", new Date(), "joe user")

prints:

4:56 pm - joe user

Upvotes: 7

Related Questions