时间类型的转化:
常用是时间:
问题一: 时间格式转化问题:
时间格式转化为字符串:
就是时间的显示,以及显示格式的问题:
1.字符串专换为Date
SimpleDateFormat format = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss_SSS);
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
}
return date;
2. 日期格式转换为字符串
SimpleDateFormat format = new SimpleDateFormat(yyyy_MM_dd);
String str = format.format(date);
return str;
问题二:时间计算问题:
时间计算: 某个时间, 几天后的一个日期:
某一天,几天后 ,的日期.
关于时间的计算问题同一转化为毫秒值进行计算.
1. 日期加time 时间
public static Date getDataByDateAndTime(Date date, Time time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR,time.getHours());
calendar.set(Calendar.MINUTE,time.getMinutes());
calendar.set(Calendar.SECOND,time.getSeconds());
return calendar.getTime();
}
其实, Time 类是Date类的一个子类;
Time的毫秒值 也是某一刻到 1970 年的毫秒值, 只是舍弃了日期部分.
2. @描述:获取多少小时以后的时间
public static Time getAfterTimeHour(Time time,double h){
Time newTime=new Time((long) (time.getTime()+h*60*60*1000));
return newTime;
}
@描述:获取多少小时以后的时间
public static Date getAfterDateHour(Date date,double h){
return new Date((long) (date.getTime()+h*60*60*1000));
}
各种转化: