Java 8日期时间API教程:LocalDateTime

该博文是Java 8中引入的有关Date Time API的系列教程的一部分。在本博文中,我将介绍LocalDateTime类中可用的一些方法。

LocalDateTime是一个不可更改的线程安全对象,它表示ISO-8601日历系统中没有时区的日期时间,例如2014-03-30T02:51:21。 通常以年-月-日-时-分-秒表示,并且提供高达纳秒范围的精度。

  • 对于月份,有效值范围是1 – 28/31
  • 对于小时,有效值范围是0 – 23。
  • 分钟和秒的有效值范围是0 – 59。
  • 对于毫微秒,有效值范围是0 – 999999999。

可以通过以下几种方法创建LocalDateTime对象。

LocalDateTime dateTime = LocalDateTime.now();//returns the system date time in the default timezone.
System.out.println("dateTime from the system is:"+dateTime);

//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour. The second and nano second are set to 0. 
LocalDateTime ldtm = LocalDateTime.of(2014, 3, 30, 12, 30);
System.out.println("dateTime created by passing in year, month,day, hour, minute is:"+ldtm.toString());

//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour, seconds of the minute. The nano second is set to 0.
LocalDateTime ldts = LocalDateTime.of(2014, 3, 30, 12, 30,23);
System.out.println("dateTime created by passing in year, month,day, hour, minute, seconds is :"+ldts.toString());

//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour, seconds of the minute and nano second of the second.
LocalDateTime ldtns = LocalDateTime.of(2014, 3, 30, 12, 30,23,12000);
System.out.println("dateTime created by passing in year, month,day, hour, minute, seconds, nano seconds is :"+ldtns.toString());

//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour. The second and nano second are set to 0.
LocalDateTime ldtem = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute is : "+ldtem.toString());

//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour, seconds of the minute. The nano second is set to 0.
LocalDateTime ldtes = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30,42);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute,second is : "+ldtes.toString());

//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour, seconds of the minute and nano seconds of the seconds.
LocalDateTime ldtens = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30,42,12000);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute,seconds, ns is : "+ldtens.toString());

有几种方法可以获取字段的值,例如月,年,周几,月,秒,分钟,纳秒。

int dayOfMonth = dateTime.getDayOfMonth();//returns the day of the month.
System.out.println("day Of Month is :"+dayOfMonth);
DayOfWeek dow = dateTime.getDayOfWeek();//returns DayOfWeek enum.
System.out.println("dow:"+dow);
int dayOfYear = dateTime.getDayOfYear();//returns the day of the year.
System.out.println("day Of Year is :"+dayOfYear);
int hour = dateTime.getHour();//returns hour of the day.
System.out.println("hour:"+hour);
int monthValue = dateTime.getMonthValue();//returns the number of the month in the year.
System.out.println("month of the date in number is:"+monthValue);
Month month = dateTime.getMonth();// returns the month enum for the month of the year.
System.out.println("month of the date is :"+month);
int sec = dateTime.getSecond();// returns seconds field value for the date time.
System.out.println("Seconds of the date time is:"+sec);
int nano = dateTime.getNano();();// returns nano seconds field value for the date time.
System.out.println("nano seconds of the date time is:"+nano);
int year = dateTime.getYear();();// returns year field value for the date time.
System.out.println("year of the date is :"+year);

有几种minusXXX方法可以从日期中减去字段的值。

//creates a new date time copy after subtracting 10 days from the date time.
LocalDateTime ldtmd = dateTime.minusDays(10);
 System.out.println("date time after subtracting days is : "+ldtmd.toString());
//creates a new date time copy after subtracting 10 hours from the date time.
LocalDateTime ldtmh = dateTime.minusHours(10);
System.out.println("date time after subtracting hours is : "+ldtmh.toString());
//creates a new date time copy after subtracting 21 minutes from the date time.
LocalDateTime ldtmm = dateTime.minusMinutes(21);
System.out.println("date time after subtracting minutes is : "+ldtmm.toString());
//creates a new date time copy after subtracting 2 months from the date time.
LocalDateTime ldtmmm = dateTime.minusMonths(2);
System.out.println("date time after subtracting months is : "+ldtmmm.toString());
//creates a new date time copy after subtracting 3 years from the date time.
LocalDateTime ldtmy = dateTime.minusYears(3);
System.out.println("date time after subtracting years is : "+ldtmy.toString());
//creates a new date time copy after subtracting 32 weeks from the date time.
LocalDateTime ldtmw = dateTime.minusWeeks(32);
System.out.println("date time after subtracting weeks is : "+ldtmw.toString());
//creates a new date time copy after subtracting 1200 seconds from the date time.
LocalDateTime ldtms = dateTime.minusSeconds(1200);
System.out.println("date time after subtracting secs is : "+ldtms.toString());
//creates a new date time copy after subtracting 12000 nano seconds from the date time.
LocalDateTime ldtmn = dateTime.minusNanos(12000);
System.out.println("date time after subtracting nanos is : "+ldtmn.toString());

