java中常用的时间处理类TimeUtil

Java时间处理实用类
本文介绍了一个Java实用类TimeUtil,用于处理各种日期和时间格式转换。该类提供了获取当前日期时间、格式化日期、计算相对日期等常用功能。
import java.text.ParseException;  
    import java.text.SimpleDateFormat;  
    import java.util.Calendar;  
    import java.util.Date;  
      
    /** 
     * 时间处理类 
     * @author Alan 
     * @version 2013-7-26 
     */  
    public class TimeUtil {  
      
        private Calendar calendar=Calendar.getInstance();  
          
          
        /** 
         * 得到当前的时间,时间格式yyyy-MM-dd 
         * @return 
         */  
        public String getCurrentDate(){  
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
            return sdf.format(new Date());  
        }  
          
        /** 
         * 得到当前的时间,自定义时间格式 
         * y 年 M 月 d 日 H 时 m 分 s 秒 
         * @param dateFormat 输出显示的时间格式 
         * @return 
         */  
        public String getCurrentDate(String dateFormat){  
            SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);  
            return sdf.format(new Date());  
        }  
          
        /** 
         * 日期格式化,默认日期格式yyyy-MM-dd 
         * @param date 
         * @return 
         */  
        public String getFormatDate(Date date){  
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
            return sdf.format(date);  
        }  
          
        /** 
         * 日期格式化,自定义输出日期格式 
         * @param date 
         * @return 
         */  
        public String getFormatDate(Date date,String dateFormat){  
            SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);  
            return sdf.format(date);  
        }  
        /** 
         * 返回当前日期的前一个时间日期,amount为正数 当前时间后的时间 为负数 当前时间前的时间 
         * 默认日期格式yyyy-MM-dd 
         * @param field 日历字段 
         * y 年 M 月 d 日 H 时 m 分 s 秒 
         * @param amount 数量 
         * @return 一个日期 
         */  
        public String getPreDate(String field,int amount){  
            calendar.setTime(new Date());  
            if(field!=null&&!field.equals("")){  
                if(field.equals("y")){  
                    calendar.add(calendar.YEAR, amount);  
                }else if(field.equals("M")){  
                    calendar.add(calendar.MONTH, amount);  
                }else if(field.equals("d")){  
                    calendar.add(calendar.DAY_OF_MONTH, amount);  
                }else if(field.equals("H")){  
                    calendar.add(calendar.HOUR, amount);  
                }  
            }else{  
                return null;  
            }         
            return getFormatDate(calendar.getTime());  
        }  
          
        /** 
         * 某一个日期的前一个日期 
         * @param d,某一个日期 
         * @param field 日历字段 
         * y 年 M 月 d 日 H 时 m 分 s 秒 
         * @param amount 数量 
         * @return 一个日期 
         */  
        public String getPreDate(Date date,String field,int amount){  
            calendar.setTime(date);  
            if(field!=null&&!field.equals("")){  
                if(field.equals("y")){  
                    calendar.add(calendar.YEAR, amount);  
                }else if(field.equals("M")){  
                    calendar.add(calendar.MONTH, amount);  
                }else if(field.equals("d")){  
                    calendar.add(calendar.DAY_OF_MONTH, amount);  
                }else if(field.equals("H")){  
                    calendar.add(calendar.HOUR, amount);  
                }  
            }else{  
                return null;  
            }         
            return getFormatDate(calendar.getTime());  
        }  
          
        /** 
         * 某一个时间的前一个时间 
         * @param date 
         * @return 
         * @throws ParseException  
         */  
        public String getPreDate(String date) throws ParseException{  
            Date d=new SimpleDateFormat().parse(date);  
            String preD=getPreDate(d,"d",1);  
            Date preDate=new SimpleDateFormat().parse(preD);  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
            return sdf.format(preDate);  
        }  
          
          
    }  
获取当天日期 获取本周一日期 获取本周日的日期 获取上周一日期:" + tt.getPreviousWeekday("yyyy-MM-dd")); 获取上周日日期:" + tt.getPreviousWeekSunday("yyyy-MM-dd")); 获取上周一日期:" + tt.getWeekday(-1, "yyyy-MM-dd")); 获取上周日日期:" + tt.getWeekSunday(-1, "yyyy-MM-dd")); 获取下周一日期:" + tt.getNextMonday("yyyy-MM-dd")); 获取下周日日期:" + tt.getNextSunday("yyyy-MM-dd")); 获取本月第一天日期:" + tt.getFirstDayOfMonth()); 获取本月最后一天日期:" + tt.getDefaultDay()); 获取上月第一天日期:" + tt.getPreviousMonthFirst("yyyy-MM-dd")); 获取上月最后一天的日期:" + tt.getPreviousMonthEnd("yyyy-MM-dd")); 获取某月第一天日期:" + tt.getMonthFirst(0, "yyyy-MM-dd")); 获取某月最后一天的日期:" + tt.getMonthEnd(0, "yyyy-MM-dd")); 获取下月第一天日期:" + tt.getNextMonthFirst("yyyy-MM-dd")); 获取下月最后一天日期:" + tt.getNextMonthEnd("yyyy-MM-dd")); 获取本年的第一天日期:" + tt.getCurrentYearFirst()); 获取本年最后一天日期:" + tt.getCurrentYearEnd()); 获取去年的第一天日期:" + tt.getPreviousYearFirst()); 获取去年的最后一天日期:" + tt.getPreviousYearEnd()); 获取明年第一天日期:" + tt.getNextYearFirst()); 获取明年最后一天日期:" + tt.getNextYearEnd()); 获取本季度第一天:" + tt.getThisSeasonFirstTime(11)); 获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11)); 获取两个日期之间间隔天数 获取当前月的第几周:" + tt.getWeekOfMonth()); 获取当前年份:" + tt.getYear()); 获取当前月份:" + tt.getMonth()); 获取今天在本年的第几天:" + tt.getDayOfYear()); 获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth()); 获得今天在本周的第几天:" + tt.getDayOfWeek()); 字符串转时间 获得一个日期所在周的星期几的日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值