Display AM and PM in lower case after date formatting

After formatting the date and time, the time is displayed as AM or PM in upper case, but I want it to be in lower case, for example, am or pm.

This is my code:

public class Timeis { public static void main(String s[]) { long ts = 1022895271767L; String st = null; st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts); System.out.println("time is " + ts); } } 
+61
java datetime simpledateformat
Nov 27 '12 at 9:59
source share
8 answers

Unfortunately, standard formatting methods do not allow you to do this. And Yoda too. I think you will have to handle the formatted date with a simple replacement in post-format.

 String str = oldstr.replace("AM", "am").replace("PM","pm"); 

You could use the replaceAll() method which uses regular expressions, but I think the above is probably enough. I do not do toLowerCase() as it can toLowerCase() formatting if you change the format string in the future to contain (say) month names or similar.

EDIT: James Jitin 's solution looks much better, and the right way to do it (as noted in the comments)

+64
Nov 27 '12 at 10:02
source share

It works

 public class Timeis { public static void main(String s[]) { long ts = 1022895271767L; SimpleDateFormat sdf = new SimpleDateFormat(" MMM d 'at' hh:mm a"); // CREATE DateFormatSymbols WITH ALL SYMBOLS FROM (DEFAULT) Locale DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault()); // OVERRIDE SOME symbols WHILE RETAINING OTHERS symbols.setAmPmStrings(new String[] { "am", "pm" }); sdf.setDateFormatSymbols(symbols); String st = sdf.format(ts); System.out.println("time is " + st); } } 
+79
Nov 27 '12 at 10:14
source share

Try the following:

 System.out.println("time is " + ts.toLowerCase()); 

Although you can create your own format here and here

Unfortunately, from box AM and PM do not seem to be configured in the standard SimpleDateFormat class

+6
Nov 27 '12 at 10:03
source share

If you do not want to do line substitution, and use Java 8 javax.time :

 Map<Long, String> ampm = new HashMap<>(); ampm.put(0l, "am"); ampm.put(1l, "pm"); DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendPattern("EM/dh:mm") .appendText(ChronoField.AMPM_OF_DAY, ampm) .toFormatter() .withZone(ZoneId.of("America/Los_Angeles")); 

You must manually build a DateTimeFormatter (specifying the individual parts), since there is no template character for the lowercase letters am / pm. You can use appendPattern before and after.

I believe that there is no way to replace the default characters am / pm, as this is the only way to do a line replacement in the last line.

+4
Feb 26 '17 at 8:41
source share
 Calendar c = Calendar.getInstance(); System.out.println("Current time => " + c.getTime()); SimpleDateFormat df = new SimpleDateFormat("HH:mm a"); String formattedDate = df.format(c.getTime()); formattedDate = formattedDate.replace("am", "AM").replace("pm","PM"); TextView textView = findViewById(R.id.textView); textView.setText(formattedDate); 
+3
Dec 30 '17 at 9:47
source share

James's answer is great if you want a different style than the default am, pm. But I'm afraid that you need to match the local and local specific AM / PM set to accept the override. Now you just use the java built-in class java.util.Formatter. Therefore, a simple example looks like this:

 System.out.println(String.format(Locale.UK, "%1$tl%1$tp", LocalTime.now())); 

He gives:

 9pm 

To note that if you want to use uppercase, just replace "% 1 $ t p" with "% 1 $ T p". You can find more information at http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#dt .

+1
Aug 6 '15 at 21:12
source share

just add toLowarCase() like this

 public class Timeis { public static void main(String s[]) { long ts = 1022895271767L; String st = null; st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts).toLowerCase(); System.out.println("time is " + ts); } } 

and toUpperCase() if you want to use uppercase

0
Apr 04 '17 at 12:21 on
source share
  String today = now.format(new DateTimeFormatterBuilder() .appendPattern("MM/dd/yyyy ") .appendText(ChronoField.AMPM_OF_DAY) .appendLiteral(" (PST)") .toFormatter(Locale.UK)); 

// output => 06/18/2019 (PST)

Locale.UK => in the morning or evening; Locale.US => AM or PM; try a different language for your needs (default, etc.)

0
Jun 18 '19 at 13:06 on
source share



All Articles