joda 适用于 java 5,java 6 ,Java 7
import org.joda.time.LocalDate; import org.joda.time.DateTimeZone; import org.joda.time.LocalDate; /** * joda * * @author ibm * */ public class App2 { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("Today's Local date : " + today); int year = today.getYear(); int month = today.getMonthOfYear(); int day = today.getDayOfMonth(); System.out.printf("Year : %d Month : %d day : %d \t %n", year, month, day); /** * 创建本地日期 */ LocalDate dateOfBirth = new LocalDate(2010, 1, 12); System.out.println("Your Date of birth is : " + dateOfBirth); /** * 获取一周后的日期 */ LocalDate nextWeek = today.plusWeeks(1); System.out.println("Today is : " + today); System.out.println("Date after 1 week : " + nextWeek); /** * 日期前后判断 */ LocalDate tomorrow = new LocalDate(2016, 11, 19); if(tomorrow.isAfter(today)){ System.out.println("Tomorrow comes after today"); } LocalDate yesterday = today.minusDays(1); if(yesterday.isBefore(today)){ System.out.println("Yesterday is day before today"); } /** * 创建包含时区的日期时间 */ DateTimeZone zone = DateTimeZone.forID("America/New_York"); DateTime dateAndTimeInNewYork = new DateTime(null, zone); System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork); } }