- 首页 > it技术 > >
java-时间格式化|java-时间格式化 整5分钟,整10分钟
public SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// time_str 为时间字符串
public String minute_fromt (String time_str,Integer is_gt) {
// start_time is_gt 为 1 ;
end_time is_gt为 0
is_gt = is_gt*5;
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(df1.parse(time_str));
} catch (ParseException e) {
e.printStackTrace();
}
int minute = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
if (sec == 0) {
is_gt = 0;
}
// 计算整10分钟 把5换成10
minute = Math.round(minute/5*5);
//计算5的整数分钟calendar.set(Calendar.MINUTE, minute+is_gt);
calendar.set(Calendar.SECOND, 0);
String time =df1.format(calendar.getTime());
return time;
}
public String hour_fromt (String time_str,Integer is_gt) {
// start_time is_gt 为 1 ;
end_time is_gt为 0
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(df1.parse(time_str));
} catch (ParseException e) {
e.printStackTrace();
}
int hour = calendarget(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
if (sec == 0 && minute==0) {
is_gt = 0;
}
calendar.set(Calendar.HOUR, hour+is_gt);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
String time =df1.format(calendar.getTime());
return time;
}
public String day_fromt (String time_str,Integer is_gt) {
// start_time is_gt 为 1 ;
end_time is_gt为 0
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(df1.parse(time_str));
} catch (ParseException e) {
e.printStackTrace();
}
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int sec = calendarget(Calendar.SECOND);
if (sec == 0 && minute==0 && hour==0) {
is_gt = 0;
}
calendar.set(Calendar.DATE, day+is_gt);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
String time =df1.format(calendar.getTime());
return time;
}
推荐阅读