java新的日期,Java新的时间日期类

一、LocalDate

LocalDate类使用ISO日历表示年、月、日。

LocalDate类的常用方法:

1、LocalDate.now():获取系统当前日期。

2、LocalDate.of(int year,int month,int dayOfMonth):按照指定日期创建LocalDate对象。

3、getYear():返回日期中的年份。

4、getMonth():返回日期中的月份。

5、getDayOfMonth():返回月份中的日。

案例:用LocalDate获取当前日期。

packagejax.timeanddate;

importjava.time.LocalDate;

publicclassLocalDateTest {

publicstaticvoidmain(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获取当前时间。

packagejax.timeanddate;

importjava.time.LocalTime;

publicclassLocalTimeTest {

publicstaticvoidmain(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获取当前时间。

packagejax.timeanddate;

importjava.time.LocalDateTime;

publicclassLocalDateTimeTest {

publicstaticvoidmain(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类的对象,并显示年月日时分秒。

packagejax.timeanddate;

importjava.time.LocalDateTime;

importjava.time.format.DateTimeFormatter;

publicclassDateTimeFormatterTest {

publicstaticvoidmain(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);按指定模版将日期对象格式化为字符串

案例:将当前日期格式化为字符串,并显示年月日时分秒。

packagejax.timeanddate;

importjava.time.ZonedDateTime;

importjava.time.format.DateTimeFormatter;

publicclassZonedDateTimeTest {

publicstaticvoidmain(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

import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.ZonedDateTime;

import java.time.format.DateTimeFormatter;

import java.util.Date;

public class Test3 {

public static void main(String[] args) {

DateTimeFormatter df=DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss");

Date date=new Date();

Instant instant = date.toInstant();

ZoneId zoneId = ZoneId.systemDefault();

LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

String format = df.format(localDateTime);

System.out.println(format);

ZonedDateTime zdt=ZonedDateTime.now();

LocalDateTime localDateTime2 = zdt.toLocalDateTime();

String format2 = df.format(localDateTime2);

System.out.println(format2);

}

}

输出:

2017-10-12 02:47:18

2017-10-12 02:47:18

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值