我有以下代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.UK);
Instant inst = DateTimeUtils.toInstant(sdf.parse("2019-08-13T18:00:00Z"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm").withLocale(Locale.UK).withZone(ZoneId.of("Europe/London"));
System.out.println(formatter.format(inst));
//prints 18:00
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶,因为我认为inst这将是 GMT/UTC 时间,并且formatter会将其格式化为伦敦时间(该日期为 BST(UTC+1:00)),生成19:00.
我在这里缺少什么?
我猜这是我的代码一个通用的问题,但如果它的确与众不同,这是利用org.threeten.bp.*从类ThreeTen-反向移植项目,在另外的适应Android的早期ThreeTenABP项目。
我正在尝试使用以下代码从 spring 的 application.properties 解析 LocalTime:
@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}')}")
private LocalTime myDateTime;
Run Code Online (Sandbox Code Playgroud)
在 application.properties 中,我定义了这样的属性:
app.myDateTime=21:45:00
Run Code Online (Sandbox Code Playgroud)
错误信息:
Failed to bind properties under 'app.my-date-time' to java.time.LocalTime:
Property: app.my-date-time
Value: 21:45:00
Origin: class path resource [application.properties]:44:15
Reason: failed to convert java.lang.String to @org.springframework.beans.factory.annotation.Value java.time.LocalTime
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?谢谢你。
调试模式错误:
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalTime] for value '21:45:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96)
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88)
at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:313) …Run Code Online (Sandbox Code Playgroud) 我需要将字符串(日期格式)转换为时间戳。
首先,我就是这样做的。
private Timestamp dateConverter(String date) {
Timestamp timestamp = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(date);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
Run Code Online (Sandbox Code Playgroud)
结果是: ParseException: Unparseable date: "16 Jan 2020 11:02"
我用另一种方式试过:
private Timestamp dateConverter(String date) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm");
LocalDateTime ldt = LocalDateTime.parse(date , f);
return Timestamp.valueOf(ldt);
}
Run Code Online (Sandbox Code Playgroud)
此时我有内部服务器错误 DateTimeParseException: Text '16 Jan 2020 11:02' could …
我有字符串格式的日期输入(例如:-)2020-01-08T07:00:00,我想检查输入时间是否在6:00到之间15:00。知道如何检查吗?
我试过下面的代码:-
java.util.Date inputDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2020-01-08T07:00:00");
if(inputDate.getTime() < "06:00" && inputDate.getTime() > "15:00")
Run Code Online (Sandbox Code Playgroud)
但它显然无法与 String 进行比较,所以我很困惑如何比较它?
我LocalData在代码中使用对象,我注意到 using
compareTo方法不一致。
我使用该compareTo方法是为了获得两个日期之间的差异(以天为单位)。
但是,它似乎只适用于同一月份的日期。
我附上一个代码片段来演示这个问题:
import java.text.MessageFormat;
import java.time.LocalDate;
public class timeComparison {
public static void main(String[] args) {
LocalDate valentinesDay = LocalDate.of(2020, 2, 14);
LocalDate darwinDay = LocalDate.of(2020, 2, 12);
LocalDate leapDay = LocalDate.of(2020, 2, 29);
LocalDate superTuesday = LocalDate.of(2020, 3, 3);
String valentinesVsDarwin = MessageFormat.format(
"Valentine day is {0} days after Darwin day",
valentinesDay.compareTo(darwinDay)
);
System.out.println(valentinesVsDarwin);
String darwinVsLeap = MessageFormat.format(
"Leap day is {0} days after Darwin day",
leapDay.compareTo(darwinDay)
);
System.out.println(darwinVsLeap);
String …Run Code Online (Sandbox Code Playgroud) 据推测,我正在为依赖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被另一个类覆盖/包装。
我已经实现了一个供国际使用的 API,我正在尝试以 GMT 时间以 ISO 8601 格式返回对象的日期。现在我这样做
ZonedDateTime.withZoneSameInstant(ZoneOffset.UTC)
Run Code Online (Sandbox Code Playgroud)
但我知道 UTC 和 GMT 不是一回事。
稍后我将为每个用户分配他们喜欢的 Locale 并返回他们的 Locale ,但现在我想以 GMT 的方式执行此操作,但我不知道如何以正确的方式执行此操作
缺少第二个 (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)但结果仍然相同。
我有地图Map<LocalDateTime, String>。
如何按键值对其进行排序?我应该使用什么比较器?
someMap.entrySet().stream().sorted(Comparator.comparing(???))
Run Code Online (Sandbox Code Playgroud)
或者
someMap.entrySet().stream().sorted(??)
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我应该写什么来代替“??” ?
排序:
Key Value
2020-01-09 09:57:58.631 Some info
2020-01-09 09:57:59.224 Some info
2020-01-09 09:59:03.144 Info
Run Code Online (Sandbox Code Playgroud)
不排序:
Key Value
2020-01-09 09:57:58.631 Some info
2020-01-09 09:59:03.144 Info
2020-01-09 09:57:59.224 Some info
Run Code Online (Sandbox Code Playgroud) 我从服务器收到 UTC 格式的时间。我解析为一个Instant对象,然后将其转换为LocalDateTime. 像这样:
fun String.toLocaleDateTime(): LocalDateTime {
return LocalDateTime.ofInstant(
Instant.parse(this),
ZoneOffset.systemDefault()
)
}
Run Code Online (Sandbox Code Playgroud)
问题是不同年份的LocalDateTime对象是不同的。
例如,将 UTC 时间转换为 IST 时间时:
1800-01-01T03:36:32Z -> 1800-01-01T09:30
1870-01-01T03:36:32Z -> 1870-01-01T08:57:42
1906-01-01-01T30Z:36:32Z -01-01T09:06:32
java-time ×10
java ×8
datetime ×2
date ×1
java-8 ×1
java-stream ×1
kotlin ×1
mockito ×1
parsing ×1
scala ×1
sorting ×1
specs2 ×1
spring ×1
spring-boot ×1
threetenabp ×1
threetenbp ×1
timezone ×1