Java操作日期时间范例代码大全

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
 
import org.apache.commons.lang.StringUtils;
 
/**
 * 日期工具类
 * 
 * @author yugc
 */
public final class DateUtil {
 
    private DateUtil() {
 
    }
 
    /**
     * yyyy-MM-dd
     */
    public static final String FORMAT1 = "yyyy-MM-dd";
 
    /**
     * yyyy.MM.dd
     */
    public static final String FORMAT2 = "yyyy.MM.dd";
 
    /**
     * yyyy/MM/dd
     */
    public static final String FORMAT3 = "yyyy/MM/dd";
 
    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String FORMAT4 = "yyyy-MM-dd HH:mm";
 
    /**
     * yyyy.MM.dd HH:mm
     */
    public static final String FORMAT5 = "yyyy.MM.dd HH:mm";
 
    /**
     * yyyy/MM/dd HH:mm
     */
    public static final String FORMAT6 = "yyyy/MM/dd HH:mm";
 
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String FORMAT7 = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * YYYY-MM-dd HH-mm-ss
     */
    public static final String FORMAT15 = "YYYY-MM-dd HH-mm-ss";
 
    /**
     * yyyy.MM.dd HH:mm:ss
     */
    public static final String FORMAT8 = "yyyy.MM.dd HH:mm:ss";
 
    /**
     * yyyy/MM/dd HH:mm:ss
     */
    public static final String FORMAT9 = "yyyy/MM/dd HH:mm:ss";
 
    /**
     * yyyy_MM_dd_HH_mm_ss
     */
    public static final String FORMAT10 = "yyyy_MM_dd_HH_mm_ss";
 
    /**
     * yy-MM-dd
     */
    public static final String FORMAT11 = "yy-MM-dd";
 
    /**
     * yyyyMMdd
     */
    public static final String FORMAT12 = "yyyyMMdd";
 
    /**
     * yyyyMMddHHmmss
     */
    public static final String FORMAT13 = "yyyyMMddHHmmss";
 
    /**
     * yyyyMM
     */
    public static final String FORMAT14 = "yyyyMM";
 
    public static Date getCurrentDate() {
 
        return new Date(System.currentTimeMillis());
    }
 
    public static Date getYesterDay() {
 
        return addDay(new Date(System.currentTimeMillis()), -1);
    }
 
    public static String getYesterDayString() {
 
        return parseDateToString(addDay(new Date(System.currentTimeMillis()), -1), FORMAT1);
    }
 
    /**
     * 得到年月日的路径
     * @return
     */
    public static String getThisYearMonthDay(String dateString) {
 
        Date date = parseStringToDate(dateString, FORMAT12);
 
        return DateUtil.getYear(date) + "/" + DateUtil.getMonth(date) + "/" + DateUtil.getDay(date) + "/";
    }
 
    /**
     * 返回当前日期或时间
     * 
     * @param format
     * @return
     */
    public static String getCurrentDate(String format) {
 
        if (StringUtils.isBlank(format)) {
            format = FORMAT1;
        }
 
        Date date = new Date();
 
        SimpleDateFormat formatter = new SimpleDateFormat(format);
 
        String currentTime = formatter.format(date);
 
        return currentTime;
    }
 
    /**
     * 返回当前时间
     * 
     * @return
     */
    public static String getCurrentTime() {
 
        String datetime = getCurrentDate(FORMAT7);
        String time = datetime.substring(datetime.indexOf(" ") + 1);
 
        return time;
    }
 
    /**
     * 返回当前时间加随机数
     * 
     * @return
     */
    public static String getCurrentDateTimeRandom() {
 
        String datetime = getCurrentDate(DateUtil.FORMAT10) + "_" + Math.random();
 
        return datetime;
    }
 
    /**
     * 返回当前时间和日期
     * @return
     */
    public static String getCurrentDateTimeString() {
 
        return getCurrentDate(DateUtil.FORMAT7);
 
    }
 
    /**
     * 返回当前日期
     * 
     * @return
     */
    public static Date getCurrentDateTime() {
 
        String datetime = getCurrentDate(FORMAT7);
 
        return parseStringToDate(datetime, "");
    }
 
    public static Timestamp getCurrentTimestamp() {
 
        String datetime = getCurrentDate(FORMAT7);
 
        return parseStringToTimestamp(datetime, "");
    }
 
    public static Timestamp getCurrentTimestamp(String format) {
 
        String datetime = getCurrentDate(format);
 
        return parseStringToTimestamp(datetime, "");
    }
 
