第一步:引入依赖:
1 <dependency>
2 <groupId>joda-time</groupId>
3 <artifactId>joda-time</artifactId>
4 <version>2.6</version>
5 </dependency>

官网:
Home:http://joda-time.sourceforge.net/

JavaDoc:http://joda-time.sourceforge.net/apidocs/index.html
互联网上的小例子:
        package com.yan.joda;
0
1 import java.util.Calendar;
2 import java.util.Date;
3 import java.util.Locale;
4 import org.joda.time.DateTime;
5 import org.joda.time.Days;
6 import org.joda.time.LocalDate;
7 import org.joda.time.format.DateTimeFormat;
8 import org.joda.time.format.DateTimeFormatter;
9
0 public class JodaTest {
1
2 public static void main(String[] args) {
3 //初始化时间
4 DateTime dateTime=new DateTime(2012, 12, 13, 18, 23,55);
5
6 // 年,月,日,时,分,秒,毫秒
7 DateTime dt3 = new DateTime(2011, 2, 13, 10, 30, 50, 333);// 2010年2月13日10点30分50秒333毫秒
8
9 //下面就是按照一点的格式输出时间
0 String str2 = dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa");
1 String str3 = dateTime.toString("dd-MM-yyyy HH:mm:ss");
2 String str4 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");
3 String str5 = dateTime.toString("MM/dd/yyyy HH:mm ZZZZ");
4 String str6 = dateTime.toString("MM/dd/yyyy HH:mm Z");
5
6 DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
7 //时间解析
8 DateTime dateTime2 = DateTime.parse("2012-12-21 23:22:45", format);
9
0 //时间格式化,输出==> 2012/12/21 23:22:45 Fri
1 String string_u = dateTime2.toString("yyyy/MM/dd HH:mm:ss EE");
2 System.out.println(string_u);
3
4 //格式化带Locale,输出==> 2012年12月21日 23:22:45 星期五
5 String string_c = dateTime2.toString("yyyy年MM月dd日 HH:mm:ss EE",Locale.CHINESE);
6 System.out.println(string_c);
7
8 DateTime dt1 = new DateTime();// 取得当前时间
9
0 // 根据指定格式,将时间字符串转换成DateTime对象,这里的格式和上面的输出格式是一样的
1 DateTime dt2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2012-12-26 03:27:39");
2
3 //计算两个日期间隔的天数
4 LocalDate start=new LocalDate(2012, 12,14);
5 LocalDate end=new LocalDate(2013, 01, 15);
6 int days = Days.daysBetween(start, end).getDays();
7
8 //计算两个日期间隔的小时数,分钟数,秒数
9
0 //增加日期
1 DateTime dateTime1 = DateTime.parse("2012-12-03");
2 dateTime1 = dateTime1.plusDays(30);
3 dateTime1 = dateTime1.plusHours(3);
4 dateTime1 = dateTime1.plusMinutes(3);
5 dateTime1 = dateTime1.plusMonths(2);
6 dateTime1 = dateTime1.plusSeconds(4);
7 dateTime1 = dateTime1.plusWeeks(5);
8 dateTime1 = dateTime1.plusYears(3);
9
0 // Joda-time 各种操作.....
1 dateTime = dateTime.plusDays(1) // 增加天
2 .plusYears(1)// 增加年
3 .plusMonths(1)// 增加月
4 .plusWeeks(1)// 增加星期
5 .minusMillis(1)// 减分钟
6 .minusHours(1)// 减小时
7 .minusSeconds(1);// 减秒数
8 .minusMonths(2);// 减月
9 //判断是否闰月
0 DateTime dt4 = new DateTime();
1 org.joda.time.DateTime.Property month = dt4.monthOfYear();
2 System.out.println("是否闰月:" + month.isLeap());
3
4 //取得 3秒前的时间
5 DateTime dt5 = dateTime1.secondOfMinute().addToCopy(-3);
6 dateTime1.getSecondOfMinute();// 得到整分钟后,过的秒钟数
7 dateTime1.getSecondOfDay();// 得到整天后,过的秒钟数
8 dateTime1.secondOfMinute();// 得到分钟对象,例如做闰年判断等使用
9
0 // DateTime与java.util.Date对象,当前系统TimeMillis转换
1 DateTime dt6 = new DateTime(new Date());
2 Date date = dateTime1.toDate();
3 DateTime dt7 = new DateTime(System.currentTimeMillis());
4 dateTime1.getMillis();
5
6 Calendar calendar = Calendar.getInstance();
7 dateTime = new DateTime(calendar);
8 }
9 }
     下面是一个小例子用来计算小宝宝从出生到现在总共的天数小时数等,首先用jdk的类去做,不用joda,然后再用joda去做,以做比较
