DateTime 类常用API 博客分类: java DateTimeDate日期时区
程序员文章站
2024-03-16 08:26:34
...
开发中经常使用到日期,我比较常用的是joda-time包中的DateTime类,里面提供了的api基本满足了日常需求,以下是常用API的使用:
package test; import java.util.Calendar; import java.util.Date; import java.util.Locale; import org.joda.time.DateTime; import org.joda.time.DateTimeConstants; import org.joda.time.DateTimeZone; import org.joda.time.Duration; import org.joda.time.Interval; import org.joda.time.LocalDate; import org.joda.time.LocalTime; import org.joda.time.Period; import org.joda.time.PeriodType; public class TestDateTime { public static void main(String args[]) { /* * 1.初始化 */ //方法一:取系统点间 DateTime dt = new DateTime(); //方法二:通过java.util.Date对象生成 dt = new DateTime(new Date()); //方法三:指定年月日点分秒生成(参数依次是:年,月,日,时,分,秒,毫秒) dt = new DateTime(2012, 5, 20, 13, 14, 0, 0); //方法四:ISO8601形式生成 dt = new DateTime("2012-05-20"); dt = new DateTime("2012-05-20T13:14:00"); //只需要年月日的时候 LocalDate localDate = new LocalDate(2009, 9, 6);// September 6, 2009 //只需要时分秒毫秒的时候 LocalTime localTime = new LocalTime(13, 30, 26, 0);// 1:30:26PM /* * 2.获取年月日点分秒 */ //年 int year = dt.getYear(); //月 int month = dt.getMonthOfYear(); //日 int day = dt.getDayOfMonth(); //星期 int week = dt.getDayOfWeek(); //点 int hour = dt.getHourOfDay(); //分 int min = dt.getMinuteOfHour(); //秒 int sec = dt.getSecondOfMinute(); //毫秒 int msec = dt.getMillisOfSecond(); // 判断当前是星期几 switch(dt.getDayOfWeek()) { case DateTimeConstants.SUNDAY: System.out.println("星期日"); break; case DateTimeConstants.MONDAY: System.out.println("星期一"); break; case DateTimeConstants.TUESDAY: System.out.println("星期二"); break; case DateTimeConstants.WEDNESDAY: System.out.println("星期三"); break; case DateTimeConstants.THURSDAY: System.out.println("星期四"); break; case DateTimeConstants.FRIDAY: System.out.println("星期五"); break; case DateTimeConstants.SATURDAY: System.out.println("星期六"); break; } /* * 3.与JDK日期对象的转换 */ //转换成java.util.Date对象 Date d1 = new Date(dt.getMillis()); Date d2 = dt.toDate(); //转换成java.util.Calendar对象 Calendar c1 = Calendar.getInstance(); c1.setTimeInMillis(dt.getMillis()); Calendar c2 = dt.toCalendar(Locale.getDefault()); /* * 4.日期前后推算 (这个方法比较实用) */ //天 DateTime yesterday = dt.minusDays(1); DateTime tomorrow = dt.plusDays(1); //月 DateTime lastMonth = dt.minusMonths(1); DateTime nextMonth = dt.plusMonths(1); //周 DateTime lastWeek = dt.minusWeeks(-1); DateTime nexWeek = dt.plusWeeks(1); //年 DateTime lastYear = dt.minusYears(1); DateTime nextYear = dt.plusYears(1); /* * 5.取特殊日期 */ //月末日期 DateTime lastday = dt.dayOfMonth().withMaximumValue(); //90天后那周的周一 DateTime firstday = dt.plusDays(90).dayOfWeek().withMinimumValue(); /* * 6.设置时区 */ //日本时间 DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo")); dt = new DateTime(); //伦敦时间 dt = new DateTime(DateTimeZone.forID("Europe/London")); //东八区时间 DateTimeZone.setDefault(DateTimeZone.forID("Asia/Shanghai")); /* * 7.计算区间 */ DateTime begin = new DateTime("2012-02-01"); DateTime end = new DateTime("2012-05-01"); //计算区间毫秒数 Duration d = new Duration(begin, end); long time = d.getMillis(); //计算区间天数 Period p = new Period(begin, end, PeriodType.days()); int days = p.getDays(); //计算特定日期是否在该区间内 Interval i = new Interval(begin, end); boolean contained = i.contains(new DateTime("2012-03-01")); /* * 8.日期比较 */ DateTime dt1 = new DateTime("2012-02-01"); DateTime dt2 = new DateTime("2012-05-01"); //和系统时间比 boolean b1 = dt1.isAfterNow(); boolean b2 = dt1.isBeforeNow(); boolean b3 = dt1.isEqualNow(); //和其他日期比 boolean f1 = dt1.isAfter(dt2); boolean f2 = dt1.isBefore(dt2); boolean f3 = dt1.isEqual(dt2); /* * 9.格式化输出 */ DateTime dateTime = new DateTime(); String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa"); String s2 = dateTime.toString("yyyy-MM-dd HH:mm:ss"); String s3 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa"); String s4 = dateTime.toString("yyyy/MM/dd HH:mm ZZZZ"); //ZZZZ 表示 时区 String s5 = dateTime.toString("yyyy/MM/dd HH:mm Z"); } }