博客
关于我
把BigDecimal类型的分钟数转成xx:xx的时间格式
阅读量:225 次
发布时间:2019-02-28

本文共 748 字,大约阅读时间需要 2 分钟。

数据库存储的是分钟数,前端需要将其展示为标准的时间格式(xx:xx)。以下是对相关代码的分析和优化方案:

代码分析

public static void main(String[] args) {    BigDecimal time = new BigDecimal(69);    BigDecimal hour = time.divide(MAXMINUTE, 0, BigDecimal.ROUND_DOWN);    BigDecimal minute = time.subtract(hour.multiply(MAXMINUTE));    String minuteStr = "00";    if (minute.compareTo(BigDecimal.ZERO) != 0) {        if (minute.compareTo(TEN) < 0) {            minuteStr = "0" + minute.toString();        } else {            minuteStr = minute.toString();        }    }    String result = hour + ":" + minuteStr;    System.out.println(result);}

问题分析

该代码的主要目标是将存储的分钟数转换为标准的时间格式。具体来说,它需要将分钟数分割为小时和分钟两部分,并按照"HH:MM"的格式输出。

优化方案

  • 代码简化:通过简化代码逻辑,使其更易读和维护。例如,可以通过调整格式化字符串来减少条件判断。
  • 异常处理:增加对异常情况的处理,例如当分钟数为负数或超出范围时,避免程序
  • 转载地址:http://rjnp.baihongyu.com/

    你可能感兴趣的文章
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>