一、LocalDate
LocalDate类使用ISO日历表示年、月、日。
LocalDate类的常用方法:
1、LocalDate.now():获取系统当前日期。
2、LocalDate.of(int year,int month,int dayOfMonth):按照指定日期创建LocalDate对象。
3、getYear():返回日期中的年份。
4、getMonth():返回日期中的月份。
5、getDayOfMonth():返回月份中的日。
案例:用LocalDate获取当前日期。
package jax.timeanddate;
import java.time.LocalDate;
public class LocalDateTest {
public static void main(String[] args) {
LocalDate date=LocalDate.now();
System.out.print(date.getYear()+" 年 ");
System.out.print(date.getMonthValue()+" 月 ");
System.out.println(date.getDayOfMonth()+" 日 ");
System.out.println(date.toString());
}
}
输出:
2016 年 5 月 25 日
2016-05-25
二、LocalTime
LocalTime类用于表示一天中的时间。
LocalTime类的常用方法:
1、LocalTime.now():获取系统当前时间。
2、LocalTime.of(int hour,int minute,int sec):按照指定时间创建LocalTime对象。
3、getHour():返回小时。
4、getMinute():返回分钟。
5、getSecond():返回秒。
案例:用LocalTime获取当前时间。package jax.timeanddate;
import java.time.LocalTime;
public class LocalTimeTest {
public static void main(String[] args) {
LocalTime time=LocalTime.now();
System.out.print(time.getHour()+" 时 ");
System.out.print(time.getMinute()+" 分 ");
System.out.println(time.getSecond()+" 秒 ");
System.out.println(time.toString());
}
}
输出:
15 时 43 分 3 秒
15:43:03.432
三、LocalDateTime
LocalDateTime类用于表示日期和时间。
LocalDateTime类的常用方法:
1、LocalDateTime.now():获取系统当前日期和时间。
2、LocalDateTime.of(int year,int month,int dayOfMonth,int hour,int minute,int sec):按照指定时间创建LocalDateTime对象。
3、getYear():返回日期中的年份。
4、getMonth():返回日期中的月份。
5、getDayOfMonth():返回月份中的日。
6、getHour():返回小时。
7、getMinute():返回分钟。
8、getSecond():返回秒。
案例:用LocalDateTime获取当前时间。package jax.timeanddate;
import java.time.LocalDateTime;
public class LocalDateTimeTest {
public static void main(String[] args) {
LocalDateTime time=LocalDateTime.now();
System.out.print(time.getYear()+" 年 ");
System.out.print(time.getMonthValue()+" 月 ");
System.out.print(time.getDayOfMonth()+" 日 ");
System.out.print(time.getHour()+" 时 ");
System.out.print(time.getMinute()+" 分 ");
System.out.println(time.getSecond()+" 秒 ");
System.out.println(time.toString());
}
}
输出:
2016 年 5 月 25 日 15 时 48 分 12 秒
2016-05-25T15:48:12.526
DateTimeFormatter类用于将字符串解析为日期
常用方法:
1、static ofPattern(String pattern):作用:按照pattern字符串指定的格式创建DateTimeFormmter对象
2、LocalDateTime.parse(strDate,formatter);
案例:将字符串“2016-05-25 15:53:55”格式化为一个LocalDateTime类的对象,并显示年月日时分秒。
package jax.timeanddate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterTest {
public static void main(String[] args) {
DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time=LocalDateTime.parse("2016-05-25 15:53:55",df);
System.out.print(time.getYear()+" 年 ");
System.out.print(time.getMonthValue()+" 月 ");
System.out.print(time.getDayOfMonth()+" 日 ");
System.out.print(time.getHour()+" 时 ");
System.out.print(time.getMinute()+" 分 ");
System.out.println(time.getSecond()+" 秒 ");
System.out.println(time.toString());
}
}
五、ZonedDateTime
ZonedDateTime处理日期和时间与相应的时区
1、ZonedDateTime.now():获取系统当前日期和时间。
2、String format(DateTimeFormatter formatter);按指定模版将日期对象格式化为字符串
案例:将当前日期格式化为字符串,并显示年月日时分秒。package jax.timeanddate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeTest {
public static void main(String[] args) {
ZonedDateTime time=ZonedDateTime.now();
DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(time.format(df));
}
}
输出:
2016/05/25 16:02:22
更多内容:
http://www.androidchina.net/5934.html