java 时间日期工具类

来源:互联网 发布:赛鸽登记软件 编辑:程序博客网 时间:2024/05/22 06:53
import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.*;public class TimeUtils{    public TimeUtils()    {    }//当前月第一天    public static Timestamp getCurrtentMonthFirstDay()    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");        Calendar cal = Calendar.getInstance();        cal.add(2, 0);        cal.set(5, 1);        String first = dateFormat.format(cal.getTime());        return Timestamp.valueOf(first);    }//当前月最后一天    public static Timestamp getCurrtentMonthLastDay()    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");        Calendar cal = Calendar.getInstance();        cal.add(2, 1);        cal.set(5, 0);        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }//n<0  几天前;n>0几天后    public static Timestamp getBeforeNDayFirst(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");        Calendar cal = Calendar.getInstance();        cal.setTime(new Date());        cal.add(5, n);        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }    public static Timestamp getBeforeNDayLast(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");        Calendar cal = Calendar.getInstance();        cal.add(5, n);        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }//n<0  几周前;n>0几周后    public static Timestamp getBeforeNWeekFirst(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");        Calendar cal = Calendar.getInstance();        cal.setTime(new Date());        int dayWeek = cal.get(7);        cal.add(7, n * 7);        cal.setFirstDayOfWeek(2);        cal.add(5, cal.getFirstDayOfWeek() - dayWeek);        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }    public static Timestamp getBeforeNWeekLast(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");        Calendar cal = Calendar.getInstance();        int dayWeek = cal.get(7);        cal.add(7, n * 7);        cal.setFirstDayOfWeek(2);        cal.add(5, (cal.getFirstDayOfWeek() - dayWeek) + 6);        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }//n<0  几月前;n>0几月后    public static Timestamp getBeforeNMonthFirst(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");        Calendar cal = Calendar.getInstance();        cal.add(2, n);        cal.set(5, 1);        String first = dateFormat.format(cal.getTime());        return Timestamp.valueOf(first);    }    public static Timestamp getBeforeNMonthLast(int n)    {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");        Calendar cal = Calendar.getInstance();        cal.add(2, n);        cal.set(5, cal.getActualMaximum(5));        String last = dateFormat.format(cal.getTime());        return Timestamp.valueOf(last);    }//************上面是返回时间点,下面返回对应的时间点集合    public static List getNdays(Long n)    {        List daysList = new ArrayList();        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");        Calendar cal = null;        for(int i = 0; (long)i < n.longValue(); i++)        {            cal = Calendar.getInstance();            cal.setTime(new Date());            cal.add(5, -i);            String day = dateFormat.format(cal.getTime());            daysList.add(day);        }        return daysList;    }    public static List getNweeks(Long n)    {        List weeksList = new ArrayList();        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");        Calendar cal = null;        for(int i = 0; (long)i < n.longValue(); i++)        {            cal = Calendar.getInstance();            cal.setTime(new Date());            int dayWeek = cal.get(7);            cal.add(7, -i * 7);            cal.setFirstDayOfWeek(2);            cal.add(5, cal.getFirstDayOfWeek() - dayWeek);            String week = dateFormat.format(cal.getTime());            weeksList.add(week);        }        return weeksList;    }    public static List getNmonths(Long n)    {        List monthsList = new ArrayList();        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");        Calendar cal = null;        for(int i = 0; (long)i < n.longValue(); i++)        {            cal = Calendar.getInstance();            cal.setTime(new Date());            cal.add(2, -i);            cal.set(5, 1);            String month = dateFormat.format(cal.getTime());            monthsList.add(month);        }        return monthsList;    }}


原创粉丝点击