java 日期操作輔助類

來源:互聯網
上載者:User

FormatDate.java

 

package com.ucit.ca.webApp.tool;import java.util.*;import java.text.*;public class FormatDate {public FormatDate() {}// 格式化日期為字串 "yyyy-MM-dd   hh:mm"public String formatDateTime(Date basicDate, String strFormat) {SimpleDateFormat df = new SimpleDateFormat(strFormat);return df.format(basicDate);}// 格式化日期為字串 "yyyy-MM-dd   hh:mm"public String formatDateTime(String basicDate, String strFormat) {SimpleDateFormat df = new SimpleDateFormat(strFormat);Date tmpDate = null;try {tmpDate = df.parse(basicDate);} catch (Exception e) {// 日期型字串格式錯誤}return df.format(tmpDate);}// 當前日期加減n天后的日期,返回String (yyyy-mm-dd)public String nDaysAftertoday(int n) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");Calendar rightNow = Calendar.getInstance();// rightNow.add(Calendar.DAY_OF_MONTH,-1);rightNow.add(Calendar.DAY_OF_MONTH, +n);return df.format(rightNow.getTime());}// 當前日期加減n天后的日期,返回String (yyyy-mm-dd)public Date nDaysAfterNowDate(int n) {Calendar rightNow = Calendar.getInstance();// rightNow.add(Calendar.DAY_OF_MONTH,-1);rightNow.add(Calendar.DAY_OF_MONTH, +n);return rightNow.getTime();}// 給定一個日期型字串,返回加減n天后的日期型字串public String nDaysAfterOneDateString(String basicDate, int n) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");Date tmpDate = null;try {tmpDate = df.parse(basicDate);} catch (Exception e) {// 日期型字串格式錯誤}long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)* (24 * 60 * 60 * 1000);tmpDate.setTime(nDay);return df.format(tmpDate);}// 給定一個日期,返回加減n天后的日期public Date nDaysAfterOneDate(Date basicDate, int n) {long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)* (24 * 60 * 60 * 1000);basicDate.setTime(nDay);return basicDate;}// 計算兩個日期相隔的天數public int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));return nDay;}// 計算兩個日期相隔的天數public int nDaysBetweenTwoDate(String firstString, String secondString) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");Date firstDate = null;Date secondDate = null;try {firstDate = df.parse(firstString);secondDate = df.parse(secondString);} catch (Exception e) {// 日期型字串格式錯誤}int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));return nDay;}}

 

源碼:

http://cid-6adab3222e64b22c.skydrive.live.com/self.aspx/.Public/%e4%bb%a3%e7%a0%81/javaToolClass/FormatDate.java

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.