一、Joda Time基础操作
1、 构造指定时间
1 2 3 4 5 6 7 8 9 10 11 12 13
| DateTime dateTime = new DateTime(2017,9,14,20,30,0);
Datetime dateTime = new DateTime(1505371053358L);
String date = "2017-09-14 20:30:00"; DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); DateTime dateTime = dateTimeFormatter.parseDateTime(date);
DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Shanghai")));
|
注意:”Asia/Shanghai”是国际时区Id,该ID可以通过JDK代码获取,代码如下:
1 2 3 4
| String[] zones = TimeZone.getAvailableIDs(); for (String zone : zones) { System.out.println(zone); }
|
2、获取当前时间的时间戳
1 2 3 4
| long currentTimeOfMills = System.currentTimeMillis();
long currentTimeOfMills = DateTime.now().getMillis();
|
3、获得当前时间的时区
1
| DateTimeZone zone = DateTime.now().getZone();
|
4、 获取指定时区的当前时间
1 2
| DateTimeZone gmt = DateTimeZone.forID("GMT"); DateTime dateTime = DateTime.now().toDateTime(gmt);
|
二、Joda Time 对年月日的一些简单操作。
1、 获取月初第一天和月末最后一天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| DateTime dateTime = new DateTime();
DateTime theFirstDateOfMonth = dateTime.dayOfMonth().withMinimumValue();
DataTime theEndDataOfMonth = dateTime.dayOfMonth().withMaximumValue();
int day = dateTime.getDayOfMonth();
int month = dateTime.getMothOfYear();
int year = dateTime.getYear();
if(dateTime.getDayOfMonth() == DateTimeConstants.SEPTEMBER){
}
LocalDate lastDayOfMonth = new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue();
|
2、关于星期的操作
1 2 3 4 5 6 7
| DateTime dateTime = new DateTime();
int week = dateTime.getDayOfWeek();
if(dateTime.getDayOfWeek() == DateTimeConstants.WEDNESDAY){ }
|
注意:DateTimeConstants中包含了许多你需要的常量,而不用你自己去定义,比如星期、月份、上午还是下午都有哦
3、计算时间差
注意开始时间与结束时间参数位置,如果开始时间小于结束时间,得到的天数是正数,否则就是负数哦!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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();
int days = Days.daysBetween(currentDateTime,targetDateTime).getDays();
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、获取零点相关的时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| DateTime currentDateTime = new DateTime();
DateTime dateTime = currentDateTime.withMillisOfDay(0);
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(-1);
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(1);
new DateTime().dayOfYear().withMaximumValue().withMillisOfDay(0)
new DateTime().dayOfYear().withMinimumValue().withMillisOfDay(0)
new DateTime().dayOfMonth().withMaximumValue().withMillisOfDay(0)
new DateTime().dayOfMonth().withMinimumValue().withMillisOfDay(0)
|
注意:要获取多少天后或者多少天前的零点,只需在plusDays()方法中填写相应参数即可
三、准确使用Joda Time的时间处理类
1、格式化就这么简单
1 2 3 4 5 6 7 8 9 10 11 12 13
| DateTime currentDateTime = new DateTime(); currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
String format = "yyyy-MM-dd HH:mm:ss"; DateTime dateTime = new DateTime(); dateTime.toString(format, Locale.US);
int millis = 120000; LocalTime localTime = new LocalTime().withMillisOfDay(millis); localTime.toString("HH:mm:ss");
|
2、 如果业务只需要日期,请使用LocalDate,因为LocalDate仅仅关心日期,更专业,也减少了不必要的资源消耗;如果业务只关心时间,那么使用LocalTime。例如:
1 2 3 4 5 6
| LocalDate localDate = new LocalDate(); LocalTime localTime = new LocalTime(); System.out.println(localDate);
System.out.println(localTime);
|
3、 如果业务需要日期时间都要使用,那么可以使用LocalDateTime, DateTime这两个类,它们都是线程安全的同时都是不可变的,使用起来不用担心出问题。
LocalDateTime是与时区无关的。
DateTime是与时区相关的一个国际标准时间。
使用的时候根据自己的需要选择,详细的解释看官方文档吧!
4、再次提醒要使用DateTimeConstants类定义好的常量,避免重复造轮子。下面给出DateTimeConstants类的常量(也不多),不在解释,望名知义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| 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;
public static final int AD = 1;
public static final int CE = 1;
public static final int MILLIS_PER_SECOND = 1000;
public static final int SECONDS_PER_MINUTE = 60;
public static final int MILLIS_PER_MINUTE = 60000;
public static final int MINUTES_PER_HOUR = 60;
public static final int SECONDS_PER_HOUR = 3600;
public static final int MILLIS_PER_HOUR = 3600000;
public static final int HOURS_PER_DAY = 24;
public static final int MINUTES_PER_DAY = 1440;
public static final int SECONDS_PER_DAY = 86400;
public static final int MILLIS_PER_DAY = 86400000;
public static final int DAYS_PER_WEEK = 7;
public static final int HOURS_PER_WEEK = 168;
public static final int MINUTES_PER_WEEK = 10080;
public static final int SECONDS_PER_WEEK = 604800;
public static final int MILLIS_PER_WEEK = 604800000;
|