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;
}
java-时间格式化 整5分钟,整10分钟
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...