時間工具類[DateUtil]

ExpandedBlockStart.gifView Code
??1?package?com.ly.util;
??2?
??3?import?java.text.DateFormat;
??4?import?java.text.ParseException;
??5?import?java.text.SimpleDateFormat;
??6?import?java.util.Calendar;
??7?import?java.util.Date;
??8?
??9?/**
?10??*?
?11??*?功能描述
?12??*?
?13??*?@author?Administrator
?14??*?@Date?Jul?19,?2008
?15??*?@Time?9:47:53?AM
?16??*?@version?1.0
?17??*/
?18?public?class?DateUtil?{
?19?
?20?????public?static?Date?date?=?null;
?21?
?22?????public?static?DateFormat?dateFormat?=?null;
?23?
?24?????public?static?Calendar?calendar?=?null;
?25?????
?26?????
?27?????/**
?28??????*?英文簡寫(默認)如:2010-12-01
?29??????*/
?30?????public?static?String?FORMAT_SHORT?=?"yyyy-MM-dd";
?31?????
?32?????/**
?33??????*?英文全稱??如:2010-12-01?23:15:06
?34??????*/
?35?????public?static?String?FORMAT_LONG?=?"yyyy-MM-dd?HH:mm:ss";
?36?????
?37?????/**
?38??????*?精確到毫秒的完整時間????如:yyyy-MM-dd?HH:mm:ss.S
?39??????*/
?40?????public?static?String?FORMAT_FULL?=?"yyyy-MM-dd?HH:mm:ss.S";
?41?????
?42?????/**
?43??????*?中文簡寫??如:2010年12月01日
?44??????*/
?45?????public?static?String?FORMAT_SHORT_CN?=?"yyyy年MM月dd";
?46?????
?47?????/**
?48??????*?中文全稱??如:2010年12月01日??23時15分06秒
?49??????*/
?50?????public?static?String?FORMAT_LONG_CN?=?"yyyy年MM月dd日??HH時mm分ss秒";
?51?????
?52?????/**
?53??????*?精確到毫秒的完整中文時間
?54??????*/
?55?????public?static?String?FORMAT_FULL_CN?=?"yyyy年MM月dd日??HH時mm分ss秒SSS毫秒";
?56?
?57?????/**
?58??????*?獲得默認的?date?pattern
?59??????*/
?60?????public?static?String?getDatePattern()?{
?61?????????return?FORMAT_LONG;
?62?????}
?63?
?64?????/**
?65??????*?根據預設格式返回當前日期
?66??????*?@return
?67??????*/
?68?????public?static?String?getNow()?{
?69?????????return?format(new?Date());
?70?????}
?71?????
?72?????/**
?73??????*?根據用戶格式返回當前日期
?74??????*?@param?format
?75??????*?@return
?76??????*/
?77?????public?static?String?getNow(String?format)?{
?78?????????return?format(new?Date(),?format);
?79?????}
?80?
?81?????
?82?????/**
?83??????*?使用預設格式格式化日期
?84??????*?@param?date
?85??????*?@return
?86??????*/
?87?????public?static?String?format(Date?date)?{
?88?????????return?format(date,?getDatePattern());
?89?????}
?90?
?91?????/**
?92??????*?使用用戶格式格式化日期
?93??????*?@param?date?日期
?94??????*?@param?pattern?日期格式
?95??????*?@return
?96??????*/
?97?????public?static?String?format(Date?date,?String?pattern)?{
?98?????????String?returnValue?=?"";
?99?????????if?(date?!=?null)?{
100?????????????SimpleDateFormat?df?=?new?SimpleDateFormat(pattern);
101?????????????returnValue?=?df.format(date);
102?????????}
103?????????return?(returnValue);
104?????}
105?
106?????/**
107??????*?使用預設格式提取字符串日期
108??????*?@param?strDate?日期字符串
109??????*?@return
110??????*/
111?????public?static?Date?parse(String?strDate)?{
112?????????return?parse(strDate,?getDatePattern());
113?????}
114?
115?????/**
116??????*?使用用戶格式提取字符串日期
117??????*?@param?strDate?日期字符串
118??????*?@param?pattern?日期格式
119??????*?@return
120??????*/
121?????public?static?Date?parse(String?strDate,?String?pattern)?{
122?????????SimpleDateFormat?df?=?new?SimpleDateFormat(pattern);
123?????????try?{
124?????????????return?df.parse(strDate);
125?????????}?catch?(ParseException?e)?{
126?????????????e.printStackTrace();
127?????????????return?null;
128?????????}
129?????}
130?
131?????/**
132??????*?在日期上增加數個整月
133??????*?@param?date?日期
134??????*?@param?n?要增加的月數
135??????*?@return
136??????*/
137?????public?static?Date?addMonth(Date?date,?int?n)?{
138?????????Calendar?cal?=?Calendar.getInstance();
139?????????cal.setTime(date);
140?????????cal.add(Calendar.MONTH,?n);
141?????????return?cal.getTime();
142?????}
143?
144?????/**
145??????*?在日期上增加天數
146??????*?@param?date?日期
147??????*?@param?n?要增加的天數
148??????*?@return
149??????*/
150?????public?static?Date?addDay(Date?date,?int?n)?{
151?????????Calendar?cal?=?Calendar.getInstance();
152?????????cal.setTime(date);
153?????????cal.add(Calendar.DATE,?n);
154?????????return?cal.getTime();
155?????}
156?????
157?????/**
158??????*?獲取距現在某一小時的時刻
159??????*?@param?format?格式化時間的格式
160??????*?@param?h????距現在的小時?例如:h=-1為上一個小時,h=1為下一個小時
161??????*?@return
162??????*/
163?????public?static?String?getpreHour(String?format,?int?h){
164?????????SimpleDateFormat?sdf?=?new?SimpleDateFormat(format);
165?????????Date?date?=?new?Date();
166?????????date.setTime(date.getTime()+h*60*60*1000);
167?????????return?sdf.format(date);
168?????}
169?????/**
170??????*?獲取時間戳
171??????*/
172?????public?static?String?getTimeString()?{
173?????????SimpleDateFormat?df?=?new?SimpleDateFormat(FORMAT_FULL);
174?????????Calendar?calendar?=?Calendar.getInstance();
175?????????return?df.format(calendar.getTime());
176?????}
177?
178?????/**
179??????*?獲取日期年份
180??????*?@param?date?日期
181??????*?@return
182??????*/
183?????public?static?String?getYear(Date?date)?{
184?????????return?format(date).substring(0,?4);
185?????}
186?????/**
187??????*?功能描述:返回月
188??????*?
189??????*?@param?date
190??????*????????????Date?日期
191??????*?@return?返回月份
192??????*/
193?????public?static?int?getMonth(Date?date)?{
194?????????calendar?=?Calendar.getInstance();
195?????????calendar.setTime(date);
196?????????return?calendar.get(Calendar.MONTH)?+?1;
197?????}
198?
199?????/**
200??????*?功能描述:返回日
201??????*?
202??????*?@param?date
203??????*????????????Date?日期
204??????*?@return?返回日份
205??????*/
206?????public?static?int?getDay(Date?date)?{
207?????????calendar?=?Calendar.getInstance();
208?????????calendar.setTime(date);
209?????????return?calendar.get(Calendar.DAY_OF_MONTH);
210?????}
211?
212?????/**
213??????*?功能描述:返回小
214??????*?
215??????*?@param?date
216??????*????????????日期
217??????*?@return?返回小時
218??????*/
219?????public?static?int?getHour(Date?date)?{
220?????????calendar?=?Calendar.getInstance();
221?????????calendar.setTime(date);
222?????????return?calendar.get(Calendar.HOUR_OF_DAY);
223?????}
224?
225?????/**
226??????*?功能描述:返回分
227??????*?
228??????*?@param?date
229??????*????????????日期
230??????*?@return?返回分鐘
231??????*/
232?????public?static?int?getMinute(Date?date)?{
233?????????calendar?=?Calendar.getInstance();
234?????????calendar.setTime(date);
235?????????return?calendar.get(Calendar.MINUTE);
236?????}
237?
238?????/**
239??????*?返回秒鐘
240??????*?
241??????*?@param?date
242??????*????????????Date?日期
243??????*?@return?返回秒鐘
244??????*/
245?????public?static?int?getSecond(Date?date)?{
246?????????calendar?=?Calendar.getInstance();
247?????????calendar.setTime(date);
248?????????return?calendar.get(Calendar.SECOND);
249?????}
250?
251?????/**
252??????*?功能描述:返回毫
253??????*?
254??????*?@param?date
255??????*????????????日期
256??????*?@return?返回毫
257??????*/
258?????public?static?long?getMillis(Date?date)?{
259?????????calendar?=?Calendar.getInstance();
260?????????calendar.setTime(date);
261?????????return?calendar.getTimeInMillis();
262?????}
263?????/**
264??????*?按默認格式的字符串距離今天的天數
265??????*?@param?date?日期字符串
266??????*?@return
267??????*/
268?????public?static?int?countDays?(String?date)?{
269?????????long?t?=?Calendar.getInstance().getTime().getTime();
270?????????Calendar?c?=?Calendar.getInstance();
271?????????c.setTime(parse(date));
272?????????long?t1?=?c.getTime().getTime();
273?????????return?(int)(t/1000?-?t1/1000)/3600/24;
274?????}
275?????
276?????/**
277??????*?按用戶格式字符串距離今天的天數
278??????*?@param?date?日期字符串
279??????*?@param?format?日期格式
280??????*?@return
281??????*/
282?????public?static?int?countDays?(String?date,?String?format)?{
283?????????long?t?=?Calendar.getInstance().getTime().getTime();
284?????????Calendar?c?=?Calendar.getInstance();
285?????????c.setTime(parse(date,?format));
286?????????long?t1?=?c.getTime().getTime();
287?????????return?(int)(t/1000?-?t1/1000)/3600/24;
288?????}
289?
290?}

