3、新的时间日期类

一、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
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为日期;日期家月份,结果为日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为日期;日期减月份,结果为日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于的日期时间,日期时间减天数或等于的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值