• Java获取任意时间、时间字符串


    /*
      * 获取时间字符串
    */
    public
    String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(); return sdf.format(date); } public String getFutureTime(int changeDay) { Calendar date = Calendar.getInstance(); String year = String.valueOf(date.get(Calendar.YEAR)); String month = String.valueOf(date.get(Calendar.MONTH) + 1); String day = String.valueOf(date.get(Calendar.DAY_OF_MONTH) + changeDay); String hour = String.valueOf(date.get(Calendar.HOUR)); String minute = String.valueOf(date.get(Calendar.MINUTE)); String second = String.valueOf(date.get(Calendar.SECOND)); while (Integer.valueOf(day) > date.getActualMaximum(Calendar.DAY_OF_MONTH)) { day = String.valueOf(Integer.valueOf(day) - date.getActualMaximum(Calendar.DAY_OF_MONTH)); month = String.valueOf(Integer.valueOf(month) + 1); if (Integer.valueOf(month) > 12) { month = String.valueOf(Integer.valueOf(month) - 12); year = String.valueOf(Integer.valueOf(year) + 1); } } if (month.length() < 2) { month = "0" + month; } if (day.length() < 2) { day = "0" + day; } if (hour.length() < 2) { hour = "0" + hour; } if (minute.length() < 2) { minute = "0" + minute; } if (second.length() < 2) { second = "0" + second; } String currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; return currentTime; }

    /*
         * 获取自定义时间
         */
        public Date getTime(int year,int month,int day,int hour,int minute,int second) {
            
            Calendar calendar =  Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month-1);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
            
            Date date = calendar.getTime();
            return date;
        }

    /* * 获取自定义时间 */public Date getTime(int year,int month,int day,int hour,int minute,int second) {Calendar calendar =  Calendar.getInstance();calendar.set(Calendar.YEAR, year);calendar.set(Calendar.MONTH, month-1);calendar.set(Calendar.DAY_OF_MONTH, day);calendar.set(Calendar.HOUR_OF_DAY, hour);calendar.set(Calendar.MINUTE, minute);calendar.set(Calendar.SECOND, second);Date date = calendar.getTime();return date;}

  • 相关阅读:
    Django 支付宝付款接口的使用
    Django 处理跨域的配置、前台处理ajax
    pip安装源
    Django 缓存机制
    Django 配置使用日志
    Celery框架的基本使用方法
    python 虚拟环境的搭建
    Django 后台管理xadmin
    Python学习之路_day_16(模块搜索路径,开发规范)
    Python学习之路_day_15(模块)
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/6083702.html
Copyright © 2020-2023  润新知