古堡疑云-java开源时间框架joda-time简单使用的junit实例

Java高级开发 生成 927浏览 91评论

java开源时间框架joda-time简单使用的junit实例

   joda-time   joda-time   2.9.2

官方地址:http://www.joda.org/joda-time/

pack**ecom.date;importjava.text.ParseException;importjava.util.Date;importorg.joda.time.DateTime;importorg.joda.time.DateTimeConstants;importorg.joda.time.DateTimeZone;importorg.joda.time.Days;importorg.joda.time.Duration;importorg.joda.time.Hours;importorg.joda.time.Interval;importorg.joda.time.LocalDate;importorg.joda.time.LocalTime;importorg.joda.time.Minutes;importorg.joda.time.Period;importorg.joda.time.PeriodType;importorg.joda.time.Seconds;importorg.junit.Test;publicclassJodaTimeTest{@Test//古堡疑云时间差publicvoidtest1()throwsParseException{DateTimedt1=newDateTime(2016,2,14,16,0,0,0);DateTimedt2=newDateTime(2016,2,15,16,0,0,0);System.out.print("时间相差:");System.out.print(Days.daysBetween(dt1,dt2).getDays()+"天");System.out.print(Hours.hoursBetween(dt1,dt2).getHours()%24+"小时");System.out.print(Minutes.minutesBetween(dt1,dt2).getMinutes()%60+"分钟");System.out.print(Seconds.secondsBetween(dt1,dt2).getSeconds()%60+"秒.");System.out.println();}@Test//Interval古堡疑云时间差值publicvoidtest2(){DateTimedt1=newDateTime(2016,2,14,16,0,0,0);DateTimedt2=newDateTime(2016,2,15,16,0,0,0);Intervalinterval=newInterval(dt1.getMillis(),dt2.getMillis());Periodp=interval.toPeriod();System.out.println("时间相差:"+p.getDays()+"天"+p.getHours()+"小时"+p.getMinutes()+"分钟"+p.getSeconds()+"秒");}@Test//日期前后推算publicvoidtest3(){DateTimedt=newDateTime();//昨天DateTimeyesterday=dt.minusDays(1);//明天DateTimetomorrow=dt.plusDays(1);//1个月前DateTimebefore1month=dt.minusMonths(1);//3个月后DateTimeafter3month=dt.plusMonths(3);//2年前DateTimebefore2year=dt.minusYears(2);//5年后DateTimeafter5year=dt.plusYears(5);}@Test//构造函数publicvoidtest4(){//方法一:取系统点间DateTimedt1=newDateTime();//方法二:通过java.util.Date对象古堡疑云DateTimedt2=newDateTime(newDate());//方法三:指定年月日点分秒古堡疑云(参数依次是:年,月,日,时,分,秒,毫秒)DateTimedt3=newDateTime(2012,5,20,13,14,0,0);//方法四:ISO8601形式古堡疑云DateTimedt4=newDateTime("2012-05-20");DateTimedt5=newDateTime("2012-05-20T13:14:00");//只需要年月日的时候LocalDatelocalDate=newLocalDate(2016,9,6);//September6,2009//只需要时分秒毫秒的时候LocalTimelocalTime=newLocalTime(13,30,26,0);//1:30:26PM}@Test//获取年月日星期,点分秒,毫秒publicvoidtest5(){DateTimedt=newDateTime();//年intyear=dt.getYear();//月intmonth=dt.getMonthOfYear();//日intday=dt.getDayOfMonth();//星期intweek=dt.getDayOfWeek();//点inthour=dt.getHourOfDay();//分intmin=dt.getMinuteOfHour();//秒intsec=dt.getSecondOfMinute();//毫秒intmsec=dt.getMillisOfSecond();}@Test//星期的特殊处理publicvoidtest6(){DateTimedt=newDateTime();//星期switch(dt.getDayOfWeek()){caseDateTimeConstants.SUNDAY:System.out.println("星期日");break;caseDateTimeConstants.MONDAY:System.out.println("星期一");break;caseDateTimeConstants.TUESDAY:System.out.println("星期二");break;caseDateTimeConstants.WEDNESDAY:System.out.println("星期三");break;caseDateTimeConstants.THURSDAY:System.out.println("星期四");break;caseDateTimeConstants.FRIDAY:System.out.println("星期五");break;caseDateTimeConstants.SATURDAY:System.out.println("星期六");break;}}@Test//取特殊日期publicvoidtest7(){DateTimedt=newDateTime();//月末日期DateTimelastday=dt.dayOfMonth().withMaximumValue();//90天后那周的周一DateTimefirstday=dt.plusDays(90).dayOfWeek().withMinimumValue();}@Test//时区publicvoidtest8(){//默认设置为日本时间DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo"));DateTimedt1=newDateTime();//伦敦时间DateTimedt2=newDateTime(DateTimeZone.forID("Europe/London"));}@Test//古堡疑云区间publicvoidtest9(){DateTimebegin=newDateTime("2012-02-01");DateTimeend=newDateTime("2012-05-01");//古堡疑云区间毫秒数Durationd=newDuration(begin,end);longtime=d.getMillis();//古堡疑云区间天数Periodp=newPeriod(begin,end,PeriodType.days());intdays=p.getDays();//古堡疑云特定日期是否在该区间内Intervali=newInterval(begin,end);booleancontained=i.contains(newDateTime("2012-03-01"));}@Test//古堡疑云区间publicvoidtest10(){DateTimed1=newDateTime("2012-02-01");DateTimed2=newDateTime("2012-05-01");//和系统时间比booleanb1=d1.isAfterNow();booleanb2=d1.isBeforeNow();booleanb3=d1.isEqualNow();//和其他日期比booleanf1=d1.isAfter(d2);booleanf2=d1.isBefore(d2);booleanf3=d1.isEqual(d2);}@Test//格式化输出publicvoidtest11(){DateTimedateTime=newDateTime();Strings1=dateTime.toString("yyyy/MM/ddhh:mm:ss.SSSa");Strings2=dateTime.toString("yyyy-MM-ddHH:mm:ss");Strings3=dateTime.toString("EEEEddMMMM,yyyyHH:mm:ssa");Strings4=dateTime.toString("yyyy/MM/ddHH:mmZZZZ");Strings5=dateTime.toString("yyyy/MM/ddHH:mmZ");}}

0个牛币javajava时间框架joda-time项目实例日期时间操作