Java第十二天
- 日期处理
- Date类
- 常用构造方法
- 常用方法
- SimpleDateFormat类
- 常用构造方法
- 常用方法
- Calender(日历类)
- 常用构造方法
- 常用方法
- 日期与日历间的转换
- 日期常用格式(重要)
- 集合(Collection)
- 常用方法(添加)
- 常用方法(删除)
- 常用方法(判断)
- 常用方法(转换)
- 迭代器(Iterator,遍历集合)
日期处理
Date:表示特定的瞬间,精确到毫秒。
Date()
分配Date对象并初始化此对象,以表示分配它的时间(时间精确到毫秒)
例:
Date date = new Date();
System.out.println(date);
输出结果:
Wed Mar 11 20:10:57 CST 2020
----------------------------------------------------------------------------------
Date(long date)
使用给定的毫秒时间值构造一个 Date对象。
(分配Date对象并初始化此对象,以表示自标准基时间以来的指定的毫秒数)
例:
long ms = 1000*60*60*24;
//获得当前时间的毫秒数
long cms = System.currentTimeMillis();
Date date = new Date(cms - ms);
System.out.println("昨日当前时间:"+date);
输出结果:
昨日当前时间:Tue Mar 10 20:24:28 CST 2020
返回值类型 方法名 参数列表
void getTime(long date)
使用给定的毫秒时间值设置现有的 Date对象。
(返回自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数)
例:
Date date = new Date();
System.out.println(date.getTime());
输出结果:
1583929685195
---------------------------------------------------------------------------------
自定义日期格式(注意大小写)
SimpleDateFormat()
构造一个 SimpleDateFormat使用默认模式和日期格式符号为默认的 FORMAT区域设置。
例:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
输出结果:
无
---------------------------------------------------------------------------------
SimpleDateFormat(String pattern)
使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。
(自定义格式化日期对象,格式要求详见上方)
例:
Date date = new Date();
//此种格式为实际工作中常用格式
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
System.out.println(sdf.format(date.getTime()));
SimpleDateFormat sdf1= new SimpleDateFormat("yyyy年MM月dd日 HH时mm:分ss秒:SSS毫秒");
System.out.println(sdf1.format(date));
System.out.println(sdf1.format(date.getTime()));
输出结果:
2020-03-12 11:39:59
2020-03-12 11:39:59
2020年03月12日 11时39:分59秒:322毫秒
2020年03月12日 11时39:分59秒:300毫秒
---------------------------------------------------------------------------------
返回值类型 方法名 参数列表
StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)
将给定的 Date成日期/时间字符串,并将结果追加到给定的 StringBuffer 。
例:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
System.out.println(simpleDateFormat.format(System.currentTimeMillis()));
System.out.println(simpleDateFormat.format(date));
System.out.println(simpleDateFormat.format(date.getTime()));
输出结果:
20-3-12 上午11:22
20-3-12 上午11:22
20-3-12 上午11:22
---------------------------------------------------------------------------------
Date parse(String text, ParsePosition pos)
从字符串中解析文本以产生一个 Date 。
例:
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "1990-01-01 00:00:00";
//可能发生用户输入的日期格式可能与程序自定义的格式不符的异常
try {
Date date = sdf.parse(str);
//输出默认时间格式的时间
System.out.println(date);
//输出经过自定义格式转换后的时间
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
输出结果:
Mon Jan 01 00:00:00 CST 1990
1990-01-01 00:00:00
---------------------------------------------------------------------------------
由于该方法的访问限定修饰符类型为protected,所以不能直接创建其对象,需要使用其子类创建其对象。
protected Calendar()
构建具有默认时区和默认的 FORMAT语言环境的日历。
例:
输出结果:
---------------------------------------------------------------------------------
protected Calendar(TimeZone zone, Locale aLocale)
构造具有指定时区和区域设置的日历。
例:
输出结果:
---------------------------------------------------------------------------------
返回值类型 方法名 参数列表
static Calendar getInstance()
使用默认时区和区域设置获取日历。
例:
输出结果:
---------------------------------------------------------------------------------
int get(int field) (注意:月份的值要加一)
返回给定日历字段的值。
例:
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
//获得月时,要将月的数值加1才是实际月
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millis = calendar.get(Calendar.MILLISECOND);
System.out.println(year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒"+millis+"毫秒");
输出结果:
2020年3月12日 13时38分35秒399毫秒
---------------------------------------------------------------------------------
void set(int field, int value)
将给定的日历字段设置为给定的值。 (设置指定月时,设置数值要比预期数值小1)
例:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,1990);
//设置指定月时,设置数值要比预期数值小1
calendar.set(Calendar.MONTH,04);
calendar.set(Calendar.DAY_OF_MONTH,06);
calendar.set(Calendar.HOUR_OF_DAY,07);
calendar.set(Calendar.MINUTE,8);
calendar.set(Calendar.SECOND,9);
calendar.set(Calendar.MILLISECOND,666);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(calendar.getTime()));
输出结果:
1990-05-06 07:08:09:666
---------------------------------------------------------------------------------
long getTimeInMillis()
以毫秒为单位返回此日历的时间值。
例:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
System.out.println(sdf.format(calendar.getTimeInMillis()));
输出结果:
2020-03-12 13:58:30:873
---------------------------------------------------------------------------------
Calendar calendar = Calendar.getInstance();
long lg = calendar.getTimeInMillis();
Date date = new Date(lg);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
System.out.println(str);
输出结果:
2020-03-12 14:05:07
---------------------------------------------------------------------------------
日期常用格式(重要)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(获得的long类型的毫秒数);
String str = sdf.format(date);
System.out.println(str);
集合(Collection)
定义:集合也叫容器,用于存储对象。
由于集合是接口,不能创建其对象,所以需要被实现。但可以作为父类被子类对象赋值(多态)
集合与数组的区别:
数组:长度固定,可以存储基本数据类型,也可以存储对象。(由于数组的容量固定,所以数组存储对象具有局限性)
集合:长度可变,由于包装类的存在,在集合中可以存储任意类型
常用方法(添加)
返回值类型 方法名 参数列表
boolean add(E e)
确保此集合包含指定的元素(可选操作)。(此方法的返回值用来确认添加元素是否成功,示例在
第二个方法中展示)
例:
//首先创建Student类
Student student = new Student();
student.setId(9527);
student.setName("张三");
student.setAge(20);
Collection collection = new ArrayList();
collection.add("例子1");
collection.add(1212);
collection.add(student);
System.out.println(collection);
输出结果:
[例子1, 1212, Student [学号=9527, 姓名=张三, 年龄=20]]
---------------------------------------------------------------------------------
boolean addAll(Collection<? extends E> c)
将指定集合中的所有元素添加到此集合(可选操作)。(此方法的返回值用来确认添加元素是否成功)
目标表集合名.addAll(被添加元素的集合名);
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
Collection collection2 = new ArrayList();
for(int i = 0;i<5;i++) {
boolean b = collection2.add("集合二"+i);
System.out.print(b+" ");
}
collection.addAll(collection2);
System.out.println(collection);
输出结果:
true true true true true [集合一0, 集合一1, 集合一2, 集合一3, 集合一4, 集合二0,
集合二1, 集合二2, 集合二3, 集合二4]
---------------------------------------------------------------------------------
常用方法(删除)
void clear()
从此集合中删除所有元素(可选操作)。
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
collection.clear();
System.out.println(collection);
输出结果:
[]
---------------------------------------------------------------------------------
boolean remove(Object o)
从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
(此方法的返回值用来确认删除指定元素是否成功)
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
boolean b = collection.remove("集合一0");
System.out.print(b);
System.out.println(collection);
输出结果:
true[集合一1, 集合一2, 集合一3, 集合一4]
---------------------------------------------------------------------------------
boolean removeAll(Collection<?> c)
删除指定集合中包含的所有此集合的元素(可选操作)。
(此方法的返回值用来确认删除元素是否成功)
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
Collection collection2 = new ArrayList();
for(int i = 0;i<5;i++) {
collection2.add("集合二"+i);
}
collection.addAll(collection2);
//collection中collection2的元素将被删除
boolean b = collection.removeAll(collection2);
System.out.print(b);
System.out.println(collection);
输出结果:
true[集合一0, 集合一1, 集合一2, 集合一3, 集合一4]
---------------------------------------------------------------------------------
常用方法(判断)
boolean contains(Object o)
如果此集合包含指定的元素,则返回 true 。
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
boolean b = collection.contains("集合一4");
System.out.print(b);
System.out.println(collection);
输出结果:
true[集合一0, 集合一1, 集合一2, 集合一3, 集合一4]
---------------------------------------------------------------------------------
boolean containsAll(Collection<?> c)
如果此集合包含指定 集合中的所有元素,则返回true。
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
Collection collection2 = new ArrayList();
for(int i = 0;i<5;i++) {
collection2.add("集合二"+i);
}
collection.addAll(collection2);
boolean b = collection.containsAll(collection2);
System.out.print(b);
System.out.println(collection);
输出结果:
true[集合一0, 集合一1, 集合一2, 集合一3, 集合一4, 集合二0, 集合二1, 集合二2, 集合二3,
集合二4]
---------------------------------------------------------------------------------
boolean isEmpty()
如果此集合不包含元素,则返回 true 。
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
collection.clear();
boolean b = collection.isEmpty();
System.out.print(b);
System.out.println(collection);
输出结果:
true[]
---------------------------------------------------------------------------------
常用方法(转换)
Object[] toArray()
返回一个包含此集合中所有元素的数组。
例:
public static void main(String[] args) {
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
System.out.println(collection.toArray());
print(collection.toArray());
}
public static void print(Object[] objects) {
for(int i = 0;i<objects.length;i++) {
System.out.print(objects[i]+" ");
}
}
输出结果:
[Ljava.lang.Object;@7852e922
集合一0 集合一1 集合一2 集合一3 集合一4
---------------------------------------------------------------------------------
定义:迭代器是一个接口,用来遍历数组。
迭代器是一次性的,入如果需要再次迭代需要重新创建一个新的的迭代器。
由于迭代器在ArrayList中是通过内部类实现的,所以只能由当前创建出来迭代器的集合来使用。
迭代器存在一个游标(类似指针),其默认位置存在于集合中第一个元素之前,其作用是查询和取值。
暂存在迭代器中的元素的类型为Object类型。
在迭代过程中,不能对集合做任何操作。
boolean hasNext()
如果迭代具有更多元素,则返回 true 。
E next()
返回迭代中的下一个元素。
例:
Collection collection = new ArrayList();
for(int i = 0;i<5;i++) {
collection.add("集合一"+i);
}
//创建迭代器
Iterator iter = collection.iterator();
//这里判断是否存在下一个元素
while(iter.hasNext()) {
//这里游标所在位置的下一个元素取出,并将原Object类型的元素强转为String类型
String str = (String)iter.next();
System.out.print(str+" ");
}
输出结果:
集合一0 集合一1 集合一2 集合一3 集合一4
---------------------------------------------------------------------------------