[英]How do I truncate a java string to fit in a given number of bytes, once UTF-8 encoded?
[英]How do I truncate a part of the string in Java?
在測試中,我想在收到特定響應時驗證系統日期時間和區域。 但是,由於在系統生成響應和測試中進行驗證之間存在一點延遲,因此失敗了。
回復的日期時間為(例如)-2019-08-09T09:02:30.836 + 10:00
測試驗證發生在(例如)-2019-08-09T09:02:30.956 + 10:00
如何刪除毫秒位(如果可能)或“ 2:30”之后的所有內容
這樣可以將String解析為ZonedDateTime,然后將納秒設置為0並進行比較。
ZonedDateTime first = ZonedDateTime.parse("2019-08-09T09:02:30.836+10:00").withNano(0);
ZonedDateTime second = ZonedDateTime.parse("2019-08-09T09:02:30.956+10:00").withNano(0);
boolean datesEqual = first.isEqual(second);
這是將字符串解析為ZonedDateTime並檢查兩個值是否在某個最大持續時間內(例如1秒)的方法。
ZonedDateTime first = ZonedDateTime.parse("2019-08-09T09:02:30.836+10:00");
ZonedDateTime second = ZonedDateTime.parse("2019-08-09T09:02:30.956+10:00");
Duration duration = Duration.between(first, second);
Duration maximumDuration = Duration.ofSeconds(1);
boolean withinDuration = duration.minus(maximumDuration).isNegative();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.