据推测,我正在为依赖specs于Scala. 测试更多地绑定到具有某些timestamp属性的事件流。
以下存根是实际实现的部分
private def eligibleForRecentPost(optionalPost: Option[SearchEntity]): Boolean = {
optionalSearch.map(search => search.timestamp)
.exists(searchTime => searchTime >= LocalDateTime.now()
.minusDays(recencyDurationInDays).atZone(ZoneId.systemDefault).toInstant.toEpochMilli)
}
Run Code Online (Sandbox Code Playgroud)
现在,我要查找的代码可能类似于
// just a mock
when(LocalDateTime.now().minusDays(any)
.atZone(ZoneId.systemDefault).toInstant.toEpochMilli)
.thenReturn(1579625874972)
Run Code Online (Sandbox Code Playgroud)
请注意,我知道测试中的 search.timestamp 可以更新,但这需要在每次recencyDurationInDays!!
但是在specs2和/或scala中是否有更好更可靠的方法来做到这一点?
编辑:我必须提到,我不期待更改实现以致LocalDateTime被另一个类覆盖/包装。
我正在尝试将 LocalDateTime 字段保存为 long,以便可以将其存储在 SQLite 数据库中。我怎样才能做到这一点?
var datetime = LocalDateTime.now()
var longdt: Long = getString(datetime).toLong()
Run Code Online (Sandbox Code Playgroud) 缺少第二个 (00) 从 LocalDateTime.parse
LocalTime time = LocalTime.NOON;
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
String value ="20200810" + time.format(formatTime);
LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
日志
LocalTime time = LocalTime.NOON;
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
String value ="20200810" + time.format(formatTime);
LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
我也尝试更改LocalTime.NOON为,LocalTime.of(12,0,0)但结果仍然相同。
我想转换以下字符串:
private String cw = "35/19"
Run Code Online (Sandbox Code Playgroud)
分成 2 个日期。
开始日期可以格式化为:
private final DateTimeFormatter startOfWeekFormat = new DateTimeFormatterBuilder()
.appendPattern("ww/YY")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), 1)
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
调用时返回:26.08.2019
LocalDate.parse(cw, startOfWeekFormat).atStartOfDay();
Run Code Online (Sandbox Code Playgroud)
但我在本周结束时挣扎,这基本上是下周“36/19”的开始。
我尝试添加 plus 8 天,但这会引发异常:
private final DateTimeFormatter endOfWeekFormat = new DateTimeFormatterBuilder()
.appendPattern("ww/YY")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), 8)
.toFormatter();
Run Code Online (Sandbox Code Playgroud) 我正在尝试从UTC时区截断毫秒。
我有下面的代码,我可以删除毫秒,但Z最后我仍然得到。
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC );
OffsetDateTime eventDateTime=now.minus(4, ChronoUnit.MINUTES);
System.out.println("====Event date Time before truncate===");
System.out.println(eventDateTime);
System.out.println("====Event date Time after truncate===");
System.out.println(eventDateTime.truncatedTo(ChronoUnit.SECONDS));
Run Code Online (Sandbox Code Playgroud)
输出如下:
====事件日期截断前的时间===
2021-03-09T20:46:24.081Z====事件日期截断后的时间===
2021-03-09T20:46:24Z
我想实现此代码以检查用户是否订阅了服务:
public String calculateSubscription(String email) {
Optional<Subscription> subscriptionsByUserEmail = subscriptionService.findSubscriptionsByUserEmail(email);
if (subscriptionsByUserEmail.isPresent()) {
Subscription subscription = subscriptionsByUserEmail.get();
LocalDateTime startAt = subscription.getStartAt();
LocalDateTime endAt = subscription.getEndAt();
LocalDateTime now = LocalDateTime.now();
if(/* do here comparison */){
return "subscribed";
}
}
return "unsubscribed";
}
Run Code Online (Sandbox Code Playgroud)
到数据库中,我存储了 2 个字段:
@Column(name = "start_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime startAt;
@Column(name = "end_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime endAt;
Run Code Online (Sandbox Code Playgroud)
我需要检查now时间间隔startAt和endAt.
这个检查如何实现到 if 语句中?
编辑:
@Converter(autoApply = true)
public class LocalDateTimeConverter implements …Run Code Online (Sandbox Code Playgroud) 这段代码:
String dateString = "20210811T021500Z";
String dateFormat = "yyyyMMdd'T'HHmmss'Z'";
System.out.println(LocalDateTime.parse(dateString, dateFormat).toString());
System.out.println(LocalDateTime.parse(dateString, dateFormat).atZone(ZoneId.of("Europe/Budapest")).toString());
System.out.println(ZonedDateTime.of(LocalDateTime.parse(dateString, dateFormat), ZoneId.of("Europe/Budapest")).toString());
Run Code Online (Sandbox Code Playgroud)
输出这个:
2021-08-11T02:15
2021-08-11T02:15+02:00[Europe/Budapest]
2021-08-11T02:15+02:00[Europe/Budapest]
Run Code Online (Sandbox Code Playgroud)
如何打印调整后的时间?我希望结果是2021-08-11T04:15.
"2021-09-17 11:48:06 UTC"
Run Code Online (Sandbox Code Playgroud)
我想解析以下字符串并创建一个LocalDateTime对象或Instant
我知道你可以写这样的东西
String dateTime = "2021-09-17 11:48:06 UTC";
LocalDateTime dt = LocalDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Run Code Online (Sandbox Code Playgroud)
如何处理UTC字符串中的部分?
Java 将时间戳 -923130000 和 -923130001 解析为 LocalDateTime 失败。两个结果之间的秒数是3599,太奇怪了。
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
public class TestLocalDateTime {
public static void main(String[] args) {
System.out.println("-923130000: " + toLocalDateTime(-923130000L));
System.out.println("-923130001: " + toLocalDateTime(-923130001L));
System.out.println(ChronoUnit.SECONDS.between(toLocalDateTime(-923130000L), toLocalDateTime(-923130001L)));
}
private static LocalDateTime toLocalDateTime(Long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
-923130000: 1940-09-30T23:00
-923130001: 1940-09-30T23:59:59
3599
Run Code Online (Sandbox Code Playgroud)
爪哇版:
$ java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
Run Code Online (Sandbox Code Playgroud)
代码在 macOS 上执行。
我有这个 NMEA 时间戳:120722202122并且我想解析它。
我试过
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun main() {
println(LocalDateTime.parse("120722202122", DateTimeFormatter.ofPattern("DDMMyyHHmmss")))
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外:
Run Code Online (Sandbox Code Playgroud)Conflict found: Field MonthOfYear 1 differs from MonthOfYear 7 derived from 2022-01-12
我不知道异常想告诉我什么,也不知道我的模式应该是什么样子。
我想一个字符串解析"Sunday, July 4, 2021"到LocalDate,如下:
string this.selectedDate = "Sunday, July 4, 2021";
LocalDate date = LocalDate.parse(this.selectedDate);
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
java.time.format.DateTimeParseException: Text 'Sunday, July 4, 2021' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
如何将这样的字符串完整日期转换为 LocalDate?
localdatetime ×11
java ×10
java-time ×4
kotlin ×2
android ×1
date ×1
date-range ×1
datetime ×1
formatting ×1
localdate ×1
mockito ×1
scala ×1
specs2 ×1
truncate ×1