一、代码 import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main1 { public static void main(String[] args) { Date nowDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("aa", Locale.ENGLISH); String format = sdf.format(nowDate); System.out.println(format); if(format.contains("PM")){ System.out.println("下午"); }else if(format.contains("AM")){ System.out.println("上午"); } SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss aa", Locale.ENGLISH); String format2 = sdf2.format(nowDate); System.out.println("24小时制:"+format2); SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd,yyyy h:mm:ss aa", Locale.ENGLISH); String format3 = sdf3.format(nowDate); System.out.println("12小时制:"+format3); } } 二、输出