時間格式處理,工具類。

2021-09-29 16:35:50 字數 1579 閱讀 5925

1、記錄乙個時間的工具類,用以獲取某一天的日期

注釋在裡面寫的很清楚,我就不多說了。

說兩個方法:

******dateformat 這個方法用於將時間的字串格式,轉化成date格式。也可以將date格式轉化成字串格式
其實在這個方法中真正操作時間的是calendar

public static string getenergyolddate(string currentdate, int distanceday)  catch (exception e) 

/*這裡需要返回的是15天前的日期*/

/*設定時間的格式*/

/*獲取乙個date的物件*/

/*獲取乙個當前時間

* 設定的是當前的為時間*/

calendar cal = calendar.getinstance();

/** 將日期設定到輸入的日期

* */

cal.settime(startdate);

/**將當前設定的日期以日期的格式增加或減少輸入的天數

* 這是的tmpdate是當前的日期的「天」

* */

int tmpdate = cal.get(calendar.date);

/* 以這種方式 獲取到輸入的日期的年月日 */

// int year = cal.get(calendar.year);

// int month = cal.get(calendar.month) + 1;

int day = cal.get(calendar.date);

/**將輸入內容的年 設定為:year + distanceday

* */

// cal.set(calendar.year, year + distanceday);

/** 將輸入內容的月 設定為:month + distanceday

* */

// cal.set(calendar.month, month + distanceday);

// /*

// * 將輸入內容的日 設定為:day + distanceday

// * */

cal.set(calendar.date, day + distanceday);

/** 總結:這一塊指的是將輸入日期中 年 月 日的某一項設定成 後面的值

* 最後生成乙個calendar型別的日期,

* */

date enddate = null;

string s = "";

/** 最後將結果

* */

// enddate = ******dateformat.parse(******dateformat.format(cal.gettime()));

date date = cal.gettime();

s = ******dateformat.format(date);

return s;

}

時間格式化處理工具類

時間格式化 引數 time 時間 type 0 今天10 20 昨天10 20 2019.01.21 10 29 1 10 20 昨天10 20 2019.01.21 10 29 2 2019.01.21 10 29 3 2019.01.21 4 10 29 5 今天10 20 2019.01.21...

java時間處理工具類

public class timehelper 預設當前時間 精確到秒 return 當前時間 public static string getnow 通過指定的格式獲取當前時間字串 param format 日期字串格式 return 當前時間字串 public static string get...

日期處理工具類

public class dateutils 日期格式化 日期格式為 yyyy mm dd param date 日期 param pattern 格式,如 dateutils.date time pattern return 返回yyyy mm dd格式日期 public static strin...