java 8 中新的 date 和 time 类入门详解-永利国际
于2017-04-11 11:05:22 发表在
java知识库
这篇文章主要是java8中新的date和time api的实战。新的date和time类是java开发者社区千呼万唤始出来的。java8 之前存在的date类一直都受人诟病,很多人都会选择使用第三方的date库。java8中的date和time api是jodatime的作者参与开发的,实现了jsr310的全部内容。这些新的api都在包java.time下。
既然第三方的joda-time,date4j都已经足够强大了,为什么java8还要重新实现他呢,一部分的原因是这些第三方的库是存在兼容问题的,比如标准的jsf日期转化器与joda-time api,就不兼容,每次使用都需要编写自己的转换器,所以标准化api是必须的,就有了jsr310,java8中就实现了他全部的规定内容。
新date类和time类背后的设计原则:
不可变类
java8之前,date类都是可变类。当我们在多线程环境下使用它,编程人员应该确认date对象的线程安全。java8的date和time api提供了线程安全的不可变类。编程人员不用考虑并发的问题。
领域模型驱动设计方法
新的日期和时间的类别遵循“域驱动设计”。对于开发者来说,理解方法和类的功能是很容易的。
接下来让我们来看看新date和time api:
java.time.localdate:
localdate只提供日期不提供时间信息。它是不可变类且线程安全的。
package org.smarttechie;
import java.time.localdate;
import java.time.temporal.chronounit;
/**
* this class demonstrates java 8 data and time api
* @author siva prasad rao janapati
* */
public class datetimedemonstration {
/**
* @param args
*/
public static void main(string[] args) {
//create date localdate localdate = localdate.now();
system.out.println("the local date is :: " localdate);
//find the length of the month. that is, how many days are there for this month.
system.out.println("the number of days available for this month:: " localdate.lengthofmonth());
//know the month name
system.out.println("what is the month name? :: " localdate.getmonth().name());
//add 2 days to the today's date.
system.out.println(localdate.plus(2, chronounit.days));
//substract 2 days from today
system.out.println(localdate.minus(2, chronounit.days));
//convert the string to date
system.out.println(localdate.parse("2017-04-07"));
}
}
java.time.localtime:
localtime只提供时间而不提供日期信息,它是不可变类且线程安全的。
package org.smarttechie;
import java.time.localtime;
import java.time.temporal.chronounit;
/**
* this class demonstrates java 8 data and time api
* @author siva prasad rao janapati
* */
public class datetimedemonstration {
/**
* @param args
*/
public static void main(string[] args) {
//get local time
localtime localtime = localtime.now();
system.out.println(localtime);
//get the hour of the day
system.out.println("the hour of the day:: " localtime.gethour());
//add 2 hours to the time.
system.out.println(localtime.plus(2, chronounit.hours));
//add 6 minutes to the time.
system.out.println(localtime.plusminutes(6));
//substract 2 hours from current time
system.out.println(localtime.minus(2, chronounit.hours));
}
}
java.time.localdatetime:
localdatetime提供时间和日期的信息,它是不可变类且线程安全的
package orr.smarttechie;
import java.time.localdatetime;
import java.time.temporal.chronounit;
/**
* this class demonstrates java 8 data and time api
* @author siva prasad rao janapati
*
*/
public class datetimedemonstration {
/**
* @param args
*/
public static void main(string[] args) {
//get localdatetime object
localdatetime localdatetime = localdatetime.now();
system.out.println(localdatetime);
//find the length of month. that is, how many days are there for this month.
system.out.println("the number of days available for this month:: " localdatetime.getmonth().length(true));
//know the month name
system.out.println("what is the month name? :: " localdatetime.getmonth().name());
//add 2 days to today's date.
system.out.println(localdatetime.plus(2, chronounit.days));
//substract 2 days from today
system.out.println(localdatetime.minus(2, chronounit.days));
}
}
java.time.year:
year提供年的信息,它是不可变类且线程安全的。
package orr.smarttechie;
import java.time.year;
import java.time.temporal.chronounit;
/**
* this class demonstrates java 8 data and time api
* @author siva prasad rao janapati
*
*/
public class datetimedemonstration {
/**
* @param args
*/
public static void main(string[] args) {
//get year
year year = year.now();
system.out.println("year ::" year);
//know the year is leap year or not
system.out.println("is year[" year "] leap year?" year.isleap());
}
}
java.time.duration:
duration是用来计算两个给定的日期之间包含多少秒,多少毫秒,它是不可变类且线程安全的
java.time.period:
period是用来计算两个给定的日期之间包含多少天,多少月或者多少年,它是不可变类且线程安全的
package orr.smarttechie;
import java.time.localdate;
import java.time.period;
import java.time.temporal.chronounit;
/**
* this class demonstrates java 8 data and time api
* @author siva prasad rao janapati
*
*/
public class datetimedemonstration {
/**
* @param args
*/
public static void main(string[] args) {
localdate localdate = localdate.now();
period period = period.between(localdate, localdate.plus(2, chronounit.days));
system.out.println(period.getdays());
}
}