【问题标题】:What would be the best way to display a localDateTime in the correct format? [duplicate]以正确格式显示 localDateTime 的最佳方法是什么? [复制]
【发布时间】:2021-01-21 07:39:44
【问题描述】:

我一直在构建一个小型银行应用程序,遇到了一个问题,即交易的 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 表示为字符串的想法。

这是 HMTL 页面:

<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 中做到这一点?

【问题讨论】:

  • “我应该尝试在 HTML 文件中制作这些格式吗?” --- 如果“在 HTML 文件中”是指“在 Thymeleaf 模板中”,那么是的。请参阅顶部的重复链接。对于LocalDateTime,请参阅answer using #temporals

标签: java html spring-boot thymeleaf localdatetime


【解决方案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(); // 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't need :) )

【讨论】:

    【解决方案2】:

    试试这个:

       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){
               
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-21
      • 2011-12-14
      • 2021-04-06
      • 2011-06-20
      • 2012-08-30
      • 2010-09-16
      • 2011-04-10
      • 2020-06-07
      相关资源
      最近更新 更多