function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KeronKeron 

CreatedDateを日本時間で表示させたい

以下のコードで、Visualforceページ上で、

日本時間を表示させたいのですが、どのようにすれば良いか悩んでおります。

ご教授頂きたく。

 

<apex:pageBlock title="結果一覧">

  <apex:pageBlockTable value="{!ResultList}" var="item">

    <apex:column value="{!item.FullName__c}"/>

    <apex:column value="{!item.Level__c}"/>

    <apex:column value="{!item.Num__c}"/>

    <apex:column value="{!item.Point__c}"/>

    <apex:column value="{!item.Percent__c}"/>

    <apex:column value="{!item.Result__c}"/>

    <apex:column value="{!item.CreatedDate}"/> → ここでDBから取得したCreatedDataを日本時間で表示させたい。 

  </apex:pageBlockTable>

</apex:pageBlock>

Best Answer chosen by Admin (Salesforce Developers) 
minoawminoaw

ページ側で変換する方法は分からなかったのでApex側で変換してみました。

Datetime#format メソッドの第二引数に日本標準時を指定して変換しています。

 

 

public class ListPage {
    
    public Item[] items { get; private set; }
    
    public ListPage() {
        items = new Item[0];
        for (Account a : [select Name, CreatedDate from Account limit 3]) {
            items.add(new Item(a));
        }
    }
    
    class Item {
        public Account account { get; private set; }
        public Item(Account account) {
            this.account = account;
        }
        public String getCreatedDateJST() {
            return account.CreatedDate.format('yyyy/MM/dd HH:mm', 'JST');
        }
    }
}

 

<apex:page controller="ListPage">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!items}" var="i">
            <apex:column value="{!i.account.Name}"/>
            <apex:column value="{!i.createdDateJST}" headerValue="{!$ObjectType.Account.fields.CreatedDate.label} (JST)"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

All Answers

minoawminoaw

ページ側で変換する方法は分からなかったのでApex側で変換してみました。

Datetime#format メソッドの第二引数に日本標準時を指定して変換しています。

 

 

public class ListPage {
    
    public Item[] items { get; private set; }
    
    public ListPage() {
        items = new Item[0];
        for (Account a : [select Name, CreatedDate from Account limit 3]) {
            items.add(new Item(a));
        }
    }
    
    class Item {
        public Account account { get; private set; }
        public Item(Account account) {
            this.account = account;
        }
        public String getCreatedDateJST() {
            return account.CreatedDate.format('yyyy/MM/dd HH:mm', 'JST');
        }
    }
}

 

<apex:page controller="ListPage">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!items}" var="i">
            <apex:column value="{!i.account.Name}"/>
            <apex:column value="{!i.createdDateJST}" headerValue="{!$ObjectType.Account.fields.CreatedDate.label} (JST)"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

This was selected as the best answer
KeronKeron

minoaw様

 

ご回答ありがとうございます。

やはりページ側でサクッと変換する方法はないのですね。

ご教授頂いたとおり、Apex側で変換する方法で対応することができました。

ありがとうございました。

erueru

outputTextなら以下のようにフォーマットして表示可能です。

 

<apex:outputText value="{0, date, yyyy/MM/dd}">
    <apex:param value="{!NOW()}" />
</apex:outputText>