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); } } }
相关文章推荐
- JAVA基本框架搭建(Maven,jetty,Joda-time,junit)
- Java日期计算之Joda-Time
- Java|Joda-Time简化时间处理利器
- Java日期简化工具Joda-Time
- Java时间计算工具joda-time
- java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
- java日期计算之joda-time
- [Java]Java日期及时间库插件 -- Joda Time.
- Java日期计算之Joda-Time
- Elasticsearch运行报错:java.lang.ClassCastException: org.joda.time.DateTime cannot be cast to org.elastic
- java TimeUnit vs Thread.sleep的用法对比
- Joda-time是java处理时间非常棒的jar
- Java日期计算之Joda-Time
- 【java】之joda-time的使用
- 【转】Java日期计算之Joda-Time
- Java 日期处理类库Joda-Time简介
- java计算相隔时间差以及Joda-Time
- java开源时间框架joda-time简单使用的junit实例
- Java日期简化工具Joda-Time
- 【JAVA秒会技术之Joda-Time】满足你所有关于日期的处理