package com.yuan; import java.text.SimpleDateFormat; import java.util.Date; /* * DateFormat class is an abstract class, its subclass has simpleDateFormat class, * is the class for formatting dates * */ public class Date2 { public static void main(String[] args) { time1(); } public static void time1() { // The simpleDateFormat class inherits the DateFormat class and creates an object of the SinpleDateFormat class SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH hour mm minute" ); String time = sdf.format(new Date()); System.out.println(time); } }
How to display the current time using the Calendar class
// Method for displaying time public static void showTime() { Calendar c=Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH)+1; int day=c.get(Calendar.DAY_OF_MONTH); int hours=c.get(Calendar.HOUR_OF_DAY); int m=c.get(Calendar.MINUTE); System.out.println(year+"年"+month+"月"+day+"日"+hours+"时"+m+"分"); }