1.
import java.util.*;
import java.text.*;
Date date=new Date();
SimpleDateFormat dformat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time=dformat.format(date);
System.out.println(time);
"yyyy年MM月dd日 HH:mm:ss"可以更换为多种格式,如"yyyy-MM-dd HH-mm-ss"
2.
import java.util.*;
import java.text.*;
Calendar cal=Calendar.getInstance();
SimpleDateFormat dformat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time=dformat.format(cal.getTime());
System.out.println(time);
3.
import java.util.*;
import java.text.*;
GregorianCalendar gcal=new GregorianCalendar();
SimpleDateFormat dformat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time=dformat.format(gcal.getTime());
System.out.println(time);
Calendar是一个抽象类,我们无法直接实例化它,它有一个具体子类实体类:java.util.GregorianCalendar,这个类实现的就是我们日常所用的公历历法,或者叫做阳历。
4.
import java.util.*;
import java.text.*;
int year,month,day,hour,minute,second;
Calendar cal=Calendar.getInstance();
year=cal.get(Calendar.YEAR); //年
month=cal.get(Calendar.MONTH)+1; //月,“+1”解释:显示的月份会比实际月份少1,因为月份定义方式是:0~11
day=cal.get(Calendar.DAY_OF_MONTH); //日
hour=cal.get(Calendar. HOUR_OF_DAY); //时
minute=cal.get(Calendar.MINUTE); //分
second=cal.get(Calendar.SECOND); //秒
5.
import java.util.*;
import java.text.*;
Date date=new Date();
DateFormat defaultFormat=DateFormat.getDateInstance();
DateFormat shortFormat=DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat mediumFormat=DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat longFormat=DateFormat.getDateInstance(DateFormat.LONG);
DateFormat fullFormat=DateFormat.getDateInstance(DateFormat.FULL);
String defaultDate=defaultFormat.format(date);
String shortDate=shortFormat.format(date);
String mediumDate=mediumFormat.format(date);
String longDate=longFormat.format(date);
String fullDate=fullFormat.format(date);
System.out.println( "(Default)Today: " +defaultDate);
System.out.println( "(SHORT)Today: " +shortDate);
System.out.println( "(MEDIUM)Today: " +mediumDate);
System.out.println( "(LONG)Today: " +longDate);
System.out.println( "(FULL)Today: " +fullDate);