在 Java HashMap 中设置日期值?
创建日历实例和日期对象-
Calendar cal = Calendar.getInstance(); Date date = new Date(); cal.setTime(date);
现在,创建一个HashMap并存储日期值-
LinkedHashMaphashMap = new LinkedHashMap (); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));
示例
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
public class Demo {
public static void main(String[] argv) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
System.out.println("Date = "+date);
cal.setTime(date);
LinkedHashMaphashMap = new LinkedHashMap();
hashMap.put("year", cal.get(Calendar.YEAR));
hashMap.put("month", cal.get(Calendar.MONTH));
hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));
System.out.println("HashMap (Date) = "+hashMap);
hashMap.put("hour", cal.get(Calendar.HOUR_OF_DAY));
hashMap.put("minute", cal.get(Calendar.MINUTE));
hashMap.put("second", cal.get(Calendar.SECOND));
hashMap.put("millisecond", cal.get(Calendar.MILLISECOND));
System.out.println("HashMap (DateTime) = "+hashMap);
}
} 输出结果Date = Fri Apr 19 17:45:24 IST 2019
HashMap (Date) = {year=2019, month=3, day=19}
HashMap (DateTime) = {year=2019, month=3, day=19, hour=17, minute=45, second=24, millisecond=98}热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短