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;}

  • 相关阅读:
    UEditor 编辑模板
    Task ProgressBar模拟现实完成后显示TextBox
    Java Lambda map返回部分属性
    Socket编程
    字节流和字符流
    File类
    volatile的作用和原理
    Java四种引用类型
    ThreadLocal
    孤儿进程和僵尸进程
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/6083702.html
Copyright © 2011-2022 走看看