Format Instant to String
Próbuję sformatować Instant na String przy użyciu nowego Java 8 time-api i wzorca:
Instant instant = ...;
String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant);
Używając powyższego kodu dostaję wyjątek, który narzeka na nieobsługiwane pole:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
...
4 answers
Strefa Czasowa
Aby sformatować Instant
wymagana jest strefa czasowa . Bez strefy czasowej formatator nie wie, jak przekształcić pola instant na ludzkie pola daty i dlatego rzuca wyjątek.
Strefa czasowa może być dodana bezpośrednio do formatera za pomocą withZone()
.
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
.withLocale( Locale.UK )
.withZone( ZoneId.systemDefault() );
Generowanie Ciągu
Teraz użyj tego formatera, aby wygenerować reprezentację ciągu Instant.
Instant instant = Instant.now();
String output = formatter.format( instant );
Dump to konsola.
System.out.println("formatter: " + formatter + " with zone: " + formatter.getZone() + " and Locale: " + formatter.getLocale() );
System.out.println("instant: " + instant );
System.out.println("output: " + output );
Kiedy biegniesz.
formatter: Localized(SHORT,SHORT) with zone: US/Pacific and Locale: en_GB
instant: 2015-06-02T21:34:33.616Z
output: 02/06/15 14:34
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-02 21:43:49
Klasa Instant
nie zawiera informacji o strefie, przechowuje jedynie znacznik czasu w milisekundach z epoki Uniksa, tj. 1 Jan 1070 od UTC.
Tak więc, formatter nie może wydrukować daty, ponieważ Data zawsze jest drukowana dla konkretnej strefy czasowej.
Powinieneś ustawić strefę czasową na formatter i wszystko będzie dobrze, jak to:
Instant instant = Instant.ofEpochMilli(92554380000L);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.UK).withZone(ZoneOffset.UTC);
assert formatter.format(instant).equals("07/12/72 05:33");
assert instant.toString().equals("1972-12-07T05:33:00Z");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 16:27:38
public static void main(String[] args) {
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());
System.out.println(DATE_TIME_FORMATTER.format(new Date().toInstant()));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-03 10:50:11
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);
Wierzę, że to może pomóc, może trzeba użyć jakiegoś wariantu localdate zamiast instant