我工作中所使用过的时间工具类

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    
    public static void main(String[] args) throws ParseException{
        
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date bb = sf.parse("2015-10-31");
        Date ee = sf.parse("2015-10-31");
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date d = sdf.parse("4/1/2020 0:0:0");
        System.out.println(d);
        
        System.out.println(bb.before(ee));
        System.out.println(bb.getTime() - ee.getTime());
        
        Date now = sf.parse(sf.format(new Date())); //系统时间(现在)
        System.out.println("系统时间是" + now);
        //获得两个日期相差多少个月
        System.out.println("获得两个日期相差多少个月: " + getDiffMonth(bb , ee));
        //获得两个日期相差多少天
        System.out.println("获得两个日期相差多少天: " + getDiffDays(bb , ee));
        //获得该日期中该月最大天数
        System.out.println("获得该日期中该月最大天数: " + getDaysOfMonth(ee));
        //获得本月第一天
        System.out.println("获得本月第一天" + getFirstDayOfMonth(bb));
        //获得本月最后一天
        System.out.println("获得本月最后一天" + getLastDayOfMonth(bb));        
        //获得本月最后一天
        getFirstAndLastDay(bb);
        //获得日期
        getDays(ee);
        //new一个date类型数据,返回的是String
        System.out.println(getCurrentTime());
        
    }
    //两个日期相差多少月份
    public static int getDiffMonth(Date beginDate,Date endDate){
        Calendar calbegin = Calendar.getInstance();
        Calendar calend = Calendar.getInstance();
        
        calbegin.setTime(beginDate);
        calend.setTime(endDate);
        
        int m_begin = calbegin.get(Calendar.MARCH) + 1; //获得开始月份日期
        int m_end = calend.get(Calendar.MONTH) + 1;
        
        int checkMonth = m_end - m_begin
                + (calend.get(Calendar.YEAR) - calbegin.get(Calendar.YEAR))
                * 12;
        return checkMonth ;
    }
    //两个日期相差多少天
    public static int getDiffDays(Date begin , Date end){
        Long times = end.getTime() - begin.getTime();
        int days = (int) (times/1000/60/60/24);
        return days;
    }
    
    //获得本月有多少天
    public static int getDaysOfMonth(Date begin){
        Calendar cal = Calendar.getInstance();
        cal.setTime(begin);
        int days = cal.getActualMaximum(Calendar.DATE);
        //int day = cal.get(Calendar.DAY_OF_YEAR); //获得今天是本年的第几天
        return days;
    }
    
    //获得本月第一天
    public static Date getFirstDayOfMonth(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
    
    //获得本月最后一天
    public static Date getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return cal.getTime();
    }
    
    //获得本月第一天与本月最后一天
    public static void getFirstAndLastDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)); //0是本月
        c.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println("本月的第一天: " + c.getTime());
        
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)+1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        System.out.println("本月的最后一天: " + c.getTime());
    }
    
    //开始日期和结束日期n天后的日期
    public static void getDays(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //开始时间是两天后的日期
        cal.add(Calendar.DAY_OF_MONTH, 2);
        Date begin = cal.getTime();    
        //结束日期是开始日期+2在加n天后的日期
        cal.add(Calendar.DAY_OF_MONTH, 11);
        Date end = cal.getTime();
        
        System.out.println(begin);
        System.out.println(end);
    }
    
    //new一个date类型的时间
    public static String getCurrentTime(){
        Calendar cal = Calendar.getInstance();
        //cal.add(Calendar.MONTH, -1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");    
        return sf.format(cal.getTime());
    }
    
    //获得当前月的第一天
    public static Date getFirstDayOfNow(){
        Calendar calendar = Calendar.getInstance();// 获取当前日期  
        calendar.add(Calendar.MONTH, 0);  
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天  
        calendar.set(Calendar.HOUR_OF_DAY, 0);  
        calendar.set(Calendar.MINUTE, 0);  
        calendar.set(Calendar.SECOND, 0);  
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
}

posted @ 2016-09-27 15:01  包远志  阅读(208)  评论(0编辑  收藏  举报