java 5分钟_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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值