轉載于:https://www.cnblogs.com/Jeanferly/archive/2012/03/27/2419078.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377618.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377618.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377618.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JQuery delegate多次綁定的解決辦法

我用delegate來控制分頁,查詢的時候會造成多次綁定 //前一頁、后一頁觸發 1 $("body").delegate("#tableFoot a:not(a.btn)", "click", function () { 2 _options.page $(this).attr("page"); 3 loadTmpl(_option…

leetcode 45. 跳躍游戲 II 思考分析

題目 給定一個非負整數數組,你最初位于數組的第一個位置。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 你的目標是使用最少的跳躍次數到達數組的最后一個位置。 示例: 輸入: [2,3,1,1,4] 輸出: 2 解釋: 跳到最后一個位置的最小跳躍數是 2。 從下標為 …

C程序實現冒泡排序

Bubble Sort is a simple, stable, and in-place sorting algorithm. 氣泡排序是一種簡單,穩定且就地的排序算法。 A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is pre…

一、爬蟲基本概念

一、爬蟲根據使用場景分類 爬蟲: 通過編寫程序,模擬瀏覽器上網,讓其去互聯網上抓取數據的過程。 ① 通用爬蟲:抓取系統重要的組成部分,抓取的是一整張頁面的數據 ② 聚焦爬蟲:建立在通用爬蟲的基礎之上&am…