10
15 用jdk做的例子,这里算的从出生到现在的时间间隔是准确的,如果是输入的某天来算的话就不是很准确,多一秒就算一天。
可以看到用jdk去做的话,要写的代码还是挺繁琐的。
10 import java.text.SimpleDateFormat;
10 import java.util.Calendar;
19 import java.util.Date;
11 import java.util.Scanner;
11
11 public class CalBaby {
11 private final static String birthday = "2012-3-10 08:20:55";
11 /**
11 * @param args
11 */
11 public static void main(String[] args) {
11 while(true){
11 String format1 = "yyyy-MM-dd";
12 String format2 = "yyyy-MM-dd HH:mm:ss";
12 Scanner s = new Scanner(System.in);
12 System.out.println("########################################");
12 cutTwoDateToDay(convertToDate1(birthday,format2),new Date(),false);
12 System.out.println("请选择操作");
12 System.out.println("请输入日期(格式例如:2012-11-08)");
12 System.out.println("########################################");
12 String endDateStr = s.nextLine();
12 Date endDate = convertToDate1(endDateStr,format1);
12 if(endDate == null){
13 System.out.println("输入格式错误!请重新输入.");
13 continue;
13 }
13 boolean inputFlag = true;
13 cutTwoDateToDay(convertToDate1(birthday,format2),endDate,inputFlag);
15 }
16
17 }
18
39 /**
10 * 计算两个日期之间的差距天数
11 *
42 * @param a
13 * @param b
14 * @return
15 */
16 public static void cutTwoDateToDay(Date beginDate, Date endDate,boolean inputFlag) {
17 Calendar calendar = Calendar.getInstance();
18 long intervalDays = 0;
49 calendar.setTime(beginDate);
150 long begin = calendar.getTimeInMillis();
15 calendar.setTime(endDate);
12 long end = calendar.getTimeInMillis();
53 long totalM = end - begin;
54 System.out.println((end -begin));
55 System.out.println(24*60*60*1000);
56 intervalDays = totalM /(24*60*60*1000);
57 long intervalHours = (totalM - (intervalDays*24*60*60*1000))/(60*60*1000);
58 long intervalMin = (totalM - intervalDays * (24*60*60*1000) - intervalHours*60*60*1000)/(60*1000);
59 if(inputFlag){
60 if(totalM > 0L && totalM %(24*60*60*1000) > 0L){
61 intervalDays = intervalDays + 1;
62 }
63 System.out.println("宝宝从出生到"+formatDate(endDate,"yyyy-MM-dd")+"已经"+intervalDays+"天了");
14 }else{
15 System.out.println("宝宝来到这个世界已经"+intervalDays+"天"+intervalHours+"小时"+intervalMin+"分钟了");
16 }
67
18 }
19
10 /**
11 * 将字符串日期转换为Date yyyy-MM-dd HH:mm:ss yyyy-MM-dd
12 *
13 * @param s
14 * @return
75 */
76 public static Date convertToDate1(String s,String format) {
17
18 if (s == null) {
19 return null;
80 }
1 try {
82 SimpleDateFormat df = new SimpleDateFormat(format);
183 return df.parse(s);
14 } catch (Exception e) {
18 return null;
16 }
17 }
88
9 public static String formatDate(Date date, String strType)
90 {
19 if (date == null)
19 {
13 return null;
14 }
95
96 SimpleDateFormat sdf = new SimpleDateFormat(strType);
97 try
18 {
99 return sdf.format(date);
00 }
1 catch (Exception e)
20 {
20 return null;
24 }
05 }
06 }
07
08
09
0 下面是用joda来做,用这个来做就简单的多了,而且也很准确。
11
12 Java代码 收藏代码
21
21 import java.util.Scanner;
2 import org.joda.time.DateTime;
26 import org.joda.time.Days;
27 import org.joda.time.LocalDate;
28 import org.joda.time.format.DateTimeFormat;
29 import org.joda.time.format.DateTimeFormatter;
2
21 public class CalBabyJoda {
22
22 private final static String birthday = "2012-3-10 08:20:55";
22
22 public static void main(String[] args) {
22 while(true){
22 Scanner s = new Scanner(System.in);
22 System.out.println("########################################");
22 DateTimeFormatter format1 = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
23 DateTimeFormatter format2 = DateTimeFormat .forPattern("yyyy-MM-dd");
23 DateTime startDateTime = DateTime.parse(birthday, format1);
23 System.out.println("宝宝来到这个世界已经");
23 calDateToDay(startDateTime,new DateTime());
24 System.out.println("如选择其它日期请输入(格式例如:2012-11-08 14:24:54或着2012-11-08)");
5 System.out.println("########################################");
26 String endDate = s.nextLine();
37 DateTime endDateTime = null;
38 try{
39 endDateTime = DateTime.parse(endDate,format1);
24 }catch(Exception e){
2 try{
2 endDateTime = DateTime.parse(endDate,format2);
2 }catch(Exception e1){
24 System.out.println("输入格式错误!请重新输入.");
24 continue;
24 }
24 }
24 System.out.println("宝宝从出生到" + endDateTime.toString("yyyy-MM-dd HH:mm:ss") + "已经");
29 calDateToDay(startDateTime,endDateTime);
25 }
25 }
25
25 public static void calDateToDay(DateTime startDateTime,DateTime endDateTime){
25
25 LocalDate start=new LocalDate(startDateTime);
25 LocalDate end=new LocalDate(endDateTime);
25 Days days = Days.daysBetween(start, end);
25 int intervalDays = days.getDays();
25 int intervalHours = endDateTime.getHourOfDay() - startDateTime.getHourOfDay();
26 int intervalMinutes = endDateTime.getMinuteOfHour() - startDateTime.getMinuteOfHour();
26 int intervalSeconds = endDateTime.getSecondOfMinute() - startDateTime.getSecondOfMinute();
26
23
24 if(intervalSeconds < 0){
25 intervalMinutes = intervalMinutes -1;
26 intervalSeconds = 60 + intervalSeconds;
27 }
28
29 if(intervalMinutes < 0){
2 intervalHours = intervalHours -1;
21 intervalMinutes = 60 + intervalMinutes;
22 }
23
24 if(intervalHours < 0){
2 intervalDays = intervalDays -1;
27 intervalHours = 24 + intervalHours;
27 }
8
29 System.out.println(intervalDays + "天" + intervalHours +
20 "小时" + intervalMinutes + "分钟" + intervalSeconds + "秒");
81 System.out.println("############################");
82 }
83
4 }
个人学习笔记
05-12 13:06