首页>代码>【猪猪-后端】Joda开源扩展插件,神马日期工具类都是浮云!!Demo下载运行即可查看,注释齐全,真心好用,分享大家。>/JodaTime/src/com/test/joda/JodaTest.java
package com.test.joda; import java.text.DateFormat; import java.util.Locale; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class JodaTest { public static void main(String[] args) { DateTime dt = new DateTime(); // 转换Date类型 System.out.println(dt.toDate()); // 当前日期加上200小时后的星期 System.out.println(dt.plusHours(200).dayOfWeek().getAsText()); // 当前月加上2个月之后的月天数 System.out.println(dt.plusMonths(1).dayOfMonth().getAsText()); // 当前日期加上对应天数,今年已经过去多少天 System.out.println(dt.plusDays(10).dayOfYear().getAsText()); /** * Format the Date The first character is the date style, and the second * character is the time style. Specify a character of 'S' for short * style, 'M' for medium, 'L' for long, and 'F' for full. A date or time * may be ommitted by specifying a style character '-'. */ // 获取当前日期的年月日 DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd"); DateTimeFormatter fmt1 = DateTimeFormat.forPattern("yyyy-MMMM-dd"); String str = fmt.print(dt); String str1 = fmt1.print(dt); System.out.println(str); System.out.println(str1); //forStyle DateTimeFormatter fmtstyle = DateTimeFormat.forStyle("FL"); System.out.println(fmtstyle.withLocale(Locale.CHINA).print(dt)); // Short Date DateTimeFormatter stf = DateTimeFormat.shortDate(); DateTimeFormatter stf1 = DateTimeFormat.shortDateTime(); DateTimeFormatter stf8 = DateTimeFormat.shortTime(); System.out.println("shortDate===" + stf.print(dt)); System.out.println("shortDateTime===" + stf1.print(dt)); System.out.println("shortTime===" + stf8.print(dt)); // Medium Date DateTimeFormatter stf2 = DateTimeFormat.mediumDate(); DateTimeFormatter stf3 = DateTimeFormat.mediumTime(); DateTimeFormatter stf4 = DateTimeFormat.mediumDateTime(); System.out.println("mediumDate===" + stf2.print(dt)); System.out.println("mediumTime===" + stf3.print(dt)); System.out.println("mediumDateTime===" + stf4.print(dt)); // Short Date DateTimeFormatter stf5 = DateTimeFormat.longDate(); DateTimeFormatter stf6 = DateTimeFormat.longDateTime(); DateTimeFormatter stf7 = DateTimeFormat.longTime(); System.out.println("longDate===" + stf5.print(dt)); System.out.println("longDateTime===" + stf6.print(dt)); System.out.println("longTime===" + stf7.print(dt)); } }