    /**
     * 日期转换为字符串
     * 
     * @param date 日期
     * @param format 格式
     * @return 返回字符型日期
     */
    public static String parseDateToString(Date date, String format) {
 
        String result = "";
        DateFormat formatter = null;
        try {
            if (date != null) {
                if (StringUtils.isBlank(format)) {
                    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                }
                else {
                    formatter = new SimpleDateFormat(format);
                }
                result = formatter.format(date);
            }
        }
        catch (Exception e) {
        }
 
        return result;
    }
 
    /**
     * 时间1-时间2的毫秒
     * 
     * @param t1
     * @param t2
     * @return
     */
    public static long between(Timestamp t1, Timestamp t2) {
 
        if ((t1 != null) && (t2 != null)) {
            return t1.getTime() - t2.getTime();
        }
 
        return 0;
    }
 
    /**
     * 两个日期date1-date2相减,相差的天数
     * 
     * @param date1
     *             日期
     * @param date2
     *             日期
     * @return 返回相减后的日期
     */
    public static int betweenTwoDates(Date date1, Date date2) {
 
        return (int) ((getMillis(date1) - getMillis(date2)) / (24 * 3600 * 1000));
    }
 
    /**   
     * 将字符串转换为日期   
     *    
     * @param str   
     * @return   
     * @throws ParseException 
     */
    public static Date parseStringToDate(String str, String format) {
 
        DateFormat formatter = null;
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
 
            if (StringUtils.isBlank(format)) {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            else {
                formatter = new SimpleDateFormat(format);
            }
 
            try {
                date = formatter.parse(str);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }
        }
 
        return date;
    }
 
    /**
     * 返回2007到今年的年份
     * 
     * @return
     */
    public static List<Integer> get2007ToThisYear() {
 
        // 当前时间
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
 
        // 返回的年份
        List<Integer> the2007ToThisYearList = new ArrayList<Integer>();
        // 当前年
        int thisYear = c.get(Calendar.YEAR);
 
        for (int i = thisYear; i >= 2007; i--) {
            the2007ToThisYearList.add(i);
        }
 
        return the2007ToThisYearList;
    }
 
    /**
     *  获取当前月的前几个月份的时间
     * @param months 
     * @return
     */
    public static Date getDateBeforeMonths(int months) {
 
        // 当前时间
        Calendar c = Calendar.getInstance();
 
        c.add(Calendar.MONTH, -months);
 
        return c.getTime();
    }
 
    /**
     * 获取当前日期的前几天的时间
     * @param days
     * @return
     */
    public static Date getDateBeforeDays(int days) {
 
        // 当前时间
        Calendar c = Calendar.getInstance();
 
        c.add(Calendar.DATE, -days);
        return c.getTime();
    }
 
    /**
     * 返回1-12月份
     * 
     * @return
     */
    public static List<String> getAllMonth() {
 
        List<String> theMonthList = new ArrayList<String>();
 
        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                theMonthList.add("0" + i);
            }
            else {
                theMonthList.add("" + i);
            }
        }
 
        return theMonthList;
    }
 
    /**
     * 返回日期中的年份
     * 
     * @param date
     *             日期
     * @return 返回年份
     */
    public static int getYear(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.YEAR);
    }
 
    /**
     * 返回日期中的月份
     * 
     * @param date
     *             日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.MONTH) + 1;
    }
 
    /**
     * 返回日期中的日
     * 
     * @param date
     *             日期
     * @return 返回日
     */
    public static int getDay(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.DAY_OF_MONTH);
    }
 
    /**
     * 返回日期中的小时
     * 
     * @param date
     *             日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.HOUR_OF_DAY);
    }
 
    /**
     * 返回日期中的分钟
     * 
     * @param date
     *             日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.MINUTE);
    }
 
    /**
     * 返回日期中的秒钟
     * 
     * @param date
     *             日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.get(Calendar.SECOND);
    }
 
    /**
     * 返回日期代表的毫秒
     * 
     * @param date
     *             日期
     * @return 返回毫秒
     */
    public static long getMillis(Date date) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
 
        return c.getTimeInMillis();
    }
 
    /**
     * 返回当前日期代表的毫秒
     * 
     * @return
     */
    public static long getCurrentTimeMillis() {
 
        return System.currentTimeMillis();
    }
 
    public static Date addMonth(Date date, int month) {
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(c.MONTH, month);
 
        return c.getTime();
    }

猜你喜欢

转载自kissuyoyo.iteye.com/blog/2341280
今日推荐