//获得当天11点的时间
public static Date getTimes11(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();
return date;
}
date.getTime()返回的是long类型的毫秒数
/**string 转为 Date
* @param strTime 要转换的string类型的时间 strTime的时间格式必须要与formatType的时间格式相同
* @param format 要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 // HH时mm分ss秒,
* @return
*/
public static Date string2Date(String strTime,String format){
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = null;
try {
date =formatter.parse(strTime);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
将毫秒转换为标准日期格式
public static String ms2Date(long _ms){
Date date = new Date(_ms);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return format.format(date);
}
//标准时间转换为时间戳
public static long Date2ms(String _data){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(_data);
return date.getTime();
}catch(Exception e){
return 0;
}
}
网友评论