转自:Ricky_Fung
今天在spring官网demo中无意看到了Joda-Time,于是去Joda-Time官网了解了一下,瞬间被它的强大功能和易用性所折服。
Joda-Time简介
Joda-Time — 面向 Java 应用程序的日期/时间库的替代选择,Joda-Time 令时间和日期值变得易于管理、操作和理解。事实上,易于使用是 Joda 的主要设计目标。其他目标包括可扩展性、完整的特性集以及对多种日历系统的支持。并且 Joda 与 JDK 是百分之百可互操作的,因此您无需替换所有 Java 代码,只需要替换执行日期/时间计算的那部分代码。
Joda-Time使用
1.创建一个用时间表示的某个随意的时刻 — 比如,2015年12月21日0时0分
DateTime dt = new DateTime(2015, 12, 21, 0, 0, 0, 333);
2.格式化时间输出
DateTime dateTime = new DateTime(2015, 12, 21, 0, 0, 0, 333)System.out.println(dateTime.toString("yyyy/MM/dd HH:mm:ss EE"))
3.解析文本格式时间
DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss")DateTime dateTime = DateTime.parse("2015-12-21 23:22:45", format)System.out.println(dateTime.toString("yyyy/MM/dd HH:mm:ss EE"))
4.在某个日期上加上90天并输出结果
DateTime dateTime = new DateTime(2016, 1, 1, 0, 0, 0, 0)System.out.println(dateTime.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS")
5.到新年还有多少天
public Days daysToNewYear(LocalDate fromDate) { LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1); return Days.daysBetween(fromDate, newYear);}
6.与JDK日期对象的转换
DateTime dt = new DateTime(); Date d1 = new Date(dt.getMillis()); Date d2 = dt.toDate();
7.时区
//默认设置为日本时间 DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo"))DateTime dt1 = new DateTime()System.out.println(dt1.toString("yyyy-MM-dd HH:mm:ss"))//伦敦时间 DateTime dt2 = new DateTime(DateTimeZone.forID("Europe/London"))System.out.println(dt2.toString("yyyy-MM-dd HH:mm:ss"))
8.计算间隔和区间
DateTime begin = new DateTime("2015-02-01"); DateTime end = new DateTime("2016-05-01"); Duration d = new Duration(begin, end); long millis = d.getMillis(); Period p = new Period(begin, end, PeriodType.days()); int days = p.getDays(); Interval interval = new Interval(begin, end); boolean contained = interval.contains(new DateTime("2015-03-01"));
9.日期比较
DateTime d1 = new DateTime("2015-10-01"); DateTime d2 = new DateTime("2016-02-01"); boolean b1 = d1.isAfterNow(); boolean b2 = d1.isBeforeNow(); boolean b3 = d1.isEqualNow(); boolean f1 = d1.isAfter(d2); boolean f2 = d1.isBefore(d2); boolean f3 = d1.isEqual(d2);
下面通过一个简单的demo来实战一下,输入一个日期(生日,格式:yyyy-MM-dd Or yyyy-MM-dd HH:mm:ss),计算出今天是你人生的第多少天/小时/分/秒,代码如下:
package com.ricky.spring.springdemoimport org.joda.time.DateTimeimport org.joda.time.Daysimport org.joda.time.Hoursimport org.joda.time.Minutesimport org.joda.time.Secondsimport org.joda.time.format.DateTimeFormatimport org.joda.time.format.DateTimeFormatterimport java.util.Scannerpublic class DayOfLife { public static void main(String[] args) { new DayOfLife().calDayNumber() } public void calDayNumber(){ Scanner scanner = new Scanner(System.in) try{ while(true){ System.out.println("*********请输入生日(yyyy-MM-dd Or yyyy-MM-dd HH:mm:ss)*********") String line = scanner.nextLine() if("exit".equalsIgnoreCase(line)){ break } DateTimeFormatter format = null if(line.length()==10){ format = DateTimeFormat .forPattern("yyyy-MM-dd") }else{ format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss") } DateTime startDateTime = null try { startDateTime = DateTime.parse(line, format) } catch (Exception e) { System.err.println("输入格式错误,请重新输入生日!") continue } calDays(startDateTime, new DateTime()) } }finally{ scanner.close() } } private void calDays(DateTime startDateTime,DateTime endDateTime){ Days days = Days.daysBetween(startDateTime, endDateTime) System.out.println("今天是你人生的第"+days.getDays()+"天") Hours hours = Hours.hoursBetween(startDateTime, endDateTime) System.out.println("今天是你人生的第"+hours.getHours()+"小时") Minutes minutes = Minutes.minutesBetween(startDateTime, endDateTime) System.out.println("今天是你人生的第"+minutes.getMinutes()+"分钟") Seconds seconds = Seconds.secondsBetween(startDateTime, endDateTime) System.out.println("今天是你人生的第"+seconds.getSeconds()+"秒") }}
- 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
- 63
- 64
- 65
- 66
- 67
- 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
- 63
- 64
- 65
- 66
- 67
参考链接:
http://www.joda.org/joda-time/quickstart.html
http://www.ibm.com/developerworks/cn/java/j-jodatime.html
0 0