一、Joda Time基礎(chǔ)操作#
1瞧哟、 構(gòu)造指定時(shí)間#
// 明確給出年月日時(shí)分秒,同時(shí)還可以指定毫秒
DateTime dateTime = new DateTime(2017,9,14,20,30,0);
// 使用時(shí)間戳構(gòu)造
Datetime dateTime = new DateTime(1505371053358L);
// 使用字符串構(gòu)造迫筑,使用字符串構(gòu)造需要自己定義pattern
String date = "2017-09-14 20:30:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = dateTimeFormatter.parseDateTime(date);
// 指定時(shí)區(qū)構(gòu)造時(shí)間
DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Shanghai")));
注意:”Asia/Shanghai”是國(guó)際時(shí)區(qū)Id规惰,該ID可以通過(guò)JDK代碼獲取丹弱,代碼如下:
String[] zones = TimeZone.getAvailableIDs();
for (String zone : zones) {
System.out.println(zone);
}
2氮帐、獲取當(dāng)前時(shí)間的時(shí)間戳#
// JDK
long currentTimeOfMills = System.currentTimeMillis();
// Joda Time
long currentTimeOfMills = DateTime.now().getMillis();
3隘截、獲得當(dāng)前時(shí)間的時(shí)區(qū)#
DateTimeZone zone = DateTime.now().getZone();
4扎阶、 獲取指定時(shí)區(qū)的當(dāng)前時(shí)間#
<p
DateTimeZone gmt = DateTimeZone.forID("GMT"); DateTime dateTime = DateTime.now().toDateTime(gmt);
</pre>
二汹胃、Joda Time 對(duì)年月日的一些簡(jiǎn)單操作。#
1东臀、 獲取月初第一天和月末最后一天#
DateTime dateTime = new DateTime();
// 月初第一天
DateTime theFirstDateOfMonth = dateTime.dayOfMonth().withMinimumValue();
// 當(dāng)前月最后一天
DataTime theEndDataOfMonth = dateTime.dayOfMonth().withMaximumValue();
// 這一天是幾號(hào)
int day = dateTime.getDayOfMonth();
// 這一天是哪月
int month = dateTime.getMothOfYear();
// 這一天是哪年
int year = dateTime.getYear();
// 判斷本月是不是9月
if(dateTime.getDayOfMonth() == DateTimeConstants.SEPTEMBER){
//TODO
}
// 獲取相對(duì)于當(dāng)前時(shí)間的月份着饥,比如獲取上個(gè)月的時(shí)間或者下個(gè)月的是時(shí)間,方法minusMoths接受一個(gè)int的參數(shù)惰赋,如果這個(gè)參數(shù)等于0宰掉,代表本月,大于0代表已經(jīng)過(guò)去的時(shí)間赁濒,小于0代表還沒(méi)有到來(lái)的時(shí)間
LocalDate lastDayOfMonth = new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue();
2轨奄、關(guān)于星期的操作#
DateTime dateTime = new DateTime();
// 今天是星期幾
int week = dateTime.getDayOfWeek();
// 判斷今天是不是星期三
if(dateTime.getDayOfWeek() == DateTimeConstants.WEDNESDAY){
// TODO
}
注意:DateTimeConstants中包含了許多你需要的常量,而不用你自己去定義流部,比如星期戚绕、月份、上午還是下午都有哦
3枝冀、計(jì)算時(shí)間差#
注意開(kāi)始時(shí)間與結(jié)束時(shí)間參數(shù)位置舞丛,如果開(kāi)始時(shí)間小于結(jié)束時(shí)間,得到的天數(shù)是正數(shù)果漾,否則就是負(fù)數(shù)哦球切!
DateTime currentDateTime = new DateTime();
DateTime targetDateTime = new DateTime(2017,10,1,0,0,0);
// 相差多少年
int years = Years.yearsBetween(currentDateTime,targetDateTime).getYears();
// 相差多少月
int months = Months.monthsBetween(currentDateTime,targetDateTime).getMonths();
// 距離國(guó)慶放假還有多少天,嘎嘎绒障!
int days = Days.daysBetween(currentDateTime,targetDateTime).getDays();
// 相差多少小時(shí)
int hours = Hours.hoursBetween(currentDateTime,targetDateTime).getHours();
// 相差多少分鐘
int minutes = Minutes.minutesBetween(currentDateTime,targetDateTime).getMinutes();
// 相差多少秒
int seconds = Seconds.secondsBetween(currentDateTime,targetDateTime).getSeconds();
// 相差多少周
int weeks = Weeks.weeksBetween(currentDateTime,targetDateTime).getWeeks();
4吨凑、獲取零點(diǎn)相關(guān)的時(shí)間#
DateTime currentDateTime = new DateTime();
// 今天的零點(diǎn)
DateTime dateTime = currentDateTime.withMillisOfDay(0);
// 昨天的零點(diǎn)
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(-1);
// 明天的零點(diǎn)
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(1);
// 這一年最后一天0點(diǎn)
new DateTime().dayOfYear().withMaximumValue().withMillisOfDay(0)
// 這一年第一天0點(diǎn)
new DateTime().dayOfYear().withMinimumValue().withMillisOfDay(0)
// 這個(gè)月最后一天0點(diǎn)
new DateTime().dayOfMonth().withMaximumValue().withMillisOfDay(0)
// 這個(gè)月月初0點(diǎn)
new DateTime().dayOfMonth().withMinimumValue().withMillisOfDay(0)
注意:要獲取多少天后或者多少天前的零點(diǎn)户辱,只需在plusDays()方法中填寫(xiě)相應(yīng)參數(shù)即可
三鸵钝、準(zhǔn)確使用Joda Time的時(shí)間處理類(lèi)#
1、格式化就這么簡(jiǎn)單#
// 格式化時(shí)間
DateTime currentDateTime = new DateTime();
currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
// 指定時(shí)區(qū)格式化
String format = "yyyy-MM-dd HH:mm:ss";
DateTime dateTime = new DateTime();
dateTime.toString(format, Locale.US);
// 格式化時(shí)分秒(單位毫秒并且最大可格式23:59:59庐镐,超出將報(bào)錯(cuò))
int millis = 120000;
LocalTime localTime = new LocalTime().withMillisOfDay(millis);
localTime.toString("HH:mm:ss");
2恩商、 如果業(yè)務(wù)只需要日期,請(qǐng)使用LocalDate,因?yàn)長(zhǎng)ocalDate僅僅關(guān)心日期必逆,更專(zhuān)業(yè)怠堪,也減少了不必要的資源消耗;如果業(yè)務(wù)只關(guān)心時(shí)間名眉,那么使用LocalTime粟矿。例如:#
LocalDate localDate = new LocalDate();
LocalTime localTime = new LocalTime();
System.out.println(localDate);
// 2017-09-14
System.out.println(localTime);
//10:54:14.506
3、 如果業(yè)務(wù)需要日期時(shí)間都要使用损拢,那么可以使用LocalDateTime, DateTime這兩個(gè)類(lèi)陌粹,它們都是線程安全的同時(shí)都是不可變的,使用起來(lái)不用擔(dān)心出問(wèn)題福压。#
LocalDateTime是與時(shí)區(qū)無(wú)關(guān)的申屹。
DateTime是與時(shí)區(qū)相關(guān)的一個(gè)國(guó)際標(biāo)準(zhǔn)時(shí)間绘证。
使用的時(shí)候根據(jù)自己的需要選擇,詳細(xì)的解釋看官方文檔吧哗讥!
4嚷那、再次提醒要使用DateTimeConstants類(lèi)定義好的常量,避免重復(fù)造輪子杆煞。下面給出DateTimeConstants類(lèi)的常量(也不多)魏宽,不在解釋?zhuān)x。#
// 月份
public static final int JANUARY = 1;
public static final int FEBRUARY = 2;
public static final int MARCH = 3;
public static final int APRIL = 4;
public static final int MAY = 5;
public static final int JUNE = 6;
public static final int JULY = 7;
public static final int AUGUST = 8;
public static final int SEPTEMBER = 9;
public static final int OCTOBER = 10;
public static final int NOVEMBER = 11;
public static final int DECEMBER = 12;
// 星期
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;
// 上午&下午
public static final int AM = 0;
public static final int PM = 1;
// 公元前...年(基督之前...年)
public static final int BC = 0;
// 公元前
public static final int BCE = 0;
// 公元...年(原義為主的紀(jì)年)
public static final int AD = 1;
// 基督紀(jì)元,公元
public static final int CE = 1;
// 1秒對(duì)應(yīng)毫秒數(shù)
public static final int MILLIS_PER_SECOND = 1000;
// 1分鐘對(duì)應(yīng)秒數(shù)
public static final int SECONDS_PER_MINUTE = 60;
// 1分鐘對(duì)應(yīng)毫秒數(shù)
public static final int MILLIS_PER_MINUTE = 60000;
// 1小時(shí)對(duì)應(yīng)分鐘數(shù)
public static final int MINUTES_PER_HOUR = 60;
// 1小時(shí)對(duì)應(yīng)的秒數(shù)
public static final int SECONDS_PER_HOUR = 3600;
// 1小時(shí)對(duì)應(yīng)的毫秒數(shù)
public static final int MILLIS_PER_HOUR = 3600000;
// 1天對(duì)應(yīng)的小時(shí)
public static final int HOURS_PER_DAY = 24;
// 1天對(duì)應(yīng)的分鐘數(shù)
public static final int MINUTES_PER_DAY = 1440;
// 1天對(duì)應(yīng)的秒數(shù)
public static final int SECONDS_PER_DAY = 86400;
// 1天對(duì)應(yīng)的毫秒數(shù)
public static final int MILLIS_PER_DAY = 86400000;
// 1周對(duì)應(yīng)的天數(shù)
public static final int DAYS_PER_WEEK = 7;
// 1周對(duì)應(yīng)的小時(shí)
public static final int HOURS_PER_WEEK = 168;
// 1周對(duì)應(yīng)的分鐘
public static final int MINUTES_PER_WEEK = 10080;
// 1周對(duì)應(yīng)的秒數(shù)
public static final int SECONDS_PER_WEEK = 604800;
// 1周對(duì)應(yīng)的毫秒數(shù)
public static final int MILLIS_PER_WEEK = 604800000;