我现在正在巴黎运行这段代码,其中12:40,其中ECT = ECT - 欧洲/巴黎
LocalDateTime creationDateTime = LocalDateTime.now(Clock.systemUTC());
ZoneId zone = ZoneId.of(ZoneId.SHORT_IDS.get("ECT"));
System.out.println ("creationDateTime --------------------------> " + creationDateTime);
System.out.println ("creationDateTime.atZone(zone).getHour() ---> " + creationDateTime.atZone(zone).getHour());
System.out.println ("creationDateTime.atZone(zone).getMinute() -> " + creationDateTime.atZone(zone).getMinute());
Run Code Online (Sandbox Code Playgroud)
但我在控制台得到这个
creationDateTime --------------------------> 2017-05-16T10:40:07.882
creationDateTime.atZone(zone).getHour() ---> 10
creationDateTime.atZone(zone).getMinute() -> 40
Run Code Online (Sandbox Code Playgroud)
我不应该得到12:40 ???????
我们将日期存储在sqlserver db表中,作为varchar。当在Java代码中将其作为字符串读取然后解析为Date时,它将被读取为UTC(Java代码位于UT中的服务器中)。将日期重新转换为ET时要晚4小时。如何处理将日期存储在此db列中的ET中,以便在Java代码中将其读取为ET。
我们正在研究偏移量,但并未确切了解该怎么做。
表03/29/2019 23:23:03中的Varchar日期//我们希望此日期位于ET中
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date beginDate = sdf.parse("03/29/2019 23:23:03");
Run Code Online (Sandbox Code Playgroud)
//问题是执行此代码时,服务器处于UTC状态。因此beginDate //被解读为UTC时间03/29/2019 23:23:03而不是美国东部时间03/29/2019 23:23:03
预计于03/29/2019 23:23:03 ET实际03/29/2019 23:23:03 UTC
日期格式将第12小时值(hh)解析为00,同时将格式应用为"yyyy-MM-dd'T'hh:mm:ss"但不解析第13小时到下午1点.PFB示例代码段.
Date testDate = DateUtil.parse("yyyy-MM-dd'T'hh:mm:ss","2010-07-09T12:50:58"); 能不能让我知道为什么这样转换......?
我只找到了Joda Time的解决方案.
我的解决方案只有在最后一天不在第一周时才有效:
LocalDate.now() // or any other LocalDate
.withDayOfMonth(31)
.withMonth(12)
.get(weekFields.weekOfWeekBasedYear())
Run Code Online (Sandbox Code Playgroud)
那么Java Time中的正确方法是什么(比如Joda Time)?
我想设置日期实例的小时和分钟.我有3个解决方案.
我该拿哪个,为什么?:
版本1:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 8);
calendar.set(Calendar.HOUR, 30);
return calendar.getTime();
Run Code Online (Sandbox Code Playgroud)
版本2:
ZonedDateTime i = date.toInstant()
.atZone(ZoneId.systemDefault())
.withHour(8)
.withMinute(30);
return Date.from(i.toInstant());
Run Code Online (Sandbox Code Playgroud)
版本3:
ZonedDateTime i = date.toInstant()
.atZone(ZoneId.systemDefault())
.withHour(8)
.withMinute(30);
return new Date(i.toEpochSecond());
Run Code Online (Sandbox Code Playgroud) 对于我只能假设是时区问题,以下代码会在同一天生成不同的一年中的值,但是时间不同:
注意,代码是用Kotlin编写的
例
fun main(args: Array<String>) {
val middaySundayAfterEpoch = Instant.EPOCH + Duration
.ZERO
.plusDays(3)
.plusHours(12)
val almostMidnightSundayAfterEpoch = Instant.EPOCH + Duration
.ZERO
.plusDays(3)
.plusHours(23)
println(getWeekOfYear(middaySundayAfterEpoch))
println(getWeekOfYear(almostMidnightSundayAfterEpoch))
}
fun getWeekOfYear(instant: Instant): Int {
val calendar: Calendar = Calendar.getInstance()
calendar.time = Date.from(instant)
return calendar.get(Calendar.WEEK_OF_YEAR)
}
Run Code Online (Sandbox Code Playgroud)
结果
1
2
假设
题
如何修改此选项以忽略时区,以便两个时间都在同一周内发生?
我有一个非常特殊的问题,我想在哪里解析"2019-12-25T17:00:00-05:00"
,以便它可以给我结果DEC 12 | Thursday | 5:00pm
我通过使用DateTimeFormatter和尝试了以下代码LocalDate
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US);
LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter);
String formattedDate = outputFormatter.format(date);
contentTextView.setText(formattedDate);
Run Code Online (Sandbox Code Playgroud)
但是它崩溃了 DateTimeParseException: Text '2019-12-25T17:00:00-05:00' could not be parsed at index 19
知道为什么会崩溃以及我的输出是否可以提供预期的结果吗?谢谢!
我在下面编写了正在运行并提供输出的代码。但是我不确定这是正确的。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
sdf.setTimeZone(TimeZone.getTimeZone("GMT-7"));
String value = sdf.format(date);
System.out.println(value);
Date date2 = sdf.parse(value);
long result = date2.getTime();
System.out.println(result);
return result;
Run Code Online (Sandbox Code Playgroud)
上面的代码我正在尝试的是,我只需要获取GMT时区的当前时间并将其转换为将在Oracle db中使用的纪元格式即可。
有人可以告诉我这种格式,并且上面的代码正确吗?
我在执行下面的代码时遇到异常我正在使用java datetime API.
String strDate = "12/4/2018 5:26:28 PM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/d/yyyy HH:mm:ss a", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter);
Run Code Online (Sandbox Code Playgroud)
以下异常即将来临
Exception in thread "main" java.time.format.DateTimeParseException: Text '12/4/2018 5:26:28 PM' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Test.main(Test.java:20)
Run Code Online (Sandbox Code Playgroud) 我需要获取现有Java TimeZone对象的缩写(作为String)。没有直接映射,有没有办法做到这一点?
Eg- TimeZone - ("America/Los_Angeles")
Abbreviation - PDT
Run Code Online (Sandbox Code Playgroud)