經營你的iOS應用日志(二):異常日志

如果你去4S店修車,給小工說你的車哪天怎么樣怎么樣了,小工有可能會立即搬出一臺電腦,插上行車電腦把日志打出來,然后告訴你你的車發生過什么故障。汽車尚且如此,何況移動互聯網應用呢。 本文第一篇:經營你的…

Discuz 升級X3問題匯總整理

最近一段時間公司的社區垃圾帖數量陡然上漲,以至于社區首頁的推薦版塊滿滿都是垃圾帖的身影,為了進一步解決垃圾帖問題我們整整花了1天時間刪垃圾貼,清除不良用戶,刪的手都酸了,可見垃圾帖的數量之多!可恥的…

【C++grammar】格式化輸出與I/O流函數

目錄1、格式化輸出1. setw manipulator(“設置域寬”控制符)2. setprecision manipulator(“設置浮點精度”控制符)3. setfill manipulator(“設置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化輸入/輸出)5.小練習2、用于輸入/輸出流的函數1. g…

python 忽略 異常_如何忽略Python中的異常?

python 忽略 異常什么是例外? (What is an Exception?) An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation t…

三、實戰---爬取百度指定詞條所對應的結果頁面(一個簡單的頁面采集器)

在第一篇博文中也提及到User-Agent,表示請求載體的身份,也就是說明通過什么瀏覽器進行訪問服務器的,這一點很重要。 ① UA檢測 門戶網站服務器會檢測請求載體的身份。如果檢測到載體的身份表示為某一款瀏覽器的請求,則說明這是一…

