17.集合上
日期类
public static void main(String[] args) {
//毫秒值 1970.1.1
long time = System.currentTimeMillis();
// System.out.println(time);
//根据毫秒值创建Date对象
Date d = new Date(time);
System.out.println(d);//Mon Apr 13 20:16:13 CST 2020
System.out.println(d.getYear());//2020-1900 = 120 已经过时,不推荐使用
System.out.println(d.getMonth());//已经过时,不推荐使用,但是要强制性用没有关系
//0 一月 3四月 0-11
//日期对象转成字符串 DateFormat
// 获取日期字符串拥有的风格: FULL、LONG、MEDIUM 和 SHORT
// 如何使用对应的风格呢?在getDateInstance方法中传入对应的参数
DateFormat dateF = DateFormat.getDateInstance(DateFormat.SHORT);
String format = dateF.format(d);
// FULL :2020年4月13日 星期一
// LONG:2020年4月13日
// MEDIUM(默认):2020-4-13
// SHORT:20-4-13
System.out.println(format);//2020-4-13
//获取时间
DateFormat dateF2 = DateFormat.getTimeInstance(DateFormat.FULL);
String format2 = dateF2.format(d);
System.out.println(format2);
//获取日期时间
DateFormat dateF3 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT);
String format3 = dateF3.format(d);
System.out.println(format3);
}
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
/*
* map集合
java.util.GregorianCalendar[time=1586781630100,
areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
offset=28800000,dstSavings=0,useDaylight=false,
transitions=19,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=3
,WEEK_OF_YEAR=16,WEEK_OF_MONTH=3,DAY_OF_MONTH=13,
DAY_OF_YEAR=104,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,
AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=40,
SECOND=30,MILLISECOND=100,ZONE_OFFSET=28800000,DST_OFFSET=0]
*/
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.DAY_OF_MONTH));//13
System.out.println(c.get(Calendar.DAY_OF_WEEK));//2 (1星期天 2星期一)
}
字符串转日期对象
public static void main(String[] args) throws ParseException {
// "2020-4-3"转成日期对象
// 字符串--》日期对象 DateFormat的parse方法
// 日期对象 --》字符串 DateFormat 的format方法
String dateStr = "2020-4-3";//模拟用户发送来的日期
// 字符串的日期风格需要和dateFormat相对应才可以进行转换,否则抛出异常
// 如果写的字符串和四种风格不对应,可以使用一个自定义风格来进行转换
DateFormat df = DateFormat.getDateInstance();
Date d = df.parse(dateStr);
System.out.println(d);
String dateStr2 = "2020/1/3 21--03--10";
//给定的是自定义的风格
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh--mm--ss");
Date da = sdf.parse(dateStr2);
System.out.println(da);
}
注:日期类
1.日期转成字符串:DateFormat 的format方法
2.字符串转成日期:DateFormat 的parse方法
3.SimpleDateFormat什么时候用(给定的字符串日期表现形式不是系统四种风格时,自定义风格)
4.Calendar什么时候用(获取年,月,日)比如:我想判断今年是不是2020年
需要接收用户在前端选择年月日
collection集合
集合和数组的区别:
数组:长度是固定,数组存储的元素的类型是确定 :
数据类型[] 变量名 = new 数据类型[长度];–>数组类型
集合:长度是变化的,存储都是对象
private static void collectionAll() {
Collection c1 = new ArrayList();
Collection c2 = new ArrayList();
//往第一个集合中添加三个元素
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
// 往第二个集合中添加两个元素
c2.add("abc1");
c2.add("abc5");
// 添加所有
c1.addAll(c2);//复制,c2数组元素不做变化
// 判断是否全部包含
// boolean b = c1.containsAll(c2);//c1是否包含c2的所有元素
// System.out.println(b);
// 删除所有 把c1中有和c2中相同的元素就删除掉
// c1.removeAll(c2);
// 把没有重复给删掉
// c1.retainAll(c2);
System.out.println("c1="+c1);
System.out.println("c2="+c2);//实现类重写了toString方法,有对象自己的表现形式
}
//alt+shift+m
private static void collection1() {
//创建集合对象
Collection c = new ArrayList();
//往集合中添加元素
c.add("abc1");
c.add("abc2");
c.add("abc3");
//
//删除元素
// c.remove("abc1");
// 判断元素是否存在 如果存在为true,不存在为false
boolean b = c.contains("abc1");
// 判断是否是空集合
boolean b2 = c.isEmpty();
// 获取集合中的个数
int size = c.size();
// 清空集合
c.clear();
System.out.println(c);
}
迭代器
因为java中的集合有很多个,如果每个都有自己的取出方式,那么代价非常大,所以java在设计的时候,
// 需要找到集合的共性,都是一个个的去元素,但是在去元素之前需要先判断是否有元素,如果有,取一个,再判断....
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("abc1");
c.add("abc2");
c.add("abc3");
c.add("abc4");
//获取集合中的每个元素
// Iterator it = c.iterator();//迭代器
// while(it.hasNext()){
// System.out.println(it.next());
// }
//for循环的三段表达式,第一段:初始化表达式,第二段:判断表达式,第三段:循环后的表达式(可以省略)
for(Iterator it =c.iterator();it.hasNext();){
System.out.println(it.next());
}
for(int x=0;x<10;){}
//无限循环
for(;;){}
}