您的位置:首页 > 编程语言 > Java开发

java 8 time包与joda 对比

2016-11-18 13:43 176 查看
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);
}

}


import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
*
* @author ibm
*
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
LocalDate today = LocalDate.now();

System.out.println("Today's Local date : " + today);
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.printf("Year : %d Month : %d day : %d \t %n", year, month, day);
/**
* 创建本地日期
*/
LocalDate dateOfBirth = LocalDate.of(2010, 1, 14);
System.out.println("Your Date of birth is : " + dateOfBirth);
/**
* 获取一周后的日期
*/
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Today is : " + today);
System.out.println("Date after 1 week : " + nextWeek);
/**
* 日期前后判断
*/
LocalDate tomorrow = LocalDate.of(2016, 11, 19);
if(tomorrow.isAfter(today)){
System.out.println("Tomorrow comes after today");
}
LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
if(yesterday.isBefore(today)){
System.out.println("Yesterday is day before today");

Instant instant = Instant.now();
ZonedDateTime zonew = ZonedDateTime.ofInstant(instant, ZoneId.of("America/New_York"));
System.out.println(zonew);

}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: