(1)Date是日期类,可以精确到毫秒。
A:构造方法
Date()
Date(long time)
Date d = new Date();
System.out.println("d:" + d);
// 创建对象
// long time = System.currentTimeMillis();
long time = 1000 * 60 * 60; // 1小时
Date d2 = new Date(time);
System.out.println("d2:" + d2);
B:成员方法
getTime()
setTime(long time)
C:日期和毫秒值的相互转换
public long getTime():获取时间,以毫秒为单位
* public void setTime(long time):设置时间
* 从Date得到一个毫秒值
* getTime()
long haomiao=d.getTime();//把Date转换成毫秒值
* 把一个毫秒值转换为Date
* 构造方法//Date(long time)
Date d1=new Date(System.currentTimeMillis());
System.out.println(d1);//把毫秒值转换为Date
* setTime(long time)
d.setTime(10000);
案例:你来到这个世界多少天了?
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);
// 通过该日期得到一个毫秒值
long myTime = d.getTime();
// 获取当前时间的毫秒值
- long nowTime = System.currentTimeMillis();
// 用D-C得到一个毫秒值
long time = nowTime - myTime;
// 把E的毫秒值转换为年
long day = time / 1000 / 60 / 60 / 24;
System.out.println("你来到这个世界:" + day + "天");
}
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
A:SimpleDateFormat(String pattern) 给定模式
yyyy-MM-dd HH:mm:ss
SimpleDateFormat的构造方法:
* SimpleDateFormat():默认模式
* SimpleDateFormat(String pattern):给定的模式
* 这个模式字符串该如何写呢?
* 通过查看API,我们就找到了对应的模式
* 年 y
* 月 M
* 日 d
* 时 H
* 分 m
* 秒 s
* 2014年12月12日 12:12:12
B:日期和字符串的转换
a:Date – String
- public final String format(Date date)
Date d=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
System.out.println(s.format(d));//是Date-〉String
b:String – Date
- public Date parse(String source)
String s3="2016年04月20日";
SimpleDateFormat s1=new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(s1.parse(s3));//String->Date
C:案例:
制作了一个针对日期操作的工具类。
8:Calendar(掌握)
(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance();
本质返回的是子类对象
Calendar c=Calendar.getInstance();//多态用法
int wenk=c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int data=c.get(Calendar.DATE);
int hour=c.get(Calendar.HOUR);
int min=c.get(Calendar.MINUTE);
System.out.println(year+"年"+(month+1)+"月"+data+"日"+hour+"时"+min+"分");
System.out.println(wenk);
- 成员方法
* public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
* public final void set(int year,int month,int date):设置当前日历的年月日
A:根据日历字段得到对应的值
B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
C:设置日历对象的年月日
// 5年后的10天前
c.add(Calendar.YEAR, 5);
c.add(Calendar.DATE, -10);
// 获取年
year = c.get(Calendar.YEAR);
// 获取月
month = c.get(Calendar.MONTH);
// 获取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
System.out.println("--------------");
c.set(2011, 11, 11);
// 获取年
year = c.get(Calendar.YEAR);
// 获取月
month = c.get(Calendar.MONTH);
// 获取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
}
}
(4)案例:
计算任意一年的2月份有多少天?
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
// 设置日历对象的年月日
Calendar c = Calendar.getInstance();
c.set(year, 2, 1); // 其实是这一年的3月1日
// 把时间往前推一天,就是2月的最后一天
c.add(Calendar.DATE, -1);
// 获取这一天输出即可
System.out.println(c.get(Calendar.DATE));