Java 日期工具類

jopen 10年前發布 | 30K 次閱讀 Java Java開發

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

/**

  • 時間處理工具類 <br>
  • @author Davint
  • @since 5.0
  • @version 1.0 */ public class TimerUtil {

    private SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd"); private static TimerUtil cc = new TimerUtil();

    public static TimerUtil getInstance() {

     return cc;
    

    }

    public static void main(String[] args) {

     TimerUtil timer=TimerUtil.getInstance();
     System.out.println(timer.getDaysByDate(timer.getDate("2012-04-01", "yyyy-MM-dd")));
    
    

    }

    //根據日期得到當月天數 public int getDaysByDate(Date date) {

     Calendar ca=Calendar.getInstance();
     ca.setTime(date);
     int year=ca.get(Calendar.YEAR);
     int month=ca.get(Calendar.MONDAY)+1;
     int day=0;
     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
         if (month==2) {
             day=29;
         }
     }else {
         if (month==2) {
             day=28;
         } 
     }
    
     if (month==4||month==6||month==9||month==11) {
         day=30;
     }
    
     if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
         day=31;
     }
     return day;
    

    }

/**
 * 上周一的日期
 * 將周一作為一周的第一天
 */
public Date getLastWeekFirstDay() {
    Calendar ca = Calendar.getInstance();
    if (ca.get(Calendar.DAY_OF_WEEK)-1==0) {
        ca.add(Calendar.DATE, -1 * 7*2);
        ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return ca.getTime();
    }
    ca.add(Calendar.DATE, -1 * 7);
    ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return ca.getTime();
}

// 本周一日期
public Date getThisWeekFirstDay() {
    Calendar ca=Calendar.getInstance();
    if (ca.get(Calendar.DAY_OF_WEEK)-1==0) {
        ca.add(Calendar.DATE, -1);
        ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return ca.getTime();
    }
    ca.add(Calendar.DATE, 0);
    ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return ca.getTime();
}

//兩個月前的第一天
public Date getLast2MonFirstDay() {
    Calendar cal=Calendar.getInstance();
    Date now=new Date();
    cal.setTime(now);
    cal.add(Calendar.MONTH, -1);
    int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    String resultTime=year+"-"+month+"-01";
    Date date =null;
    try {
        date=sp.parse(resultTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

// 上個月第一天日期
public Date getLastMonFirstDay() {
    Calendar cal=Calendar.getInstance();
    Date now=new Date();
    cal.setTime(now);
    int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    String resultTime=year+"-"+month+"-01";
    Date date =null;
    try {
        date=sp.parse(resultTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

// 本月第一天
public Date getThisMonFirstDay() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 2;
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.add(Calendar.DAY_OF_MONTH, -1);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    String months = "";
    String days = "";
    if (month > 1) {
        month--;
    } else {
        year--;
        month = 12;
    }
    if (!(String.valueOf(month).length() > 1)) {
        months = "0" + month;
    } else {
        months = String.valueOf(month);
    }
    if (!(String.valueOf(day).length() > 1)) {
        days = "0" + day;
    } else {
        days = String.valueOf(day);
    }
    String firstDay = "" + year + "-" + months + "-01";
    String[] lastMonth = new String[2];
    lastMonth[0] = firstDay;
    try {
        return sp.parse(firstDay);
    } catch (ParseException e) {
    }
    return null;
}

// 昨天日期
public String getYesterday() {
    Date today = new Date();
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, -1);
    return sp.format(ca.getTime());
}

// 7天前日期
public String getSevenDayAgo() {
    Date today = new Date();
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, -7);
    return sp.format(ca.getTime());
}

// 3天前日期
public String getThreeDayAgo() {
    Date today = new Date();
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, -3);
    return sp.format(ca.getTime());
}

// 一月前日期
public String getOneMonthAgo() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, -1); // 得到前一個月
    long date = cal.getTimeInMillis();
    Date kk = new Date(date);
    return sp.format(kk);
}

// 3月前日期
public String getThreeMonthAgo() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, -3);
    long date = cal.getTimeInMillis();
    Date kk = new Date(date);
    return sp.format(kk);
}

/**
 * getDate<br>
 * 格式:yyyy-MM-dd 00:00:00 eg.2013-08-19 00:00:00<br>
 * @param type -1表示昨天;-2表示前天;以此類推-N表示前N天
 * @return java.util.Date
 */
public Date getDate(int type) {
    Date resultDate = null;
    Date today = new Date();
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, type);
    // 將時間轉換成00:00:00
    try {
        resultDate = sp.parse(sp.format(ca.getTime()));
        return resultDate;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return resultDate;
}

/**
 * N天前的具體時間
 * 
 * @param n  0 表示今天,-1代表昨天
 * @param type yyyyMMddHHmmss、yyyy-MM-dd HH:mm:ss
 * @return
 */
public String getSomeDaysAgoString(int n, String type) {
    Date today = new Date();
    SimpleDateFormat sp = new SimpleDateFormat();
    sp.applyPattern(type);
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, n);
    return sp.format(ca.getTime());
}

/**
 * N天前的具體時間
 * 
 * @param n 0 表示今天,-1代表昨天
 * @return
 */
public Date getSomeDaysAgoDate(int n) {
    Date today = new Date();
    Calendar ca = Calendar.getInstance();
    ca.setTime(today);
    ca.add(Calendar.DAY_OF_YEAR, n);
    return ca.getTime();
}

/**
 * 格式化某一時間
 * 
 * @param date
 * @param type
 * @return
 */
public String getString(Date date, String type) {
    SimpleDateFormat sp = new SimpleDateFormat();
    sp.applyPattern(type);
    if (date != null && type != null) {
        return sp.format(date);
    }
    return "時間格式化異常!";
}

/**
 * 格式化某一時間字符串
 * 
 * @param date
 * @param type
 * @return
 */
public Date getDate(String date, String type) {
    SimpleDateFormat sp = new SimpleDateFormat();
    sp.applyPattern(type);
    try {
        if (date != null && type != null) {
            return sp.parse(date);
        }
        return null;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * 某日期前、后N天的日期
 * @param date 某一日期
 * @param type N天 0表示當天,N或+N表示后N天的日期,-N表示前N天的日期
 * @return java.util.Date
 */
public Date getDate(Date date,int type) {
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_YEAR, type);
    return cal.getTime();
}

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!