Spring MVC攔截器實現分析

SpringMVC的攔截器不同于Spring的攔截器,SpringMVC具有統一的入口DispatcherServlet,所有的請求都通過DispatcherServlet,所以只需要在DispatcherServlet上做文章即可,DispatcherServlet也沒有代理,同時SpringMVC管理的…

碩士畢業后去國外讀法學博士_法學碩士的完整形式是什么?

碩士畢業后去國外讀法學博士法學碩士:豆科大法師(拉丁)/法學碩士 (LLM: Legum Magister (Latin)/ Master of Law) LLM is an abbreviation of Legum Magister. It is in term of Latin which states the masters degree of Law. In the majority, LLM is generally …

android:layout_weight屬性的簡單使用

效果&#xff1a; style.xml <style name"etStyle2"><item name"android:layout_width">match_parent</item><item name"android:layout_height">wrap_content</item><item name"android:background"…

一、環境配置安裝

一、Anaconda Ⅰ下載 最新版的anaconda可能會需要各種各樣的問題&#xff0c;python3.6版本比較穩定&#xff0c;建議使用。 老鐵們可以通過&#xff0c;Anaconda以前版本所自帶Python版本&#xff0c;查看Anaconda所帶的python版本 我用的是這個&#xff0c;Anaconda3-5.2.0…

leetcode 35. 搜索插入位置 思考分析

目錄題目暴力二分迭代二分遞歸題目 給定一個排序數組和一個目標值&#xff0c;在數組中找到目標值&#xff0c;并返回其索引。如果目標值不存在于數組中&#xff0c;返回它將會被按順序插入的位置。 你可以假設數組中無重復元素。 示例 1: 輸入: [1,3,5,6], 5 輸出: 2 示例 2:…

java優秀算法河內之塔_河內塔的Java程序

java優秀算法河內之塔Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are: 河內塔是一個數學難題&a…

轉——C# DataGridView控件 動態添加新行

DataGridView控件在實際應用中非常實用&#xff0c;特別需要表格顯示數據時。可以靜態綁定數據源&#xff0c;這樣就自動為DataGridView控件添加相應的行。假如需要動態為DataGridView控件添加新行&#xff0c;方法有很多種&#xff0c;下面簡單介紹如何為DataGridView控件動態…

分享通用基類庫-C#通用緩存類

1 /************************************************************************************* 2 * 代碼:吳蔣 3 * 時間:2012.03.30 4 * 說明:緩存公共基類 5 * 其他: 6 * 修改人&#xff1a; 7 * 修改時間&#xff1a; 8 * 修改說明&#xff1a; 9 ******************…

二、PyTorch加載數據

一、常用的兩個函數 dir()函數可以理解為打開某個包&#xff0c;help()可以理解為返回如何使用某個具體的方法 例如&#xff1a;若一個A錢包里面有a&#xff0c;b&#xff0c;c&#xff0c;d四個小包&#xff0c;則可通過dir(A)&#xff0c;打開該A錢包&#xff0c;返回a&…

leetcode 1005. K 次取反后最大化的數組和 思考分析

題目 給定一個整數數組 A&#xff0c;我們只能用以下方法修改該數組&#xff1a;我們選擇某個索引 i 并將 A[i] 替換為 -A[i]&#xff0c;然后總共重復這個過程 K 次。&#xff08;我們可以多次選擇同一個索引 i。&#xff09; 以這種方式修改數組后&#xff0c;返回數組可能…

三、TensorBoard

一、安裝TensorBoard 管理員身份運行Anaconda Prompt&#xff0c;進入自己的環境環境 conda activate y_pytorch&#xff0c;pip install tensorboard 進行下載&#xff0c;也可以通過conda install tensorboard進行下載。其實通俗點&#xff0c;pip相當于菜市場&#xff0c;c…