java 小时时间就近取整

/** * 时间就近取整 * 08:00 -> 08:00, * 08:20 -> 08:30, * 08:30 -> 08:30, * 08:45 -> 09:00, * 23:56 -> 00:00 * * @param time * @return outTime */public static String getCompleteTime(String time) {    String hour = "00";//小时    String minutes = "00";//分钟    String outTime = "00:00";    StringTokenizer st = new StringTokenizer(time, ":");    List<String> inTime = new ArrayList<String>();    while (st.hasMoreElements()) {        inTime.add(st.nextToken());    }    hour = inTime.get(0).toString();    minutes = inTime.get(1).toString();    if (Integer.parseInt(minutes) > 30) {        hour = (Integer.parseInt(hour) + 1) + "";        outTime = hour + ":00";        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");        try {            outTime = sdf.format(sdf.parse(outTime));        } catch (Exception e) {            e.printStackTrace();        }    } else if (Integer.parseInt(minutes) == 00) {        outTime = hour + ":00";        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");        try {            outTime = sdf.format(sdf.parse(outTime));        } catch (Exception e) {            e.printStackTrace();        }    } else if (Integer.parseInt(minutes) <= 30 && Integer.parseInt(minutes) != 00) {        outTime = hour + ":30";        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

        try {            outTime = sdf.format(sdf.parse(outTime));        } catch (Exception e) {            e.printStackTrace();        }    }    return outTime;}

原文地址:https://www.cnblogs.com/zhangheliang/p/12611683.html

时间: 2024-08-18 10:00:01

java 小时时间就近取整的相关文章

java的四种取整方法

java 中取整操作提供了四种方法:分别是: public static double ceil(double a)//向上取整  public static double floor(double a)//向下取整  public static long round(double a)//四舍五入取整  public static double rint(double a)//最近取整   第一种:ceil是天花板的意思,表示向上取整.   测试: System.out.println(Mat

关于erlang的向上取整和向下取整

在erlang的API中,erlang:trunc/1 是就近取整,erlang:round/1是四舍五入的, 整理下:对于正数的向上和向下取整, 1 %% 向上取整 2 ceil(N) -> 3 T = trunc(N), 4 case N == T of 5 true -> T; 6 false -> 1 + T 7 end. 1 %% 向下取整 2 floor(X) -> 3 T = trunc(X), 4 case (X < T) of 5 true -> T

java小数取整

java中提供三种小数取整方式 Math.ceil() Math.floor() Math.round() ceil:天花板,向上quzheng Math.ceil(11.5) = 12 Math.ceil(-11.5) = -11 floor:地,向下取整 Math.floor(11.5) = 11 Math.floor(-11.5) = -12 round:4舍5入 Math.round(x + 0.5),再向下取整 当 x = 11.5 时,Math.round(x + 0.5) = Ma

java取整和java四舍五入方法

1 import java.math.BigDecimal; 2 import java.text.DecimalFormat; 3 4 public class TestGetInt{ 5 public static void main(String[] args){ 6 double i=2, j=2.1, k=2.5, m=2.9; 7 System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i)); 8 Syst

java取整和java四舍五入方法 转自董俊杰

import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetInt{ public static void main(String[] args){    double i=2, j=2.1, k=2.5, m=2.9;    System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i));    System.out.

取整的一些方法总结(java)

Math类中提供了三个与取整有关的方法:ceil.floor.round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11:floor的英文意义是地板,该方法就表示向下取整,所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12:最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(

Java 取整

向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws Exception { double a = 35; double b = 20; double c = a / b; System.out.println("c===>" + c); // 1.75 System.out.println("c===>"

java 向上、向下取整,四舍五入

public class MathTest { public static void main(String[] args) { // 四舍五入 long round = Math.round(1.499); long round2 = Math.round(1.5); System.out.println(round); System.out.println(round2); // 向上取整 int s = (int)Math.ceil(1.1); System.out.println(s);

java日期时间格式转换

Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateForma