Calendar获取时间用法
Calendar calendar = Calendar.getInstance();
//获取时间
calendar.get(Calendar.YEAR); //年
calendar.get(Calendar.MONTH); //月(别忘了+1哦!)
calendar.get(Calendar.DATE); //日
calendar.get(Calendar.HOUR_OF_DAY); //小时(24制)
calendar.get(Calendar.HOUR); //小时(12制)
calendar.get(Calendar.MINUTE); //分
calendar.get(Calendar.SECOND); //秒
calendar.get(Calendar.DAY_OF_WEEK); //星期
calendar.get(Calendar.AM); //上午
calendar.get(Calendar.PM); //下午
//设置时间
calendar.set(2013, 5, 4, 13, 44, 51);//年月日时分秒(月份0代表1月)
calendar.set(Calendar.YEAR, 2014); //年
............以此类推.................
System.out.println(calendar.getTimeInMillis()); //得到毫秒值
//亦可以直接使用SimpleDateFormat,获取当前时间的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(calendar.getTime())); //2017-03-14 14:05:54
//年月日转换成时间戳(2020-10-10)
String time="2020-10-10";
Calendar calendar = Calendar.getInstance();
String[] times=time.split("-");
calendar.set(TypesUtil.asInt(times[0]), TypesUtil.asInt(times[1])-1, TypesUtil.asInt(times[2]), 0, 0, 0);
//转换时间戳
long curtime = calendar.getTimeInMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
//昨日开始时间
long start_time = calendar.getTimeInMillis();
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.SECOND, -1);
//昨日结束时间
long end_time = calendar.getTimeInMillis();