关于编码中的一些时区问题。
今天在工作中有涉及到。
web应用在不同的时区访问,用户肯定希望以自己的时区来显示时间。但是有时候后台的数据不是可能不是以用户当地时间来运行的。
如何使时间自动匹配用户的时间呢。
我使用中的是xslt来处理,当然java肯定能处理的。
测试数据:
<ns:SRCancelInput xmlns:ns="http://www.avaya.com/ebond/1/0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ebond="http://www.avaya.com/ebond/1/0" xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<!--1 or more repetitions:-->
<ServiceRequest>
<RequesterID>string</RequesterID>
<ProviderID>string</ProviderID>
<ReferringCaseID>string</ReferringCaseID>
<CurrentState>Unassinged</CurrentState>
<RequesterSeverity>1-Critical</RequesterSeverity>
<ProviderPriority>2-High</ProviderPriority>
<ResponseTime>2014-09-18T17:18:33</ResponseTime>
<WorkFlowStatus>string</WorkFlowStatus>
<Comment>string</Comment>
<Entitled>3</Entitled>
<Acknowledge>error</Acknowledge>
<LastTransaction>string</LastTransaction>
<TransactionId>string</TransactionId>
<ServiceRequester>
<Organization>
<OrganizationName>string</OrganizationName>
<Business>string</Business>
</Organization>
<Person>
<FirstName>string</FirstName>
<LastName>string</LastName>
<WorkPhone>string</WorkPhone>
<Email>string</Email>
<Salutation>string</Salutation>
<Title>string</Title>
<ContactType>string</ContactType>
<CommunicationMode>Fax</CommunicationMode>
<CommunicationMethod>string</CommunicationMethod>
</Person>
<Address>
<AddressType>3</AddressType>
<GeoAddress1>string</GeoAddress1>
<GeoAddress2>string</GeoAddress2>
<GeoAddress3>string</GeoAddress3>
<GeoAddress4>string</GeoAddress4>
<City>string</City>
<Region>string</Region>
<PostalCode>string</PostalCode>
<TimeZone>string</TimeZone>
<Country>string</Country>
</Address>
<Location>
<LocationType>3</LocationType>
<LocationID>string</LocationID>
</Location>
</ServiceRequester>
<ServiceProvider>
<Organization>
<OrganizationName>string</OrganizationName>
<Business>string</Business>
</Organization>
<Person>
<FirstName>string</FirstName>
<LastName>string</LastName>
<WorkPhone>string</WorkPhone>
<Email>string</Email>
<Salutation>string</Salutation>
<Title>string</Title>
<ContactType>string</ContactType>
<CommunicationMode>Wireless</CommunicationMode>
<CommunicationMethod>string</CommunicationMethod>
</Person>
<Address>
<AddressType>3</AddressType>
<GeoAddress1>string</GeoAddress1>
<GeoAddress2>string</GeoAddress2>
<GeoAddress3>string</GeoAddress3>
<GeoAddress4>string</GeoAddress4>
<City>string</City>
<Region>string</Region>
<PostalCode>string</PostalCode>
<TimeZone>string</TimeZone>
<Country>string</Country>
</Address>
<Location>
<LocationType>3</LocationType>
<LocationID>string</LocationID>
</Location>
</ServiceProvider>
<Problem>
<Likelihood>3</Likelihood>
<Impact>3</Impact>
<Category>
<CategoryType>Installation</CategoryType>
<CategoryText>string</CategoryText>
</Category>
<Statement>
<StatementText>string</StatementText>
</Statement>
<Feature>
<Description>string</Description>
<ValidValues>string</ValidValues>
<DataType>3</DataType>
<FeatureValue>string</FeatureValue>
</Feature>
<Product>
<Vendor>string</Vendor>
<ProductName>string</ProductName>
<Version>string</Version>
<Asset>
<AssetID>string</AssetID>
<AssetName>string</AssetName>
</Asset>
</Product>
</Problem>
<Agreement>
<ContractID>string</ContractID>
<AgreementType>string</AgreementType>
</Agreement>
<Activity>
<ActivityType>SR Note</ActivityType>
<LocalDate>2009-09-03T18:00:00Z</LocalDate>
<Status>Done</Status>
<DueDate>2012-09-13T07:00:34-06:00</DueDate>
<DeliveryService>string</DeliveryService>
<DeliveryTrackingNumber>string</DeliveryTrackingNumber>
<ActionLog>string</ActionLog>
<Asset>
<AssetID>string</AssetID>
<AssetName>string</AssetName>
</Asset>
<Person>
<FirstName>string</FirstName>
<LastName>string</LastName>
<WorkPhone>string</WorkPhone>
<Email>string</Email>
<Salutation>string</Salutation>
<Title>string</Title>
<ContactType>string</ContactType>
<CommunicationMode>Fax</CommunicationMode>
<CommunicationMethod>string</CommunicationMethod>
</Person>
<Resource>
<ResourceType>string</ResourceType>
<Quantity>3</Quantity>
<UnitofMeasure>string</UnitofMeasure>
</Resource>
</Activity>
<CustomerTicketNumber>string</CustomerTicketNumber>
<DispatchTicketNumber>string</DispatchTicketNumber>
</ServiceRequest>
</ns:SRCancelInput>
测试的文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="java SOAP-ENC xsi xsd SOAP-ENV ebond" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ebond="http://www.avaya.com/ebond/1/0" xmlns:java="http://xml.apache.org/xslt/java">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="*">
</xsl:apply-templates>
</xsl:template>
<!--Open template-->
<xsl:template match="ebond:SRCreateInput">
<xsl:element name="Open">
<xsl:call-template name="TranInfo"/>
<xsl:call-template name="TradPartInfo"/>
<xsl:call-template name="RequestInfo"/>
</xsl:element>
</xsl:template>
<!--Update template-->
<xsl:template match="ebond:SRUpdateInput">
<xsl:element name="Update">
<xsl:call-template name="TranInfo"/>
<xsl:call-template name="TradPartInfo"/>
<xsl:call-template name="RequestInfo"/>
</xsl:element>
</xsl:template>
<!--Update template-->
<xsl:template match="ebond:SRCancelInput">
<xsl:element name="Close">
<xsl:call-template name="TranInfo"/>
<xsl:call-template name="TradPartInfo"/>
<xsl:call-template name="RequestInfo"/>
</xsl:element>
</xsl:template>
<!--Update template-->
<xsl:template match="ebond:SRCloseInput">
<xsl:element name="Close">
<xsl:call-template name="TranInfo"/>
<xsl:call-template name="TradPartInfo"/>
<xsl:call-template name="RequestInfo"/>
</xsl:element>
</xsl:template>
<!--TranInfo template-->
<xsl:template name="TranInfo">
<xsl:element name="TranInfo">
<xsl:if test="ServiceRequest/B2BTranId">
<xsl:element name="B2BTranId">com.ge.gis.kintana:<xsl:value-of select="ServiceRequest/B2BTranId"/></xsl:element>
</xsl:if>
<xsl:element name="ICN"/>
<xsl:choose>
<xsl:when test="../ebond:SRCreateInput">
<xsl:element name="TranName">Open</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRUpdateInput">
<xsl:element name="TranName">NotesUpdate</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCancelInput">
<xsl:element name="TranName">Cancel</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCloseInput">
<xsl:element name="TranName">Close</xsl:element>
</xsl:when>
</xsl:choose>
<xsl:element name="RequestType">incident</xsl:element>
<xsl:element name="ServiceType">voice service</xsl:element>
</xsl:element>
</xsl:template>
<!--TradPartInfo template-->
<xsl:template name="TradPartInfo">
<xsl:element name="TradPartInfo">
<xsl:element name="From">
<xsl:element name="Name">com.ebond.avaya</xsl:element>
<xsl:if test="ServiceRequest/ProviderID">
<xsl:element name="RequestNum">
<xsl:value-of select="ServiceRequest/ProviderID"/>
</xsl:element>
</xsl:if>
</xsl:element>
<xsl:element name="To">
<xsl:element name="Name">com.ge.gis.kintana</xsl:element>
<xsl:if test="ServiceRequest/RequesterID">
<xsl:element name="RequestNum">
<xsl:value-of select="ServiceRequest/RequesterID"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
<!--RequestInfo template-->
<xsl:template name="RequestInfo">
<xsl:element name="RequestInfo">
<xsl:element name="ReqStatus">
<xsl:choose>
<xsl:when test="../ebond:SRCloseInput">Closed</xsl:when>
<xsl:when test="../ebond:SRCancelInput">Canceled</xsl:when>
<xsl:otherwise><xsl:value-of select="ServiceRequest/CurrentState"/></xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:if test="ServiceRequest/RequesterSeverity">
<xsl:element name="Severity">
<xsl:value-of select="ServiceRequest/RequesterSeverity"/>
</xsl:element>
</xsl:if>
<xsl:call-template name="ClientContactDetails"/>
<xsl:if test="ServiceRequest/Problem/Category/CategoryType">
<xsl:element name="IssueCategory">
<xsl:value-of select="ServiceRequest/Problem/Category/CategoryType"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Statement/StatementText">
<xsl:element name="Desc">
<xsl:value-of select="ServiceRequest/Problem/Statement/StatementText"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/ProductName">
<xsl:element name="AssetType">
<xsl:value-of select="ServiceRequest/Problem/Product/ProductName"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/Version">
<xsl:element name="ProductVersion">
<xsl:value-of select="ServiceRequest/Problem/Product/Version"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/Asset/AssetID">
<xsl:element name="AssetId">
<xsl:value-of select="ServiceRequest/Problem/Product/Asset/AssetID"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/Asset/AssetName">
<xsl:element name="AssetName">
<xsl:value-of select="ServiceRequest/Problem/Product/Asset/AssetName"/>
</xsl:element>
</xsl:if>
<xsl:choose>
<xsl:when test="ServiceRequest/Activity/LocalDate">
<xsl:element name="UpdateDateTime">
<xsl:call-template name="convert-EBondToGECharstandardtime">
<xsl:with-param name="originalDateTime">
<xsl:value-of select="ServiceRequest/Activity/LocalDate"/>
</xsl:with-param>
</xsl:call-template>
</xsl:element>
<xsl:choose>
<xsl:when test="../ebond:SRCreateInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="convert-EBondToGECharstandardtime">
<xsl:with-param name="originalDateTime">
<xsl:value-of select="ServiceRequest/Activity/LocalDate"/>
</xsl:with-param>
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCloseInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="convert-EBondToGECharstandardtime">
<xsl:with-param name="originalDateTime">
<xsl:value-of select="ServiceRequest/Activity/LocalDate"/>
</xsl:with-param>
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCancelInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="convert-EBondToGECharstandardtime">
<xsl:with-param name="originalDateTime">
<xsl:value-of select="ServiceRequest/Activity/LocalDate"/>
</xsl:with-param>
</xsl:call-template>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:element name="UpdateDateTime">
<xsl:call-template name="ConvertDatetime"/>
</xsl:element>
<xsl:choose>
<xsl:when test="../ebond:SRCreateInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="ConvertDatetime"/>
</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCancelInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="ConvertDatetime"/>
</xsl:element>
</xsl:when>
<xsl:when test="../ebond:SRCloseInput">
<xsl:element name="RestoreDateTime">
<xsl:call-template name="ConvertDatetime"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
<xsl:element name="Notes">
<xsl:if test="ServiceRequest/RequesterSeverity">
Severity:<xsl:value-of select="ServiceRequest/RequesterSeverity"/>
</xsl:if>
<xsl:if test="ServiceRequest/ProviderPriority">
Priority:<xsl:value-of select="ServiceRequest/ProviderPriority"/>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/ProductName">
ProductDesc:<xsl:value-of select="ServiceRequest/Problem/Product/ProductName"/>
</xsl:if>
<xsl:if test="ServiceRequest/Problem/Product/Asset/AssetID">
AssetId:<xsl:value-of select="ServiceRequest/Problem/Product/Asset/AssetID"/>
</xsl:if>
<xsl:if test="ServiceRequest/CurrentState">
ReqStatus:<xsl:value-of select="ServiceRequest/CurrentState"/>
</xsl:if>
<xsl:if test="ServiceRequest/ProviderID">
RequestNum:<xsl:value-of select="ServiceRequest/ProviderID"/>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/FirstName">
FirstName:<xsl:value-of select="ServiceRequest/ServiceRequester/Person/FirstName"/>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/LastName">
LastName:<xsl:value-of select="ServiceRequest/ServiceRequester/Person/LastName"/>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/Email">
Email:<xsl:value-of select="ServiceRequest/ServiceRequester/Person/Email"/>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/WorkPhone">
Mobile:<xsl:value-of select="ServiceRequest/ServiceRequester/Person/WorkPhone"/>
</xsl:if>
<xsl:if test="ServiceRequest/Activity/ActionLog">
SRNote:<xsl:value-of select="ServiceRequest/Activity/ActionLog"/>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
<!--ClientContactDetails template-->
<xsl:template name="ClientContactDetails">
<xsl:element name="ClientContactDetails">
<xsl:if test="ServiceRequest/ServiceRequester/Organization/Business">
<xsl:element name="Business">
<xsl:value-of select="ServiceRequest/ServiceRequester/Organization/Business"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/FirstName">
<xsl:element name="FirstName">
<xsl:value-of select="ServiceRequest/ServiceRequester/Person/FirstName"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/LastName">
<xsl:element name="LastName">
<xsl:value-of select="ServiceRequest/ServiceRequester/Person/LastName"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/Email">
<xsl:element name="Email">
<xsl:value-of select="ServiceRequest/ServiceRequester/Person/Email"/>
</xsl:element>
</xsl:if>
<xsl:if test="ServiceRequest/ServiceRequester/Person/WorkPhone">
<xsl:element name="Mobile">
<xsl:value-of select="ServiceRequest/ServiceRequester/Person/WorkPhone"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template name="ConvertDatetime">
<xsl:variable name="getLocalTime" select="java:format(java:java.text.SimpleDateFormat.new('MMM dd, yyyy hh:mm:ss a z'), java:java.util.Date.new())"/>
<xsl:value-of select="translate($getLocalTime,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
</xsl:template>
<xsl:template name="convert-EBondToGECharstandardtime" >
<xsl:param name="originalDateTime"/>
<xsl:variable name="removeT" select="translate($originalDateTime,'T', ' ')"/>
<xsl:variable name="removeZ" select="concat(translate($removeT,'Z', ' '),'UTC')"/>
<xsl:variable name="from" select="java:parse(java:java.text.SimpleDateFormat.new('yyyy-MM-dd HH:mm:ss z'),$removeZ)"/>
<xsl:variable name="format" select="java:java.text.SimpleDateFormat.new('MMM dd, yyyy hh:mm:ss a z')"/>
<xsl:variable name="ESTTimeZone" select="java:java.util.TimeZone.getTimeZone('GMT-5')"/>
<xsl:variable name="setTimeZone" select="java:setTimeZone($format,$ESTTimeZone)"/>
<xsl:variable name="to" select="java:format($format,$from)"/>
<xsl:value-of select="concat(substring($to,0,25),' EST')"/>
</xsl:template>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
</xsl:stylesheet>
其中最主要的是
<xsl:template name="convert-EBondToGECharstandardtime" >
<xsl:param name="originalDateTime"/>
<xsl:variable name="removeT" select="translate($originalDateTime,'T', ' ')"/>
<xsl:variable name="removeZ" select="concat(translate($removeT,'Z', ' '),'UTC')"/>
<xsl:variable name="from" select="java:parse(java:java.text.SimpleDateFormat.new('yyyy-MM-dd HH:mm:ss z'),$removeZ)"/>
<xsl:variable name="format" select="java:java.text.SimpleDateFormat.new('MMM dd, yyyy hh:mm:ss a z')"/>
<xsl:variable name="ESTTimeZone" select="java:java.util.TimeZone.getTimeZone('GMT-5')"/>
<xsl:variable name="setTimeZone" select="java:setTimeZone($format,$ESTTimeZone)"/>
<xsl:variable name="to" select="java:format($format,$from)"/>
<xsl:value-of select="concat(substring($to,0,25),' EST')"/>
</xsl:template>
函数。
这个函数先把输入的时间,转化成0时区的时间(UTC),因为输入的时间本来就是0时区时间。如果你的不是0时区时间,要写自己时区的代表字符。然后设置一个需要显示的时间 时区格式(GMT-5)。
然后去格式化(java:format($format,$from)).
主要是表达一个思想。
java中的肯定有这个函数来处理的。