Java工具类(一)

JAVA学习网 2018-01-07 20:52:01

最近总是接触时间处理问题,花了点时间整理以前使用过的方法,修改整合,记录下来方便以后使用。

package cc.vvxtoys.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 
 * @author vvxtoys
 * @date 2018年1月6日下午9:48:02
 *
 */
public class DateUtils {
    
    private static  Calendar cal = Calendar.getInstance();
    private static String fm = "";//format month
    private static String fd = "";//format day
    
    
    public static void main(String[] args) throws Exception{
        int year = 2018;
        int month = 2;
        System.out.println(isLeapYear(year));
        System.out.println(getCurrentDate());
        System.out.println(getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        System.out.println(addDays("2018-01-01", 10));
        System.out.println(addMonths("2018-01-31", 1));
        System.out.println(addWeeks("2018-01-03", 1));
        System.out.println(getMonthDays(year, month));
        System.out.println(addYears("2011-09-01", 1));
        System.out.println(getDayOfWeek("2018-01-01"));
        System.out.println(getStartDayOfMonth(year, 2));
        System.out.println(getEndDayOfMonth(year, 2));
        System.out.println(getStartDayOfThisWeek("2018-01-01"));
        System.out.println(getEndDayOfThisWeek("2018-01-01"));
        System.out.println(addQuarters("2018-01-01", 2));
        System.out.println(getWeekOfYear("2018-01-01"));        
        System.out.println(translateDate("2019-01-01", null));
        System.out.println(translateDate(new Date(), null));
        System.out.println(getEndDayOfMonth(year, 2));
        System.out.println(getSubDays(3*3));
        System.out.println(intervalDay("2018-10-10", "2019-01-01"));
        
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @return
     * @description 判断是否为闰年
     */
    public static boolean isLeapYear(int year){
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @param month
     * @return
     * @description 获取月天数
     */
    public static String getMonthDays(int year,int month){
        String [] days = {"31", null, "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"};
        if(isLeapYear(year)){
            days[1]="29";
        }else{
            days[1]="28";
        }
        return days[month-1];
    } 
    /**
     * 
     * @author vvxtoys
     * @param date
     * @return
     * @throws ParseException
     * @description 获取时间在当前年为第几周
     */
    public static int getWeekOfYear(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");      
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String firstDay = date.substring(0, 4) + "-01-01";
        cal.setTime(sdf.parse(firstDay));
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 2) {
            cal.setTime(sdf.parse(date));
            int num = cal.get(Calendar.WEEK_OF_YEAR);
            return num;
        } else {
            cal.setTime(sdf.parse(date));
            int num = cal.get(Calendar.WEEK_OF_YEAR) - 1;
            return num;
        }
    }
    /**
     * 
     * @author vvxtoys
     * @param date
     * @param num
     * @return
     * @description 天计算
     */
    public static String addDays(String date,int num){
        try{
        cal.setTime(toDate(date));
        cal.add(Calendar.DATE, num);
        toFormatDate();
        }catch(Exception e){
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }   
    /**
     * 
     * @author vvxtoys
     * @param date
     * @param num
     * @return
     * @description 星期
     */
    public static String addWeeks(String date,int num){
        try {
            cal.setTime(toDate(date));
            cal.add(Calendar.WEEK_OF_YEAR, num);
            toFormatDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    /**
     * 
     * @author vvxtoys
     * @param date
     * @param num
     * @return
     * @description 月
     */
    public static String addMonths(String date,int num){
        try {
            cal.setTime(toDate(date));
            cal.add(Calendar.MONTH, num);
            toFormatDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }   
    /**
     * 
     * @author vvxtoys
     * @param date
     * @param num
     * @return
     * @description 季度
     */
    public static String addQuarters(String date,int num){
        try {
            cal.setTime(toDate(date));
            cal.add(Calendar.MONTH, num*3);
            toFormatDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }   
    /**
     * 
     * @author vvxtoys
     * @param date
     * @param num
     * @return
     * @description 年
     */
    public static String addYears(String date,int num){
        try {
            cal.setTime(toDate(date));
            cal.add(Calendar.YEAR, num);
            toFormatDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    /**
     * 
     * @author vvxtoys
     * @param time
     * @param pattern
     * @return
     * @description 日期转换
     */
    public static Object translateDate(Object time, String pattern) {
        Object date = new Object();
        if (isEmpty(pattern)) {
            pattern = "yyyy-MM-dd";
        }
        if (isEmpty(time)) {
            time = "1970-1-1";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            if (time instanceof String) {
                date = sdf.parse((String) time);
            } else if (time instanceof Date) {
                date = sdf.format(time);
            } else {
                throw new Exception("转换失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 
     * @author vvxtoys
     * @param date
     * @return
     * @description 当前日期为本周第几天
     */
    public static int getDayOfWeek(String date){
        try{
        cal.setTime(toDate(date));
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        }catch(ParseException e){
            e.printStackTrace();
        }
        return cal.get(Calendar.DAY_OF_WEEK)==1?7:cal.get(Calendar.DAY_OF_WEEK)-1;
    }
    /**
     * 
     * @author vvxtoys
     * @param date
     * @return
     * @description 周一
     */
    public static String getStartDayOfThisWeek(String date){
      return addDays(date, -(getDayOfWeek(date)-1));
    }   
    /**
     * 
     * @author vvxtoys
     * @param date
     * @return
     * @description 周日
     */
    public static String getEndDayOfThisWeek(String date){
        return addDays(date,(7-getDayOfWeek(date)));
    }
    //获取当前时间
    public static String getCurrentDate(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    //当前时间
    public static String getCurrentDate(String pattern){
        Date date = new Date();
        if(isEmpty(pattern)){
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }   
    /**
     * 
     * @author vvxtoys
     * @param num
     * @return
     * @description 时间间隔  月 季度 年  *3 *12
     */
    public static int getSubDays(int num){
        Date date = new Date();
        cal.setTime(date);
        cal.add(Calendar.MONTH, num);
        long time = cal.getTimeInMillis() - date.getTime();
        return (int) (time / (1000 * 60 * 60 * 24));
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @param num
     * @return
     * @description 获取第几周开始时间
     */
    public static String getStartDayOfWeek(int year,int num){
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, num);
        toFormatDate();
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @param num
     * @return
     * @description 获取第几周结束时间
     */
    public static String getEndDayOfWeek(int year,int num){
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, num);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        toFormatDate();
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @param num
     * @return
     * @description 获取第几月开始时间
     */
    public static String getStartDayOfMonth(int year,int num){
        String month = num < 10 ? "0" + num : String.valueOf(num);
        return year + "-" + month + "-" + "01";
    }
    /**
     * 
     * @author vvxtoys
     * @param year
     * @param num
     * @return
     * @description 获取第几月结束时间
     */
    public static String getEndDayOfMonth(int year,int num){
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, num - 1);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        toFormatDate();
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    //获取第几季度开始时间
    public static String getStartDayOfQuarter(int year,int num){
        num = 3 * num - 2;
        String month = num < 10 ? "0" + num : String.valueOf(num);
        return year + "-" + month + "-" + "01";
    }
    //获取第几季度结束时间
    public static String getEndDayOfQuarter(int year,int num){
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, 3 * num - 1);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        toFormatDate();
        return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd;
    }
    //时间间隔
    public static int intervalDay(String d1,String d2){
        int num = 0;
        try{
        long second = toDate(d2).getTime()-toDate(d1).getTime();
        num=(int)(Math.abs(second)/(1000 * 60 * 60 * 24));
        }catch(ParseException e){
            e.printStackTrace();
        }
        return num;
    }
    private static void toFormatDate(){
        int month = (cal.get(Calendar.MONTH) + 1);
        fm = month<10?"0"+month:String.valueOf(month);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        fd = day<10?"0"+day:String.valueOf(day);    
    }   
    private static Date toDate(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(date);
    }
    private static boolean isEmpty(Object obj){
        return obj==null||obj.toString().trim().equals("");
    }
}
阅读(755) 评论(0)