什么是显示LocalDateTime的正确格式的最佳方法?

huangapple go评论82阅读模式
英文:

What would be the best way to display a localDateTime in the correct format?

问题

我一直在构建一个小型银行应用程序,遇到了一个问题,交易的localDateTime以完整格式显示,即"2020-10-06T11:54:00.517734"。

显然,这样的显示方式不太友好,所以我尝试了一些不同的格式化方法,但大多数方法最终都导致空指针异常。

以下是从数据库中向模型添加数据的部分:

for (Transaction transaction : allTransactions) {
    TransactionInfo transactionInfo = new TransactionInfo();

    BankAccount bankAccount;

    if (transaction.getDebitAccount() == selectedBankAccount) {
        bankAccount = transaction.getCreditAccount();
        transactionInfo.setAmount(transaction.getAmount().negate());
    } else {
        bankAccount = transaction.getDebitAccount();
        transactionInfo.setAmount(transaction.getAmount());
    }
    
    transactionInfo.setDateTime(transaction.getDateTime());
    transactionInfo.setName(bankAccount.getAccountName());
    transactionInfo.setIban(bankAccount.getIban());
    transactionInfo.setDescription(transaction.getDescription());
    transactionInfo.setTransactionId(transaction.getId());

    transactions.add(transactionInfo);
}
modelAndView.addObject("transactions", transactions);
...

因此,我尝试在transactionInfo.setDateTime(transaction.getDateTime())使用.format(DateTimeFormatter.ofPattern("HH:mm:ss"))

但是,这需要一个localDateTime数据类型。当我尝试在对象类中更改这个数据类型时,我一直收到空指针异常,而且我不喜欢将dateTime表示为字符串。

以下是HTML页面的部分内容:

<table class="transaction-table">
    <tr>
        <th>Afzender</th>
        <th>Tegenrekening</th>
        <th>Bedrag</th>
        <th>Datum</th>
        <th>Beschrijving</th>
    </tr>

    <tr th:each="transaction : ${transactions}">
        <td th:text="${transaction.name}"></td>
        <td th:text="${transaction.iban}"></td>
        <td>€<span th:text="${transaction.amount}"></span></td>
        <td th:text="${transaction.dateTime}"></td>
        <td th:text="${transaction.description}"></td>
    </tr>
</table>

我应该尝试在HTML文件中进行这些格式化吗?还是在Java中有更好的方法来处理?

英文:

I've been building a small banking app, and have run into an issue where the localDateTime of a transaction is displayed as the full format "2020-10-06T11:54:00.517734".

This is obviously pretty not great to look at so I've tried a few different methods of formatting it however most end up in null pointer exceptions.

Here the data gets added to models from the database:

for (Transaction transaction : allTransactions) {
    TransactionInfo transactionInfo = new TransactionInfo();

    BankAccount bankAccount;

    if (transaction.getDebitAccount() == selectedBankAccount) {
        bankAccount = transaction.getCreditAccount();
        transactionInfo.setAmount(transaction.getAmount().negate());
    } else {
        bankAccount = transaction.getDebitAccount();
        transactionInfo.setAmount(transaction.getAmount());
    }
    
    transactionInfo.setDateTime(transaction.getDateTime());
    transactionInfo.setName(bankAccount.getAccountName());
    transactionInfo.setIban(bankAccount.getIban());
    transactionInfo.setDescription(transaction.getDescription());
    transactionInfo.setTransactionId(transaction.getId());

    transactions.add(transactionInfo);
}
modelAndView.addObject(&quot;transactions&quot;, transactions);
... 

So I've tried using .format( DateTimeFormatter.ofPattern( &quot;HH:mm:ss&quot; ) ) at transactionInfo.setDateTime(transaction.getDateTime()).

However this requires a localDateTime data type. when I try to change this in the object class I keep getting null pointer exceptions and I don't like the idea of representing the dateTime as a String.

this is the HMTL page:

&lt;table class=&quot;transaction-table&quot;&gt;
                    &lt;tr&gt;
                        &lt;th&gt;Afzender&lt;/th&gt;
                        &lt;th&gt;Tegenrekening&lt;/th&gt;
                        &lt;th&gt;Bedrag&lt;/th&gt;
                        &lt;th&gt;Datum&lt;/th&gt;
                        &lt;th&gt;Beschrijving&lt;/th&gt;
                    &lt;/tr&gt;

                    &lt;tr th:each=&quot;transaction : ${transactions}&quot;&gt;
                        &lt;td th:text=&quot;${transaction.name}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${transaction.iban}&quot;&gt;&lt;/td&gt;
                        &lt;td&gt;€&lt;span th:text=&quot;${transaction.amount}&quot;&gt;&lt;/span&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${transaction.dateTime}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${transaction.description}&quot;&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;

Should I try to make these formats in the HTML file instead? or is there a better method to do it in Java?

答案1

得分: 1

这应该可以工作。如果你得到了NPE(空指针异常),你可能在一个没有实际对象支持的引用上调用了某个方法(例如,某个 getSomething() 返回了 null,然后你试图对其进行操作)。

以下是一些示例:

LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE); // 2020-10-06
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME); 
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")); // 2020/10/06 15:20:03

还有一些其他有用的方法,你可以考虑使用:

LocalDateTime.now().toLocalDate(); // 仅获取日期
LocalDateTime.now().toLocalTime(); // 仅获取时间
LocalDateTime.now().withNano(0); // 打印类似 2020-10-06T15:26:58 的内容(没有通常不需要的纳秒部分 :))
英文:

It should work. If you are getting NPE, you probably call some method on a reference with no actual object behind it (e.g. some getSomething() returns null and you try to do smth. on it).

Here are a few examples:

LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE); // 2020-10-06
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME); 
LocalDateTime.now().format(DateTimeFormatter.ofPattern(&quot;yyyy/MM/dd HH:mm:ss&quot;)); // 2020/10/06 15:20:03

There are some other useful methods, you can consider:

LocalDateTime.now().toLocalDate(); // get date only
LocalDateTime.now().toLocalTime(); // get time only
LocalDateTime.now().withNano(0); // prints something like 2020-10-06T15:26:58 (no nanos which usually we don&#39;t need :) )

答案2

得分: 0

尝试一下:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
    Date date = format.parse("2020-10-06T11:54:00.517734");
    System.out.println(date);
} catch (Exception ex) {

}
英文:

Try this:

   SimpleDateFormat format = new SimpleDateFormat( &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&quot;,Locale.US);
       format.setTimeZone(TimeZone.getTimeZone(&quot;UTC&quot;));
       try{
       Date date = format.parse(&quot;2020-10-06T11:54:00.517734&quot;);
       System.out.println(date);
       }catch(Exception ex){
           
       }

huangapple
  • 本文由 发表于 2020年10月6日 20:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64225564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定