JSTL and time zones

I have some questions regarding time zones. We save all our date in UTC, but we need to show some of them in local (Eastern US time) and UTC at the same time.

Here is my test, I have a date in UTC and you want to display it in UTC and local time:

<html>
<!-- let assume this date is in UTC, I get it from Database in my code -->
<jsp:useBean id="dateValue" class="java.util.Date" />

GMT
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="GMT"/>
</html>
<!-- Displays the original time +4  - not what I need-->

No time zone
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z"/> 
<!-- Displays the original time, but timezone is EDT -->

US/Eastern
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="US/Eastern"/> 
<!-- Displays the original time, timezone is EDT, I need original + 4 -->

</html>

Repeat: I have UTC time from the database and you want to format it and show it in the UTC time zone. The server is running in a different time zone than UTC.

Basically, I need a function like

convertToTimezone(date, originalTimeZone, desiredTimeZone). 

What fmt: formatDate provides is something like

convertToTimezone(date, serverTimeZone, desiredTimeZone). 

I could hack it, but this usually causes problems while saving daylight, etc.

P.S. , - - UTC, . , - UTC, , , , UTC. , .

+3
6

. . , , new Date() GMT, .. 19:21 GMT. . , . fmt:setTimeZone , new Date(). fmt:setTimeZone , fmt:formatDate. , GMT.

fmt:setTimeZone, , () 4 . -Duser.timezone=UTC ( ), , . new Date().

, , UTC, . . UTC. - , , -Duser.timezone=UTC VM. UTC , . . DST Timezone.

+12

Java , GMT + 4, - GMT + 4.

+2

Try:

<fmt:setTimeZone value="Europe/London" scope="session"/>
+2

(UTC/Zulu) , "GMT + 1". . .

, UTC, tomcat:

-Duser.timezone = "UTC"

/* Java */      

@RequestMapping(value = "/web", method = { RequestMethod.POST, RequestMethod.GET })
public String web(Model model, HttpSession session, Locale locale) {

    Date today = new Date();
    model.addAttribute("currentTime", today);
    model.addAttribute("timezone", "GMT+1");

    return "web";
}

, ()

/* JSP web */

<fmt:timeZone value="${timezone}">
<spring:message code="date_format_dateMin" var="pattern"/>
<fmt:formatDate value="${currentTime}" timeZone="${timezone}" pattern="${pattern}" var="searchFormated" />
<span class="innerLabel">${searchFormated}</span>   
</fmt:timeZone>

/* Properties */

date_format_dateMin=yyyy/MM/dd HH:mm
date_format=yyyy/MM/dd HH:mm:ss    
date_format2=yyyy/MM/dd
date_format3_js=yy/mm/dd
date_format4_time=HH:mm
date_format4=dd/MM/yyyy HH:mm:ss
+2

UTC, :

 public Date getUTCDateFromString(String fecha, Locale yourLocale) {
    SimpleDateFormat simpleDateFormat = new 
    SimpleDateFormat("yyyy-MM-dd HH:mm:ss", yourLocale);
    TimeZone utcZone = TimeZone.getTimeZone("UTC");
    simpleDateFormat.setTimeZone(utcZone);
    Date date = null;
    try {
        date = simpleDateFormat.parse(fecha);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
    }
+2
<c:when test="${currentField eq 'dateline'}">
  <c:set target="${storyContent}" property="publishedDate">
  <fmt:formatDate value="${article.publishedDateAsDate}" timeZone="Asia/Dacca"       pattern="${storyContent.dateFormat}"/>
  </c:set>

3 .

:

<fmt:formatDate value="${article.publishedDateAsDate}" pattern="${storyContent.dateFormat}"/>

UTC + 00 hrs.

, . -

TimeZone = "/"

Java http://joda-time.sourceforge.net/timezones.html

0

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


All Articles