zoukankan      html  css  js  c++  java
  • [Java]某日期时间加上若干分钟得到新的日期时间

    使用Java自带类库实现日期时间增减还是比自己人工拆分编写要牢靠,代码也简洁多了。

    下面代码实现了在原有日期时间上加上一些分钟得到新的日期时间的功能,稍加改造还可以实现逆向运算。

    代码:

    package datetime;
    
    import java.text.MessageFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class TimeTest {
        public static String getNewTime(String datetime,String addMinutes) throws Exception{
            SimpleDateFormat formatter =new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
            
            Date originalDate = formatter.parse(datetime);
            
            Calendar newTime = Calendar.getInstance();
            newTime.setTime(originalDate);
            newTime.add(Calendar.MINUTE,Integer.parseInt(addMinutes));//日期加n分
            
            Date newDate=newTime.getTime();
            String retval = formatter.format(newDate);
            
            return retval;
        }
        
        
        public static void main(String[] args) throws Exception {
            String[][] arrays= {
                    {"2019年11月15日 08时63分","10"},
                    {"2019年11月15日 08时03分","15"},
                    {"2019年11月15日 09时63分","20"},
                    {"2019年11月15日 24时63分","25"},
                    {"2019年11月15日 08时63分","30"},
                    {"2019年11月15日 18时63分","35"},
                    {"2019年11月15日 08时63分","40"},
                    {"2019年11月15日 08时63分","45"},
                    {"2019年11月15日 15时00分","50"},
                    {"2019年11月15日 18时01分","60"},
                    {"2019年11月15日 18时01分","360"},
            };
            
            for(String[] arr:arrays) {
                String template=" {0} + {1}分 = {2}";
                Object[] objs={arr[0],arr[1],getNewTime(arr[0],arr[1])};
                System.out.println(MessageFormat.format(template, objs));
            }
        }
    }

    输出:

     2019年11月15日 08时63分 + 10分 = 2019年11月15日 09时13分
     2019年11月15日 08时03分 + 15分 = 2019年11月15日 08时18分
     2019年11月15日 09时63分 + 20分 = 2019年11月15日 10时23分
     2019年11月15日 24时63分 + 25分 = 2019年11月16日 01时28分
     2019年11月15日 08时63分 + 30分 = 2019年11月15日 09时33分
     2019年11月15日 18时63分 + 35分 = 2019年11月15日 19时38分
     2019年11月15日 08时63分 + 40分 = 2019年11月15日 09时43分
     2019年11月15日 08时63分 + 45分 = 2019年11月15日 09时48分
     2019年11月15日 15时00分 + 50分 = 2019年11月15日 15时50分
     2019年11月15日 18时01分 + 60分 = 2019年11月15日 19时01分
     2019年11月15日 18时01分 + 360分 = 2019年11月16日 00时01分

    附录:SimpleFormat输出格式定义,这个必不可少:

    yyyy:年
    MM:月
    dd:日
    hh:1~12小时制(1-12)
    HH:24小时制(0-23)
    mm:分
    ss:秒
    S:毫秒
    E:星期几

    --END-- 2019-11-15 18:35

  • 相关阅读:
    根据指定的编码格式返回请求的参数集合
    【C++ 学习笔记】 size_t 和 int
    C++ 编译错误
    【Flex】 加载Gif文件
    【C++ 学习笔记】:STLmap
    【C++ 学习笔记】:MFC(细节部分)
    小卡的土豆园开园了~
    [论文笔记] A novel reduction approach to analyzing QoS of workflow processes (Concurrency and Computation: Practice and Experience, 2009)
    [论文收集] WWW2008 相关或感兴趣的论文
    [论文泛读] Accurate and efficient stochastic reliability analysis of composite services using their compact Markov reward model representations (SCC, 2007)
  • 原文地址:https://www.cnblogs.com/heyang78/p/11868279.html
Copyright © 2011-2022 走看看