Статьи

Учебное пособие по API даты и времени в Java 8: LocalDateTime

Этот пост является частью серии руководств по API Date Time, представленной в Java 8. В этом посте я расскажу о некоторых методах, доступных в классе LocalDateTime.

LocalDateTime — это неизменяемый потокобезопасный объект, который представляет дату-время без часового пояса в календарной системе ISO-8601, например 2014-03-30T02: 51: 21. Обычно он представлен в виде год-месяц-день-час-минута-секунда и обеспечивает точность до диапазона наносекунд.

  • Для месяца допустимый диапазон значений: 1 — 28/31
  • Для часов допустимый диапазон значений составляет 0 — 23.
  • Допустимые значения минут и секунд находятся в диапазоне 0 — 59.
  • Для нано секунд допустимый диапазон значений составляет 0 — 999999999.

Объект LocalDateTime может быть создан несколькими способами, перечисленными ниже.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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());

Есть несколько способов получить значения полей, такие как месяц, год, день недели, день месяца, секунды, минуты, нано секунды.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
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, которые могут вычесть значения для полей из даты.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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, которые могут установить значение для определенного поля.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
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());

Если вы хотите извлечь только дату или время из этой даты, то следующие методы могут сделать это.

1
2
3
4
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 имеет несколько опций для манипулирования датой и временем, некоторые из которых описаны ниже.

Ссылка: Учебное пособие по API Java 8 Date Time: LocalDateTime от нашего партнера по JCG Венкаты Киран из блога Coding square .