类似地,还有plusXXX方法可以将值添加到可以创建LocalDateTime对象的日期时间的字段中。

//creates a new date time copy after adding 10 days to the date time. 
LocalDateTime ldtpd = dateTime.plusDays(10);
System.out.println("date time after adding days is : "+ldtpd.toString());
//creates a new date time copy after adding 100 hours to the date time. 
LocalDateTime ldtph = dateTime.plusHours(100);
System.out.println("date time after adding hours is : "+ldtph.toString());
//creates a new date time copy after adding 190 minutes to the date time. 
LocalDateTime ldtpmm = dateTime.plusMinutes(190);
System.out.println("date time after adding minutes is : "+ldtpmm.toString());
//creates a new date time copy after adding 32 months to the date time. 
LocalDateTime ldtpdm = dateTime.plusMonths(32);
System.out.println("date time after adding months is : "+ldtpdm.toString());
//creates a new date time copy after adding 120000 nano seconds to the date time. 
LocalDateTime ldtpn = dateTime.plusNanos(120000);
System.out.println("date time after adding nanos is : "+ldtpn.toString());
//creates a new date time copy after adding 1200 seconds to the date time. 
LocalDateTime ldtps = dateTime.plusSeconds(1200);
System.out.println("date time after adding seconds is : "+ldtps.toString());
//creates a new date time copy after adding 24 weeks to the date time. 
LocalDateTime ldtpw = dateTime.plusWeeks(24);
System.out.println("date time after adding weeks is : "+ldtpw.toString());
//creates a new date time copy after adding 3 years to the date time. 
LocalDateTime ldtpy = dateTime.plusYears(3);
System.out.println("date time after adding years is : "+ldtpy.toString());

有几种withXXX方法可以将值设置为特定字段。

LocalDateTime ldtmddm = dateTime.withDayOfMonth(10); // sets the day of the month to 10.
System.out.println("date time after adding modifying day of the month is : "+ldtmddm.toString());
LocalDateTime ldtmddy = dateTime.withDayOfYear(12);//sets the day of year to 12.
System.out.println("date time after adding modifying day of the year is : "+ldtmddy.toString());
LocalDateTime ldtmdh = dateTime.withHour(12);//sets the hour of the day to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying hour is : "+ldtmdh.toString());
LocalDateTime ldtmdmm = dateTime.withMinute(12);//sets the minute of the hour to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying minutes is : "+ldtmdmm.toString());
LocalDateTime ldtmdm = dateTime.withMonth(4);//sets the month to 4. The other date time fields are not modified.
System.out.println("date time after adding modifying month is : "+ldtmdm.toString());
LocalDateTime ldtmdy = dateTime.withYear(2011);//sets the year to 2012. The other date time fields are not modified.
System.out.println("date time after adding modifying year  is : "+ldtmdy.toString());
LocalDateTime ldtmds = dateTime.withSecond(12);//sets the seconds of the minute to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying seconds is : "+ldtmds.toString());
LocalDateTime ldtmdn = dateTime.withNano(120000);//sets the nano seconds of seconds to 120000. The other date time fields are not modified.
System.out.println("date time after adding modifying nanos is : "+ldtmdn.toString());

如果只想从该日期时间中检索日期或时间,则可以使用以下方法。

LocalDate ld = dateTime.toLocalDate();//gets the date value(LocalDate) from the date time. A LocalDate with same year, month and day as this LocalDateTime object will be returned.
System.out.println("The date field from the date time object is : "+ld.toString());
LocalTime lt = dateTime.toLocalTime();// gets the time value(LocalTime) from the date time. returns a LocalTime with the same hour, minute, second and nanosecond as this date-time.
System.out.println("The time field from the date time object is : "+lt.toString());

结论

LocalDateTime有一些操作日期和时间的选项,下面对其中一些进行了描述。

翻译自: https://www.javacodegeeks.com/2014/04/java-8-date-time-api-tutorial-localdatetime.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值