英文:
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("transactions", transactions);
...
So I've tried using .format( DateTimeFormatter.ofPattern( "HH:mm:ss" ) )
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:
<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>
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("yyyy/MM/dd HH:mm:ss")); // 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'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( "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){
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论