DateTime日期操作

DateTime日期操作

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 static java.lang.Long.*;

/**
 * 时间工具类
 * Created by 张勇波 on 2016/8/18.
 */
public class DateTime {
    /**
     * 获取当前系统的日期和时间,返回格式为:2015-10-23 14:00:46
     *
     * @return String
     */
    public static String getDateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    /**
     * 获取当前系统的日期和时间,yyyyMMddHHmmss返回格式为:20151023140046
     *
     * @return String
     */
    public static String getDateTimeExt() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }

    /**
     * 获取当前系统的日期和时间,yyyyMMddHHmmssSSS返回格式为:20151023140046449
     *
     * @return String
     */
    public static String getDateTimeExts() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return df.format(new Date());
    }

    /**
     * 获取当前系统的日期,返回格式为:2015-10-23
     *
     * @return String
     */
    public static String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date());
    }

    /**
     * 获取当前系统的日期,返回格式为:2015/10/23
     *
     * @return String
     */
    public static String getDateExt() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        return df.format(new Date());
    }

    /**
     * 当前周
     *
     * @return String
     */
    public static String getWeek() {
        Calendar cd = Calendar.getInstance();
        return String.valueOf(cd.get(Calendar.WEEK_OF_YEAR));
    }

    /**
     * 当前年
     *
     * @return String
     */
    public static String getYear() {
        Calendar cd = Calendar.getInstance();
        return String.valueOf(cd.get(Calendar.YEAR));
    }

    /**
     * 当前月
     *
     * @return String
     */
    public static String getMonth() {
        Calendar cd = Calendar.getInstance();
        return String.valueOf(cd.get(Calendar.MONTH) + 1);
    }

    /**
     * 获取日期
     *
     * @return String
     */
    public static String getDay() {
        Calendar cd = Calendar.getInstance();
        return String.valueOf(cd.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * 获取当前时间差
     *
     * @return 天数
     */
    public static long differenceDay(String strDateTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null, date = null;
        try {
            now = df.parse(getDateTime());
            date = df.parse(strDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
        return l / (24 * 60 * 60 * 1000);
    }

    /**
     * 遍历输出2个日期之间的时间
     *
     * @param st_year   开始年份
     * @param st_month  开始月份
     * @param st_day    开始天
     * @param end_year  结束年份
     * @param end_month 结束月份
     * @param end_day   结束天
     * @return string[]
     */
    public static String[] ergodicDate(int st_year, int st_month, int st_day, int end_year, int end_month,
            int end_day) {
        StringBuilder sb = new StringBuilder();
        Calendar start = Calendar.getInstance();
        st_month = st_month - 1;
        start.set(st_year, st_month, st_day);
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end_month = end_month - 1;
        end.set(end_year, end_month, end_day);
        Long endTime = valueOf(end.getTimeInMillis());
        Long oneDay = valueOf(1000 * 60 * 60 * 24L);
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            sb.append(df.format(d)).append(",");
            time += oneDay;
        }
        return sb.toString().split(",");
    }

    /**
     * 获取当前时间差
     *
     * @return 小时
     */
    public static long differenceHour(String strDateTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null, date = null;
        try {
            now = df.parse(getDateTime());
            date = df.parse(strDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
        return l / (60 * 60 * 1000);
    }

    /**
     * 获取当前时间差
     *
     * @return 分钟
     */
    public static long differenceMin(String strDateTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null, date = null;
        try {
            now = df.parse(getDateTime());
            date = df.parse(strDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
        return l / (60 * 1000);
    }

    /**
     * 获取当前时间差
     *
     * @return 秒
     */
    public static long differenceS(String strDateTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null, date = null;
        try {
            now = df.parse(getDateTime());
            date = df.parse(strDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = (now != null ? now.getTime() : 0) - (date != null ? date.getTime() : 0);
        return l / 1000;
    }

    /**
     * 计算2个日期之间的月份
     *
     * @param minDate 开始日期
     * @param maxDate 结束日期
     * @return 月份list集合
     */
    public static ArrayList<String> getMonthBetween(String minDate, String maxDate) {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        try {
            min.setTime(sdf.parse(minDate));
            max.setTime(sdf.parse(maxDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        while (min.before(max)) {
            result.add(sdf.format(min.getTime()));
            min.add(Calendar.MONTH, 1);
        }
        return result;
    }
}

  目录