1、Map
1.1、Map中entrySet()方法
for (Entry<String, Object> entry : map.entrySet()) { System.out.PRintln(entry.getKey()); //循环的取出每个键值对中的键
System.out.println(entry.getValue()); //循环的取出每个键值对中的值
}
1.2、Map中keySet()方法
for(String key:map.keySet()){
System.out.println(key); //在键集合中循环的取出每个键
}
1.3、Map中的values()方法:返回的是一个Collection的集合
List<Object> list = new ArrayList<Object>();
list.addAll(map.values());
2、java时间部分。建议使用日历时间,因为Date类之后,开发人员发现很多不合理的地方,就开发了日历类,Date类很多方法都过期了。
//判断年份等于2018或者2019. 就用当前日期+postime的小时分钟秒 if(deviceMsg!=null && deviceMsg.getPosTime()!=null){ Calendar posCa=Calendar.getInstance(); //实例化日历类对象实例 posCa.setTime(deviceMsg.getPosTime()); int year = posCa.get(Calendar.YEAR); if(year==2018 || year==2019){ Date date = new Date(); String nowDate = DateFormatUtils.getDateString(date); int hour = posCa.get(Calendar.HOUR_OF_DAY); int minute = posCa.get(Calendar.MINUTE); int second = posCa.get(Calendar.SECOND); String nowDateTime = nowDate +" "+hour+":"+minute+":"+second; Timestamp newPosTime= new Timestamp(DateFormatUtils.getDateTime(nowDateTime).getTime()); deviceMsg.setPosTime(newPosTime); logger.error("@2018ErrorData:"+reqContent); } }
//date:要处理的原始日期
// amount:对年份改变的数量,比如1:增加1年,-1:减少1年
public static Date addYear(Date date, int amount) { return add(date, Calendar.YEAR, amount); } public static Date addMonth(Date date, int amount) { return add(date, Calendar.MONTH, amount); } public static Date addDay(Date date, int amount) { return add(date, Calendar.DATE, amount); } public static Date addWeek(Date date, int amount) { return add(date, Calendar.WEEK_OF_YEAR, amount); } public static Date addHour(Date date, int amount) { return add(date, Calendar.HOUR, amount); } public static Date addSecond(Date date, int amount) { return add(date, Calendar.SECOND, amount); }
public static Date add(Date date, int field, int amount) { try { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(field, amount); return c.getTime(); } catch (Exception e) { } return null; } public static Timestamp add(Timestamp date, int field, int amount) { try { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(field, amount); return new Timestamp(c.getTimeInMillis()); } catch (Exception e) { } return null; }
新闻热点
疑难解答