格式化时间
Date date =new Date();
StringstrDateFormat = "yyyy-MM-dd HH:mm:ss";
(HH:为24小时制,hh为12小时制)
SimpleDateFormatsdf = newSimpleDateFormat(strDateFormat);
System.out.PRintln(sdf.format(date));
获取当前时间
SimpleDateFormatsdf = newSimpleDateFormat();// 格式化时间
sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
Datedate = newDate();// 获取当前时间
System.out.println("现在时间:" + sdf.format(date));// 输出已经格式化的现在时间(24小时制)
获取年份月份
Calendarcal =Calendar.getInstance();
intday =cal.get(Calendar.DATE);
intmonth =cal.get(Calendar.MONTH) + 1;
intyear =cal.get(Calendar.YEAR);
intdow =cal.get(Calendar.DAY_OF_WEEK); //一周的第几天
intdom =cal.get(Calendar.DAY_OF_MONTH);//一个月的第几天
intdoy =cal.get(Calendar.DAY_OF_YEAR); //一年的第几天
时间戳转换成时间:
LongtimeStamp = System.currentTimeMillis();//获取当前时间戳
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd"); //定义格式
Stringsd = sdf.format(newDate(Long.parseLong(String.valueOf(timeStamp))));// 时间戳转换成时间
新闻热点
疑难解答