Spring + Thymeleaf: Time in a Custom Time Zone

How can I print the date and time indicated by the time zone with Thymeleaf? Sort of:

<span th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm', 'PST')}">2010-01-01 16:30</span>
+4
source share
5 answers

As I was puzzled by this question, I was widely looking for possible solutions.

Here are my conclusions: I did not find any pure function to change the time zone and display it, as in jsp:

<fmt:timeZone value="US">
<fmt:formatDate value="${today}"  type="both" />
</fmt:timeZone>

A possible solution that will work is to create an instance of the calendar using createForTimeZone and format it, since it returns the raw value of the calendar, so from this:

#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone)

you will get something like this:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=14,WEEK_OF_MONTH=1,DAY_OF_MONTH=24,DAY_OF_YEAR=91,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=6,HOUR_OF_DAY=7,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]

( ), .

, , .format , , . ${#calendars.format(#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone), 'dd-MMM-yyyy HH:mm')}

"zzz" . , , , , .

, :

${#dates.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'PST'), 'yyyy-MMM-dd HH:mm')}

${#calendars.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'CET'), 'yyyy-MMM-dd HH:mm')}

.

PST CET:

2014-Feb-24 16:00
2014-Feb-24 07:00

2014-Mar-01 03:00
2014-Feb-28 18:00

,

+4

:

${T(xx.xxx.utils.DateUtils).format(myDate, 'yyyy-MM-dd HH:mm', 'CET')}

  public static String format(Date date, String pattern, String timeZone) {
    TimeZone tz = TimeZone.getTimeZone(timeZone);
    return DateFormatUtils.format(date, pattern, tz);
  }

lang3 ( GAE - sun.util.calendar):

<div th:with="timeZone=${T(java.util.TimeZone).getTimeZone('CET')}">
   <span th:text="${T(org.apache.commons.lang3.time.DateFormatUtils).format(myDate, 'yyyy-MM-dd HH:mm', timeZone)}"></span>
</div>
+3

, LocalDateTime . , LocalDateTime , .

ZonedDateTime, . LocalDateTime#atZone, local, .

, DateTimeFormatter local date time, zoned date time. , .

0

. , , . . , JavaScript, .

0

Thymeleaf #temporals new Temporals(locale, zoneId).

Temporals temporals = new Temporals(LocaleContextHolder.getLocale(), zone) (-, - zone, , ) (, "temporalsZone").

Then use it in the user interface: th:text="${temporalsZone.format(value, pattern, #locale)}">and enjoy full support #temporals.

0
source

Source: https://habr.com/ru/post/1534114/


All Articles