java-time

How to format a date by any locale obtained from language tag with java time?

谁都会走 提交于 2020-12-15 06:27:07
问题 I want to get a date as string and a language tag and based on that to parse the string by the locale format. This is what I've done so far: public static String formatDateByLocale(String date, String languageTag) { Locale locale = Locale.forLanguageTag(languageTag); String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.LONG, FormatStyle.LONG, Chronology.ofLocale(locale), locale); DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern(datePattern)

OffsetDateTime - print offset instead of Z

三世轮回 提交于 2020-12-10 04:08:52
问题 I have this code: String date = "2019-04-22T00:00:00+02:00"; OffsetDateTime odt = OffsetDateTime .parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME) .withOffsetSameInstant(ZoneOffset.of("+00:00")); System.out.println(odt); This print: 2019-04-21T22:00Z How can I print 2019-04-21T22:00+00:00 ? With offset instead of Z . 回答1: None of the static DateTimeFormatter s do this in the standard library. They either default to Z or GMT . To achieve +00:00 for no offset, you will have to build your own

OffsetDateTime - print offset instead of Z

南笙酒味 提交于 2020-12-10 04:07:58
问题 I have this code: String date = "2019-04-22T00:00:00+02:00"; OffsetDateTime odt = OffsetDateTime .parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME) .withOffsetSameInstant(ZoneOffset.of("+00:00")); System.out.println(odt); This print: 2019-04-21T22:00Z How can I print 2019-04-21T22:00+00:00 ? With offset instead of Z . 回答1: None of the static DateTimeFormatter s do this in the standard library. They either default to Z or GMT . To achieve +00:00 for no offset, you will have to build your own