-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDateUtil.java
More file actions
31 lines (25 loc) · 949 Bytes
/
DateUtil.java
File metadata and controls
31 lines (25 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package ru.javawebinar.basejava.util;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
/**
* gkislin
* 20.07.2016
*/
public class DateUtil {
public static final LocalDate NOW = LocalDate.of(3000, 1, 1);
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("MM/yyyy");
public static LocalDate of(int year, Month month) {
return LocalDate.of(year, month, 1);
}
public static String format(LocalDate date) {
if (date == null) return "";
return date.equals(NOW) ? "Сейчас" : date.format(DATE_FORMATTER);
}
public static LocalDate parse(String date) {
if (HtmlUtil.isEmpty(date) || "Сейчас".equals(date)) return NOW;
YearMonth yearMonth = YearMonth.parse(date, DATE_FORMATTER);
return LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), 1);
}
}