zoukankan      html  css  js  c++  java
  • 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;}

  • 相关阅读:
    Java 常见异常种类
    关系型数据库和非关系型数据库的区别
    HTTP中Get与Post的区别
    经典排序算法详细总结。
    新浪实时股票数据接口http://hq.sinajs.cn/list=股票代码
    WinIo驱动级键盘模拟编程
    Logback_日志使用详解(转)
    Java_位运算(移位、位与、或、异或、非)
    算法_bitmap算法
    Selenium_IEDriver操作sendkeys输入速度太慢
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/6083702.html
Copyright © 2011-2022 走看看