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

posted on 2016-11-20 22:03  我要的明天  阅读(6789)  评论(0编辑  收藏  举报

导航