<pre name="code" class="java">//==========================日期时间相关API==================
public class DateDemo {
private static final long LEVEL = 20150701;//本码讲版本
/**=========================Date========================
* Date用于存储时间 如果用其获取或计算时间将不保证精确(均已过时)
* 因此官方建议将Date用于表示一个固定的时间点
==========================================================================*/
public void testDate() {
/*
* Date的创建和常用方法
*/
Date date = new Date();
System.out.println("当前时间:" + date);
//getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
long l = date.getTime();
System.out.println("当前时间的Long值:" + l);
// setTime() 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
l += 1000 * 60 * 60 * 24;
date.setTime(l);
System.out.println("明天:" + date);
}
/**=====================SimpleDateFormat=====================
* SimpleDateFormat类继承DateFormat用来格式化日期时间
================================================================================*/
public void testSimpleDateFormat() {
String pattern = "yyyy年MM月dd日 HH点mm分ss秒";
/*
* Date--->String
*/
// 直接使用SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat(pattern);
System.out.println("1.直接使用SimpleDateFormat "
+ format.format(new Date()));
// 直接使用DateFormat
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,
Locale.ENGLISH);
System.out.println("2.直接使用DateFormat " + df.format(new Date()));
//兼容性最好的创建方法 使用DateFormat的工厂方法实例化,可控使用哪个地区的时间表示
SimpleDateFormat simple = (SimpleDateFormat) DateFormat
.getDateInstance(DateFormat.FULL, Locale.CHINA);
simple.applyPattern(pattern);
System.out.println("3.使用DateFormat工厂方法实例化SimpleDateFormat "
+ simple.format(new Date()));
/*
* String--->Date
*/
String str = "2008年08月08日 20点08分00秒";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
Date date = sdf.parse(str);
System.out.println("4.字符串2008年08月08日 20点08分00秒转换为时间:" + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
/**====================Calendar=====================
* Calendar用于获取和计算时间
====================================================================*/
Calendar calendar = Calendar.getInstance();
int year;
int month;// 月从0开始
int day;
int week;
@Test
public void testCalendar() {
/*
* Calendar--->date
*/
System.out.println("Calendar--->Date: " + calendar.getTime());
/*
* Date--->Calendar
*/
Date date = new Date();
calendar.setTime(date);
/*
* 操作Calendar设置时间
*/
//为当前Calendar设置时间 2008-08-08 20:08:00
//方式一:直接赋值时间分量
// cal.set(Calendar.YEAR, 2008);
// cal.set(Calendar.MONTH, 8);
// cal.set(Calendar.DAY_OF_MONTH, 8);//等效DATE
//方式二:一次设置6种时间分量
calendar.set(2008, Calendar.AUGUST, 8, 20, 8, 0);// 月从0开使 设置值超过限制会向上进位
//对Calendar中时间的格式化
SimpleDateFormat sdf = new SimpleDateFormat(
"G w yyyy年MM月dd日 HH点mm分ss秒 E F W a z");
System.out.println("3.手动设置Calendar时间: "
+ sdf.format(calendar.getTime()));
/*
* 获取Calendar时间分量
*/
getCalendar();//见方法
System.out.println("分别获取Calendar时间分量:" + year + "年" + month + '月' + day
+ "日 星期" + (week == 1 ? 7 : week - 1));
int dayOfYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
System.out.println("计算当前年度的最大天数值: "+year + "共" + dayOfYear + "天");
/*
* 使用Calendar进行时间计算
*/
calendar.add(Calendar.MONTH, 3);// 计算3个月后那天
calendar.add(Calendar.DAY_OF_YEAR, 25);// 计算25天后那天
getCalendar();// 星期从周日开始
System.out.println("计算当前时间3月25天后的时间: " + year + "年"
+ month + '月' + day + "日" + "星期" + (week == 1 ? 7 : week - 1));
calendar.add(Calendar.YEAR, -1);// 一年前的那天
getCalendar();
System.out.println("计算一年前的今天: " + year + "年" + month
+ '月' + day + "日" + "星期" + (week == 1 ? 7 : week - 1));
}
/**
* 分别获取Calendar时间分量
*/
private void getCalendar() {
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;// 月从0开始
day = calendar.get(Calendar.DATE);
week = calendar.get(Calendar.DAY_OF_WEEK);
}
}