请考虑以下代码:
public static void main(String[] args) {
String pattern = "MMM dd, yyyy HH:mm:ss a z";
// joda-time
DateTime dt = DateTime.now();
System.out.println(dt.toString(pattern));
// java.time
ZonedDateTime ldt = ZonedDateTime.now();
System.out.println(ldt.toString(pattern)); // doesn't exist
System.out.println(ldt.format(DateTimeFormatter.ofPattern(pattern)));
}
Run Code Online (Sandbox Code Playgroud)
joda-time类(DateTime,LocalDate等)包含一个toString接受所需格式的String的方法.这是一种非常方便的方法.然而,java-8实现省略了这种方法.相反,你需要打电话format(DateTimeFormatter formatter).
一个小抱怨,当然.但我的问题是:是否有任何原因从java.time中省略了这个?将我的应用程序从joda-time转换为java.time将会因为这个小小的遗漏而变得更加困难.但是,如果有合理的原因,我很乐意.并不是说你在这里缓和我的担忧,但我想我会问以防万一.
编辑:对于选民来说,这不是一个基于意见的问题.任何熟悉joda-time/java.time内部工作的人以及他们以某种方式发展的动机都有资格回答这个问题,即使他们从未这样做过.
我需要检查,如果给定的日期在当月,和我写了下面的代码,但IDE提醒我,getMonth()和getYear()方法已过时.我想知道如何在较新的Java 7或Java 8中做同样的事情.
private boolean inCurrentMonth(Date givenDate) {
Date today = new Date();
return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear();
}
Run Code Online (Sandbox Code Playgroud) 我有一个Java.Time.LocalTimestartTime 实例.完成一项任务后,我想获得当前时间并获得两个差异hours.
int noOfHours = LocalTime.now() - startTime;
Run Code Online (Sandbox Code Playgroud) 我想解析两个单独的字符串"1982"并"SEP"进入一个java.time.YearMonth对象.
java.time.YearMonth.parse("1978 SEP", java.time.format.DateTimeFormatter.ofPattern("yyyy LLL"))
Run Code Online (Sandbox Code Playgroud)
给我
java.time.format.DateTimeParseException: Text '1978 SEP' could not be parsed at index 5
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.YearMonth.parse(YearMonth.java:295)
Run Code Online (Sandbox Code Playgroud) 我正在使用ZonedDateTime和Java 8的DateTimeFormatter.当我尝试解析自己的模式时,它无法识别并抛出异常.
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
ZonedDateTime dt = ZonedDateTime.parse(oraceDt, formatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '1970-01-01 00:00:00.0' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1970-01-01T00:00 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at com.timezone.Java8DsteTimes.testZonedDateTime(Java8DsteTimes.java:31)
at com.timezone.Java8DsteTimes.main(Java8DsteTimes.java:11)
Run Code Online (Sandbox Code Playgroud)
引起:java.time.DateTimeException:无法从TemporalAccessor获取ZonedDateTime:{},ISO解析为1970-01-01T00:00,类型为java.time.format.Parsed
我将要求用户输入一个特定时间:10 AM、12:30PM、2:47PM、1:09AM、5PM,等等。我将使用a Scanner来获取用户的输入。
如何将其解析/转换String为LocalTime对象?Java中是否有任何内置函数可以使我做到这一点?
我知道有类似这样的问题,但我不太能找到与我相似的例子.我了解了LocalDateTime,ZonedDateTime但我不知道怎么告诉我ZonedDateTime假设解析日期的时区.
我正在从Java 7迁移到Java 8.在我的代码中,我有一个这样的方法:
public String changeTZ(String tzFrom, String tzTo, String dateToChange) {
ZoneId zoneFrom = ZoneId.of(tzFrom);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat.toPattern());
LocalDateTime localtDateAndTime = LocalDateTime.parse(dateToChange, formatter);
ZonedDateTime dateAndTimeINeed = ZonedDateTime.of(localtDateAndTime, zoneFrom );
ZonedDateTime rezDate = dateAndTimeINeed.withZoneSameInstant(ZoneId.of(tzTo));
return formatter.format(rezDate);
}
Run Code Online (Sandbox Code Playgroud)
示例用法是:
String rez = changeTZ("CEST", "UTC", "2017-08-10 14:23:58");
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,它以字符串的形式接收日期时间,时区日期是tzFrom变量,而TZ我需要(toTo变量).
在Java 7中执行该操作的代码是荒谬而复杂的,因此我不会在此处进行讨论.你能告诉我如何在不改变方法接口(参数和返回类型)的情况下在Java 8中实现相同的功能吗?DST怎么样?它是在Java 8中自动处理的吗?
请注意,该方法还支持"CEST","CET","UTC"形式的时区以及"欧洲/伦敦"等标准时区.
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime accessStart;
private ZoneId accessStartZoneId;
Run Code Online (Sandbox Code Playgroud)
Jackson中是否有内置的模式映射器来映射包含java的JSON请求ZoneId?
{
"accessStart": "2017-10-10T10:10:10.100",
"accessStartZoneID": "Australia/Sydney"
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Spring Boot 1.5.4.RELEASE.如何配置解串器ZoneId?它适用于accessStart
JSON解析错误:无法构造java.time.ZoneId的实例:抽象类型要么需要映射到具体类型,要么具有自定义反序列化器,要么包含其他类型信息
我正在使用clojure.java-time0.3.2版,并且我有一个TemporalAdjuster定义如下:
(def next-pay-day
(reify java.time.temporal.TemporalAdjuster
(adjustInto [this temporal]
(let [local-date (java.time.LocalDate/from temporal)
day-of-month (j/as local-date :day-of-month)
last-day-of-month (j/as (j/adjust local-date :last-day-of-month) :day-of-month)]
(cond
(= day-of-month last-day-of-month)
(j/plus local-date (j/days 14))
(< day-of-month 14)
(j/plus local-date (j/days (- 14 day-of-month)))
:else
(j/adjust local-date :last-day-of-month))))))
Run Code Online (Sandbox Code Playgroud)
哪个是这样的:
=> (require '[pto-calculator.logic.pay-periods :as p])
nil
=> (require '[java-time :as j])
nil
=> (take 4 (j/iterate j/adjust (j/local-date 2019 3 3) p/next-pay-day))
(#object[java.time.LocalDate 0x74e53020 "2019-03-03"] #object[java.time.LocalDate 0x49980a "2019-03-14"] #object[java.time.LocalDate 0x2e332a36 "2019-03-31"] #object[java.time.LocalDate …Run Code Online (Sandbox Code Playgroud) 我想将一个时间戳值保存到postgres数据库中。对应的列是TIMESTAMP WITHTIME TIME ZONE的类型。输入的Java应用程序获取纪元时间(长值)时,可以使用ZoneOffset.UTC将其转换为Instant或OffsetDateTime。
最好的方法是什么?这种方法有什么缺点吗?