本文實(shí)例為大家分享了Calendar時(shí)間操作常用方法,具體內(nèi)容如下
package test;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * Date和Calendar常用方法,Date很多方法已經(jīng)棄用,因此以Calendar為主 * * @author tuzongxun123 * */public class DateAndCalendarTest {public static void main(String[] args) {// 直接用Date獲取當(dāng)前系統(tǒng)時(shí)間,結(jié)果:Tue May 03 08:25:44 CST 2016Date date = new Date();// Date中的許多方法,例如獲取某年、某月、某日等,以及設(shè)置某年、某月、某日等都不再建議使用,// 建議使用calendar的各種方法替代,因此便不做記錄// 獲取指定日期的毫秒數(shù),常用來(lái)比較兩個(gè)日期的大小。date.getTime();// 使用Calendar獲取當(dāng)前系統(tǒng)時(shí)間,需要獲取Calendar對(duì)象后轉(zhuǎn)換成Date輸出Calendar calendar = Calendar.getInstance();// 這個(gè)方法相當(dāng)于Date中的getTime,獲取當(dāng)前時(shí)間的毫秒數(shù)calendar.getTimeInMillis();// 獲取指定日期所在周的第一天的日期,默認(rèn)的一周的第一天是周日calendar.getFirstDayOfWeek();// 返回當(dāng)前calendar日期所在的年,如2016calendar.get(1);// Calendar轉(zhuǎn)Date,輸出結(jié)果:Tue May 03 09:31:59 CST 2016Date date2 = calendar.getTime();System.out.println(date2);// Calendar設(shè)置年、月、日,輸出結(jié)果:Mon Jun 03 09:31:59 CST 2013// 相關(guān)常用重載方法:calendar.set(year, month, date, hourOfDay, minute);// calendar.set(year, month, date, hourOfDay, minute, second);參數(shù)均為intcalendar.set(2013, 5, 3);System.out.println(calendar.getTime());// 使用Calendar設(shè)置年,輸出結(jié)果:Fri Jun 03 09:42:43 CST 2011calendar.set(Calendar.YEAR, 2011);System.out.println(calendar.getTime());// 使用Calendar和數(shù)字設(shè)置月,注意月份從0開(kāi)始,代表1月,輸出結(jié)果:Mon Jan 03 09:45:32 CST 2011calendar.set(Calendar.MONTH, 0);System.out.println(calendar.getTime());// 使用Calendar和自帶常量設(shè)置月,注意月份從0開(kāi)始,代表1月,輸出結(jié)果:Thu Feb 03 09:47:07 CST 2011calendar.set(Calendar.MONTH, Calendar.FEBRUARY);System.out.println(calendar.getTime());// 使用Calendar和數(shù)字設(shè)置日,輸出結(jié)果:Sat Feb 05 09:48:25 CST 2011// calendar.set(Calendar.DAY_OF_MONTH, 5)結(jié)果一樣;calendar.set(Calendar.DATE, 5);System.out.println(calendar.getTime());// 設(shè)置小時(shí)calendar.set(Calendar.HOUR, 15);System.out.println(calendar.getTime());// 根據(jù)毫秒數(shù)設(shè)置Calendar時(shí)間calendar.setTimeInMillis(0);// Date轉(zhuǎn)String,輸出結(jié)果:2016-05-03 09:25:29String forDate = dateToString(new Date());System.out.println(forDate);// String轉(zhuǎn)Date,輸出結(jié)果:Thu Nov 12 13:23:11 CST 2015Date strDate = stringToDate("2015-11-12 13:23:11");System.out.println(strDate);// Date轉(zhuǎn)Calendar,輸出結(jié)果:2015Calendar calendar2 = dateToCalendar(strDate);System.out.println(calendar2.get(1));}/*** 把指定的date類(lèi)型時(shí)間轉(zhuǎn)換為指定格式的字符串* * @author:tuzongxun* @Title: dateToString* @param @param date* @return void* @date May 3, 2016 9:09:25 AM* @throws*/static String dateToString(Date date) {String str = "yyyy-MM-dd hh:mm:ss";SimpleDateFormat format = new SimpleDateFormat(str);String dateFormat = format.format(date);return dateFormat;}/*** 把指定的日期格式的字符串轉(zhuǎn)換成Date類(lèi)型* * @author:tuzongxun* @Title: StringToDate* @param @param string* @return void* @date May 3, 2016 9:16:38 AM* @throws*/static Date stringToDate(String string) {String str = "yyyy-MM-dd hh:mm:ss";SimpleDateFormat format = new SimpleDateFormat(str);Date date = new Date();try {date = format.parse(string);} catch (Exception e) {e.getStackTrace();}return date;}/*** 把指定的date類(lèi)型日期轉(zhuǎn)換成Calendar類(lèi)型* * @author:tuzongxun* @Title: dateToCalendar* @param @param date* @return void* @date May 3, 2016 9:13:49 AM* @throws*/static Calendar dateToCalendar(Date date) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar;}}又有哪些坑吶?
坑在哪里:
在我之前接觸的一個(gè)項(xiàng)目中涉及到這么一項(xiàng)功能:每天00:00:00把某些數(shù)據(jù)移動(dòng)到mongodb數(shù)據(jù)庫(kù)的另一個(gè)集合中,也就是關(guān)系型數(shù)據(jù)庫(kù)的表中。這個(gè)集合名是一個(gè)固定的名稱(chēng)加上當(dāng)前的兩個(gè)月前的日期所在的年和月份。
這個(gè)功能是在我接觸這個(gè)項(xiàng)目之前就已經(jīng)存在,也就是之前的同事實(shí)現(xiàn)的功能,寫(xiě)了一個(gè)java的定時(shí)任務(wù)。
那個(gè)同事如今已經(jīng)不在我們公司了,但是最近卻發(fā)現(xiàn)這個(gè)功能有些問(wèn)題,數(shù)據(jù)的移動(dòng)并不像預(yù)計(jì)那樣,原本應(yīng)該存在2月的數(shù)據(jù)卻出現(xiàn)在了1月的表中。
坑的根源:
mongodb相關(guān)的問(wèn)題暫時(shí)歸我維護(hù),這個(gè)問(wèn)題也自然而然需要我來(lái)解決,于是便把他的代碼翻出來(lái)看了一遍。結(jié)果便發(fā)現(xiàn)了問(wèn)題出在了calendar的相關(guān)方法上。
要轉(zhuǎn)移兩個(gè)月前的數(shù)據(jù),首先要獲取兩個(gè)月前的日期,他的生成表名的相關(guān)代碼是這樣的:
private String getDataCollectionName() { Calendar calendar = Calendar.getInstance(); try { calendar.set(Calendar.DATE, -day); return "alarm_" + ToolUtils.dateToFormatStrDate(calendar.getTime(), "yyyy_MM"); } catch (Exception e) { logger.error("{}: data transformating failed,{}", this.getClass().getName(), e.getMessage()); } return null; } 問(wèn)題正是出在calendar的set方法上,包括后來(lái)的查詢數(shù)據(jù)使用的開(kāi)始和結(jié)束時(shí)間,也一樣用的是calendar.set(Calendar.DATE, -day);
這個(gè)方法乍一看起來(lái)似乎就是設(shè)置日期為當(dāng)前日期減去指定的天數(shù),但是實(shí)際上結(jié)果并不是想象中的這樣得到兩個(gè)月前的日期(這里的day它定義的是60,也就是兩個(gè)月)。
填坑:
找到原因之后,我把這個(gè)方法進(jìn)行了替換,把set改為了add,至于里邊的參數(shù)沒(méi)有去動(dòng),而結(jié)果證明這個(gè)方法才是真正能實(shí)現(xiàn)當(dāng)前功能的,得到的結(jié)果也正是預(yù)想中的結(jié)果。
當(dāng)然了,也可以不改方法的同時(shí)把參數(shù)略做改變,如calendar.set(Calendar.DATE, calendar.get(calendar.DATE)-day);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家掌握Calendar時(shí)間操作常用方法有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選