本文转自:http://blog.youkuaiyun.com/dongzhouzhou/article/details/8655700
示例:
package com.xy6;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class EqualDemo {
public static void main(String[] args){
Date date = new Date();
DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
System.out.println(df1.format(date));
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df2.format(date));
DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
System.out.println(df3.format(date));
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
System.out.println(df4.format(date));
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
System.out.println(df5.format(date));
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
System.out.println(df6.format(date));
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
System.out.println(df7.format(date));
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//获取年份
int month=cal.get(Calendar.MONTH);//获取月份
int day=cal.get(Calendar.DATE);//获取日
int hour=cal.get(Calendar.HOUR);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒
int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
System.out.println("现在的时间是:公元" + year + "年" + month + "月" + day
+ "日 " + hour + "时" + minute + "分" + second + "秒 星期"
+ WeekOfYear);
}
}
输出:
2014-5-11
2014-5-11 15:32:40
15:32:40
2014年5月11日 星期日 下午03时32分40秒 CST
2014年5月11日 下午03时32分40秒
14-5-11 下午3:32
2014-5-11 15:32:40
现在的时间是:公元2014年4月11日 3时32分40秒 星期1