Java的日期格式化常用方法
一般常用格式化类DateFormat和SimpleDateFormat的format(Date time)方法进行格式化日期.
1. 首先介绍java.text.SimpleDateFormat类.
public class SimpleDateFormat extendspublic?void?testCalendar(){
????????Calendar?c1?=?Calendar.getInstance();
????????c1.setTime(new?Date());
????????
????????//当Calendar中设置的时间超过每项的最大值时,会以减去最大值后的值设置时间,例如月份设置13,最后会变成13-11=02
????????Calendar?c2?=?Calendar.getInstance();
????????c2.set(1920,?13,?24,?22,?32,?22);
?? ? ? ?//使用pattern
????????SimpleDateFormat?format?=?new?SimpleDateFormat("yyyy-MM-dd?H:m:s");
????????SimpleDateFormat?format2?=?new?SimpleDateFormat("yy-MM-dd?H:m:s");
????????SimpleDateFormat?format3?=?new?SimpleDateFormat("y-M-d?H:m:s");
?? ? ? ?//使用约定格式DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());?
?? ? ? ?//获取Calendar中各个属性字段的方法
????????log.info("The?year?now?time?is?"?+?c1.get(c1.YEAR));
????????log.info("The?month?now?time?is?"?+?c1.get(c1.MONTH));
????????log.info("The?day_of_month?now?time?is?"?+?c1.get(c1.DAY_OF_MONTH));
????????log.info("The?day_of_week?now?time?is?"?+?c1.get(c1.DAY_OF_WEEK));
????????log.info("今天是在这个月的第几个星期:?"?+?c1.get(c1.DAY_OF_WEEK_IN_MONTH));
????????log.info("The?day_of_year?now?time?is?"?+?c1.get(c1.DAY_OF_YEAR));
?? ? ? ?//不同模式对应的格式略有不同,有时间可以测试多一点模式
????????log.info("yyyy-MM-dd?H:m:s-->"?+?format.format(c1.getTime()));
????????log.info("yy-MM-dd?H:m:s-->"?+?format2.format(c1.getTime()));
????????log.info("y-M-d?H:m:s-->"?+?format3.format(c1.getTime()));
?log.info("DateFormat.FULL-->" + dateFormat.fomat(c1.getTime()));????????log.info(format.format(c2.getTime()));
}?