Toon de huidige tijd in 12-uurs formaat met AM/PM

Momenteel wordt de tijd weergegeven als 13:35 PM
Ik wil echter de 12-uursnotatie weergeven met AM/PM, d.w.z. 13:35 in plaats van 13:35

De huidige code is zoals hieronder

private static final int FOR_HOURS = 3600000;
private static final int FOR_MIN = 60000;
public String getTime(final Model model) {
    SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm a");
    formatDate.setTimeZone(userContext.getUser().getTimeZone());
    model.addAttribute("userCurrentTime", formatDate.format(new Date()));
    final String offsetHours = String.format("%+03d:%02d", userContext.getUser().getTimeZone().getRawOffset()
    / FOR_HOURS, Math.abs(userContext.getUser().getTimeZone().getRawOffset() % FOR_HOURS / FOR_MIN));
    model.addAttribute("offsetHours",
                offsetHours + " " + userContext.getUser().getTimeZone().getDisplayName(Locale.ROOT));
    return "systemclock";
}

Antwoord 1, autoriteit 100%

Gemakkelijkste manier om het te krijgen door het datumpatroon te gebruiken – h:mm a, waarbij

  • u – Uur in am/pm (1-12)
  • m – Minuut in uur
  • a – Am/pm-markering

Codefragment :

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Lees meer over documentatie – SimpleDateFormat java 7


Antwoord 2, autoriteit 23%

Gebruik deze SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");

voer hier de afbeeldingsbeschrijving in

Java-documenten voor SimpleDateFormat


Antwoord 3, autoriteit 15%

gebruik "HH:mm a"in plaats van "HH:mm a". Hier HHvoor 12-uurs formaat en HHvoor 24-uurs formaat.

Live Demo


Antwoord 4, autoriteit 7%

SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");
  • h wordt gebruikt voor AM/PM tijden (1-12).

  • H wordt 24 uur per dag gebruikt (1-24).

  • a is de AM/PM-markering

  • m is minuut in uur

Opmerking: met twee h’s wordt een voorloopnul afgedrukt: 13:13 uur. Eén h wordt afgedrukt zonder de voorloopnul: 13:13 uur.

Het lijkt erop dat iedereen me al voor was, maar ik dwaal af


Antwoord 5, autoriteit 4%

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
String formattedDate = dateFormat.format(new Date()).toString();
System.out.println(formattedDate);

Uitvoer:
11-Sep-13 12.25.15.375 UUR


Antwoord 6, autoriteit 3%

// hh:mm will print hours in 12hrs clock and mins (e.g. 02:30)
System.out.println(DateTimeFormatter.ofPattern("hh:mm").format(LocalTime.now()));
// HH:mm will print hours in 24hrs clock and mins (e.g. 14:30)
System.out.println(DateTimeFormatter.ofPattern("HH:mm").format(LocalTime.now())); 
// hh:mm a will print hours in 12hrs clock, mins and AM/PM (e.g. 02:30 PM)
System.out.println(DateTimeFormatter.ofPattern("hh:mm a").format(LocalTime.now())); 

Antwoord 7, autoriteit 2%

Java 8 gebruiken:

LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
System.out.println(localTime.format(dateTimeFormatter));

De uitvoer is in AM/PMformaat.

Sample output:  3:00 PM

Other episodes