简体   繁体   中英

How do I get current time using Calendar?

My time displays in this format: 11:35 PM.

But I want this format: 11:35 PM <-- notice the extra dot.

this is my current approach:

 SimpleDateFormat("hh:mm a.").format(calendar.getTime());

this is what I have tried, but It would crash:

SimpleDateFormat("hh:mm .a.").format(calendar.getTime());

If you want to override the AM/PM text, but otherwise retain normal formatting, use the DateFormatSymbols object.

Using DateFormatSymbols ensures that the SimpleDateFormat can be used for both parsing and formatting .

The advantage of this solution, as opposed to my other answer , is that you are not susceptible to the JDK changing a particular locales formatting in future versions, and/or other JDK implementations.

The following example uses the standard formatting for US English, but overriding the AM/PM texts to be uppercase with periods. The example shows that both parsing and formatting works.

DateFormatSymbols symbols = DateFormatSymbols.getInstance(Locale.US);
symbols.setAmPmStrings(new String[] { "A.M.", "P.M." });

SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a", symbols);

Calendar cal = Calendar.getInstance();
cal.setTime(fmt.parse("10:23 A.M."));
cal.add(Calendar.MINUTE, 500); // 8 hrs 20 mins
System.out.println(fmt.format(cal.getTime())); // prints:  06:43 P.M.

Using DateFormatSymbols , as shown in my other answer , is more reliable. I'm keeping this answer as an illustration on the effect of locales on date formatting.

Only 3 locales seem to use pm and am (on my JDK 1.8.0_91):

  • es-US
  • ga
  • ga-IE

So you could set the locale, eg

SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a", Locale.forLanguageTag("es-US"));
System.out.println(fmt.format(new Date(2000,0,1,12,1))); // prints:  12:01 p.m.

If you want to check the effect of locales on a particular format string, run this code:

Date am = new Date(2017,0,1,6,0);
Date pm = new Date(2017,0,1,18,0);
Map<String, Set<String>> map = new TreeMap<>();
for (Locale locale : Locale.getAvailableLocales()) {
    SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a", locale);
    String s = fmt.format(am) + "  " + fmt.format(pm);
    map.computeIfAbsent(s, k -> new TreeSet<>()).add(locale.toLanguageTag());
}
map.entrySet().forEach(System.out::println);

On my JDK it creates this output:

06:00 AM  06:00 PM=[be, be-BY, bg, bg-BG, ca, ca-ES, da, da-DK, de, de-AT, de-CH, de-DE, de-GR, de-LU, en, en-AU, en-CA, en-GB, en-IE, en-IN, en-MT, en-NZ, en-PH, en-SG, en-US, en-ZA, es, es-AR, es-BO, es-CL, es-CO, es-CR, es-CU, es-DO, es-EC, es-ES, es-GT, es-HN, es-MX, es-NI, es-PA, es-PE, es-PR, es-PY, es-SV, es-UY, es-VE, et, et-EE, fr, fr-BE, fr-CA, fr-CH, fr-FR, fr-LU, he, he-IL, hi, hr, hr-HR, id, id-ID, is, is-IS, it, it-CH, it-IT, lt, lt-LT, lv, lv-LV, mk, mk-MK, ms, ms-MY, nl, nl-BE, nl-NL, nn-NO, no, no-NO, pl, pl-PL, pt, pt-BR, pt-PT, ro, ro-RO, ru, ru-RU, sk, sk-SK, sl, sl-SI, sr, sr-BA, sr-CS, sr-Latn, sr-Latn-BA, sr-Latn-ME, sr-Latn-RS, sr-ME, sr-RS, tr, tr-TR, uk, uk-UA, und]
06:00 DE  06:00 DU=[hu, hu-HU]
06:00 PD  06:00 MD=[sq, sq-AL]
06:00 QN  06:00 WN=[mt, mt-MT]
06:00 SA  06:00 CH=[vi, vi-VN]
06:00 a.m.  06:00 p.m.=[es-US, ga, ga-IE]
06:00 ap.  06:00 ip.=[fi, fi-FI]
06:00 dop.  06:00 odp.=[cs, cs-CZ]
06:00 fm  06:00 em=[sv, sv-SE]
06:00 ΠΜ  06:00 ΜΜ=[el-CY]
06:00 πμ  06:00 μμ=[el, el-GR]
06:00 ص  06:00 م=[ar, ar-AE, ar-BH, ar-DZ, ar-EG, ar-IQ, ar-JO, ar-KW, ar-LB, ar-LY, ar-MA, ar-OM, ar-QA, ar-SA, ar-SD, ar-SY, ar-TN, ar-YE]
06:00 ก่อนเที่ยง  06:00 หลังเที่ยง=[th, th-TH]
06:00 上午  06:00 下午=[zh, zh-CN, zh-HK, zh-SG, zh-TW]
06:00 午前  06:00 午後=[ja, ja-JP, ja-JP-u-ca-japanese-x-lvariant-JP]
06:00 오전  06:00 오후=[ko, ko-KR]
०६:०० पूर्वाह्न  ०६:०० अपराह्न=[hi-IN]
๐๖:๐๐ ก่อนเที่ยง  ๐๖:๐๐ หลังเที่ยง=[th-TH-u-nu-thai-x-lvariant-TH]

Some devices like Redmi Note 3 etc. are defaultly using am and pm

So after formatting replaceAll am with am and pm with pm

String time = new SimpleDateFormat("hh:mm aaa").format(calendar.getTime());
System.out.println(time.toUpperCase().replaceAll("AM.", "A.M.").replaceAll("PM.", "P.M."));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM